How to Use lm() Function in R to Fit Linear Models

Spread the love

Linear regression models are foundational in the world of statistical modeling and data analysis. In R, one of the most popular and easy-to-use functions to fit such models is the lm() function, which stands for “linear model”. In this article, we’ll provide a deep dive into the usage, interpretation, and nuances of the lm() function in R.


  1. Introduction to Linear Models
  2. Syntax and Basic Usage
  3. Interpretation of Output
  4. Diagnostic Plots
  5. Assumptions and Model Validation
  6. Extensions and Advanced Usage
  7. Conclusion

1. Introduction to Linear Models

Linear models attempt to model the relationship between two or more variables by fitting a linear equation to observed data. The simplest form of the equation with two variables is:


  • Y is the dependent variable.
  • X is the independent variable.
  • β0​ is the intercept.
  • β1​ is the slope.
  • ϵ is the error term.

2. Syntax and Basic Usage

The basic usage of lm() is:

model <- lm(formula, data)


  • formula: A symbolic description of the model. For example, y ~ x models y as a function of x.
  • data: The dataset used.


model <- lm(mpg ~ wt, data = mtcars)

3. Interpretation of Output

When you fit a linear model using lm() and then call the summary() function, the output comprises:

  • Call: Shows the model formula.
  • Residuals: Summary statistics of the residuals.
  • Coefficients: Shows estimates, standard error, t-values, and p-values.
  • R-squared: Proportion of variance explained by the model.
  • Adjusted R-squared: Adjusts R-squared for the number of predictors in the model.
  • F-statistic: A measure to assess the significance of the overall model.

4. Diagnostic Plots

After fitting a linear model, it’s crucial to examine diagnostic plots:


This command produces four plots:

  1. Residuals vs. Fitted: Checks for linearity.
  2. Normal Q-Q: Checks the normality of residuals.
  3. Scale-Location: Checks for equal variance (homoscedasticity).
  4. Residuals vs. Leverage: Identifies influential cases.

5. Assumptions and Model Validation

Linear regression makes several assumptions:

  • Linearity: The relationship between predictors and response is linear.
  • Independence: Observations are independent of each other.
  • Homoscedasticity: The variance of residuals is constant.
  • Normality: Residuals are normally distributed.

Violation of these assumptions may lead to biased or inefficient estimates. It’s essential to check these assumptions using diagnostic plots and statistical tests.

6. Extensions and Advanced Usage

  • Multiple Linear Regression: Include more than one predictor.
model <- lm(mpg ~ wt + hp, data = mtcars)

Interaction Effects: To see if the effect of one predictor varies depending on the level of another predictor.

model <- lm(mpg ~ wt * hp, data = mtcars)

Polynomial Regression: Introduce polynomial terms.

model <- lm(mpg ~ wt + I(wt^2), data = mtcars)

Factor Variables: lm() can handle categorical predictors by introducing dummy variables automatically.

model <- lm(mpg ~ gear, data = mtcars)

7. Conclusion

The lm() function in R is a versatile and foundational tool for fitting and analyzing linear models. Proper interpretation of its output, combined with thorough diagnostic checks, ensures that you can leverage linear regression effectively in your data analysis projects. Remember that while lm() provides a lot of information, the responsibility lies with the analyst to interpret and use this information correctly.

Posted in RTagged

Leave a Reply