The Difference Between glm and lm in R

Spread the love

In the R programming language, which is primarily used for statistical analysis and data visualization, lm and glm are two essential functions. While both are used for regression modeling, they serve different purposes and are applicable in different scenarios. This comprehensive guide will take you through the nuances and differences between these two functions.


  1. Introduction to Regression Modeling
  2. Breaking Down lm
  3. Introduction to Generalized Linear Models and glm
  4. Key Differences Between lm and glm
  5. Practical Examples
  6. Conclusion

1. Introduction to Regression Modeling

Regression modeling is a statistical technique that establishes a relationship between a dependent variable and one or more independent variables. Regression is used for prediction, forecasting, and determining the causal-effect relationship.

2. Breaking Down lm

lm stands for Linear Models. It’s used for simple and multiple linear regression analysis.

Features of lm:

  • Assumes that the relationship between variables is linear.
  • Assumes that the errors, or residuals, are normally distributed and have constant variance (homoscedasticity).
  • Is best suited for continuous dependent variables.


model <- lm(dependent_var ~ independent_var, data = dataset)

3. Introduction to Generalized Linear Models and glm

While lm is specifically designed for linear regression, glm (Generalized Linear Models) provides a more generalized framework.

Features of glm:

  • Can model relationships that are not necessarily linear.
  • Doesn’t assume that the residuals have a normal distribution.
  • Allows for response variables that have error distribution models other than a normal distribution. Examples include binomial, Poisson, and gamma distributions.
  • Incorporates a link function to relate the linear model to the mean of the response variable.


model <- glm(formula, family = gaussian, data = dataset)

Where family specifies the error distribution and link function.

4. Key Differences Between lm and glm

  1. Purpose:
    • lm is specifically for linear regression.
    • glm is more versatile and can handle various distributions and link functions.
  2. Distribution Assumption:
    • lm assumes that the residuals are normally distributed.
    • glm allows for other distributions such as binomial, Poisson, etc.
  3. Response Variable:
    • lm is limited to continuous response variables.
    • glm can handle binary, count, and other types of response variables.
  4. Flexibility:
    • lm is a specific case of glm. When using glm with the Gaussian family and identity link function, it becomes equivalent to lm.

5. Practical Examples

a. Using lm for Simple Linear Regression:

data <- data.frame(x = 1:10, y = 2*(1:10) + rnorm(10))
linear_model <- lm(y ~ x, data = data)

b. Using glm for Logistic Regression (a type of generalized linear model):

data <- data.frame(x = rnorm(100), y = ifelse(rnorm(100) > 0, 1, 0))
logistic_model <- glm(y ~ x, family = binomial(link="logit"), data = data)

6. Conclusion

In the vast landscape of regression modeling in R, both lm and glm play crucial roles. While lm is tailored for linear relationships with continuous response variables, glm offers a flexible framework for a broader set of relationships and variable types. For budding statisticians and seasoned data scientists alike, understanding when and how to use each function is key to successful data analysis and modeling in R. As always, the choice between lm and glm should be driven by the nature of your data and the specific problem you’re trying to solve.

Posted in RTagged

Leave a Reply