lm() (Linear Model) (R)
lm() fits a linear regression model in R using a formula and a data frame. It is the foundational function for modeling a numeric response as a linear combination of predictors.
R
lm(y ~ x, data = df) fits an ordinary least-squares linear regression, estimating coefficients that best predict the response y from the predictor(s). It returns a model object (an S3 list) you then explore: summary() gives coefficients, standard errors, p-values, and R-squared; coef() extracts the estimates; predict() makes predictions; and plot() produces diagnostic plots.
The right-hand side uses formula syntax, so you add predictors with +, interactions with *, and transformations inline (lm(log(y) ~ x)). Factor predictors are automatically expanded into indicator variables, with the factor's first level as the baseline — which is why managing factor levels matters for interpretation.
lm() is the gateway to R's modeling ecosystem; glm() generalizes it to logistic and other regressions, and the same formula-and-data interface recurs across packages. Always check assumptions with the diagnostic plots before trusting the fit.
Syntax
model <- lm(y ~ x1 + x2, data = df)
summary(model)Examples
R — summary() reports the intercept, slope, R-squared, and significance.
# Predict mpg from car weight
model <- lm(mpg ~ wt, data = mtcars)
# View the full model summary
summary(model)