How to Fit Linear Model and Interpret Results in R
To fit linear model in R, use the lm() function with a formula interface. Whether you’re running simple regression or multiple regression with interactions, lm() provides a consistent way to model relationships between variables. The formula weight ~ height reads as “weight explained by height,” and R handles the rest — estimating coefficients, computing standard errors, and producing diagnostic measures automatically.
# Create data and fit a simple linear model
df <- data.frame(
height = c(150, 160, 170, 180, 190, 155, 165, 175, 185, 195),
weight = c(55, 65, 75, 85, 95, 58, 68, 78, 88, 98)
)
model <- lm(weight ~ height, data = df)
summary(model)
# Coefficients:
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) -95.0000 4.0825 -23.27 1.25e-09 ***
# height 1.0000 0.0233 42.91 1.05e-14 ***
# Multiple R-squared: 0.9958, Adjusted R-squared: 0.9948
The summary shows that for every 1-unit increase in height, weight increases by 1 unit. Use predict(model, newdata = new_data) to make predictions on fresh data. For multiple regression, add predictors with +: lm(weight ~ height + age, data = df). Use * to include interaction terms: lm(weight ~ height * age, data = df).
# Extract model components
coef(model) # coefficients
residuals(model) # residuals
fitted(model) # fitted values
summary(model)$r.squared # R-squared: 0.9958
# Diagnostic plots
par(mfrow = c(2, 2))
plot(model)
The four diagnostic plots help check linearity, normality of residuals, constant variance, and influential points. For categorical predictors, R automatically creates dummy variables — just include the factor column in the formula. When interpreting results, focus on the p-values in the coefficients table and the adjusted R-squared; a significant coefficient with a tiny R-squared often means your model explains the relationship but large amounts of variation remain unaccounted for.
See also
- sd(), Standard deviation for regression diagnostics
- var(), Variance calculation
- How to Run a t-Test in R, Related statistical tests