Skip to main content
HomeAbout RLearn R

GLM in R: Generalized Linear Model

Learn about generalized linear models (GLM) and how they differ from linear models.
Jun 2020  · 2 min read

Generalized linear model (GLM) is a generalization of ordinary linear regression that allows for response variables that have error distribution models other than a normal distribution like Gaussian distribution.

Basics of GLM

GLMs are fit with function glm(). Like linear models (lm()s), glm()s have formulas and data as inputs, but also have a family input.

Generalized Linear Model Syntax

Generalized Linear Model Syntax

The Gaussian family is how R refers to the normal distribution and is the default for a glm().

Similarity to Linear Models

If the family is Gaussian then a GLM is the same as an LM.

Similarity to Linear Models

Non-normal errors or distributions

Generalized linear models can have non-normal errors or distributions. However, there are limitations to the possible distributions. For example, you can use Poisson family for count data, or you can use binomial family for binomial data.

GLMs also have a non-linear link functions, which links the regression coefficients to the distribution and allows the linear model to generalize.

Interactive Example of Predicting with glm()

This example predicts the expected number of daily civilian fire injury victims for the North American summer months of June, July, and August using the Poisson regression you and the newDat dataset.

Here is the data in the newDat dataset:

1     6
2     7
3     8 

The Poisson slope and intercept estimates are on the natural log scale and can be exponentiated to be more easily understood. You can do this by specifying type = "response" with the predict function.

# use the model to predict with new data
predOut <- predict(object = poissonOut, newdata = newDat, type = "response")

# print the predictions

When we run the above code, it produces the following result:

         1          2          3
0.08611111 0.12365591 0.07795699 

Try it for yourself.

To learn more about generalized linear models in R, please see this video from our course, Generalized Linear Models in R.

This content is taken from DataCamp’s Generalized Linear Models in R course by Richard Erickson.

Check out our Logistic Regression in R Tutorial.

R Courses

Certification available

Introduction to R

BeginnerSkill Level
4 hr
Master the basics of data analysis in R, including vectors, lists, and data frames, and practice R with real data sets.
See DetailsRight Arrow
Start Course
See MoreRight Arrow

Google Cloud for Data Scientists: Harnessing Cloud Resources for Data Analysis

How can using Google Cloud make data analysis easier? We explore examples of companies that have already experienced all the benefits.
Oleh Maksymovych's photo

Oleh Maksymovych

9 min

40 R Programming Interview Questions & Answers For All Levels

Learn the 40 fundamental R programming interview questions and answers to them for all levels of seniority: entry-level, intermediate, and advanced questions.
Elena Kosourova's photo

Elena Kosourova

20 min

A Guide to Docker Certification: Exploring The Docker Certified Associate (DCA) Exam

Unlock your potential in Docker and data science with our comprehensive guide. Explore Docker certifications, learning paths, and practical tips.
Matt Crabtree's photo

Matt Crabtree

8 min

Bash & zsh Shell Terminal Basics Cheat Sheet

Improve your Bash & zsh Shell skills with the handy shortcuts featured in this convenient cheat sheet!
Richie Cotton's photo

Richie Cotton

6 min

Functional Programming vs Object-Oriented Programming in Data Analysis

Explore two of the most commonly used programming paradigms in data science: object-oriented programming and functional programming.
Amberle McKee's photo

Amberle McKee

15 min

A Comprehensive Introduction to Anomaly Detection

A tutorial on mastering the fundamentals of anomaly detection - the concepts, terminology, and code.
Bex Tuychiev's photo

Bex Tuychiev

14 min

See MoreSee More