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:

  Month   
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
print(predOut)

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.

Topics

R Courses

Certification available

Course

Introduction to R

4 hr
2.7M
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
Related

Data Science in Finance: Unlocking New Potentials in Financial Markets

Discover the role of data science in finance, shaping tomorrow's financial strategies. Gain insights into advanced analytics and investment trends.
 Shawn Plummer's photo

Shawn Plummer

9 min

5 Common Data Science Challenges and Effective Solutions

Emerging technologies are changing the data science world, bringing new data science challenges to businesses. Here are 5 data science challenges and solutions.
DataCamp Team's photo

DataCamp Team

8 min

Navigating R Certifications in 2024: A Comprehensive Guide

Explore DataCamp's R programming certifications with our guide. Learn about Data Scientist and Data Analyst paths, preparation tips, and career advancement.
Matt Crabtree's photo

Matt Crabtree

8 min

A Data Science Roadmap for 2024

Do you want to start or grow in the field of data science? This data science roadmap helps you understand and get started in the data science landscape.
Mark Graus's photo

Mark Graus

10 min

R Markdown Tutorial for Beginners

Learn what R Markdown is, what it's used for, how to install it, what capacities it provides for working with code, text, and plots, what syntax it uses, what output formats it supports, and how to render and publish R Markdown documents.
Elena Kosourova 's photo

Elena Kosourova

12 min

Introduction to DynamoDB: Mastering NoSQL Database with Node.js | A Beginner's Tutorial

Learn to master DynamoDB with Node.js in this beginner's guide. Explore table creation, CRUD operations, and scalability in AWS's NoSQL database.
Gary Alway's photo

Gary Alway

11 min

See MoreSee More