Calculating Marginal Effects Exercises

September 4, 2017

(This article was first published on R-exercises, and kindly contributed to R-bloggers)

A common experience for those in the social sciences migrating to R from SPSS or STATA is that some procedures that happened at the click of a button will now require more work or are too obscured by the unfamiliar language to see how to accomplish. One such procedure that I’ve experienced is when calculating the marginal effects of a generalized linear model. In this exercise set, we will explore calculating marginal effects for linear, logistic, and probit regression models in R.

Exercises in this section will be solved using the Margins and mfx packages. It is recommended to take a look at the concise and excellent documentation for these packages before continuing.

Answers to the exercises are available here.

Exercise 1
Load the mtcars dataset. Build a linear regression of mpg on wt, qsec, am, and hp.

Exercise 2
Print the coefficients from the linear model in the previous exercise.

Exercise 3
Using Margins package find marginal effects.

Exercise 4
Verify that you receive the same results from Exercises 2 and 3. Why do these marginal effects match the coefficients found when printing the linear model object?

Exercise 5
Using the mtcars dataset, built a linear regression similar to Exercise 1 except include an interaction term with am and hp. Find the marginal effects for this regression.

Exercise 6
Using your favorite dataset (mine is field.goals from the nutshell package), construct a logistic regression.

Exercise 7
Explain why marginal effects for a logit model more complex than for a linear model?

Exercise 8
For the next two exercises, you may use either package. Calculate the marginal effects with respect to the mean.

Exercise 9
Calculate the average marginal effects.

Exercise 10
If these marginal effects are different, explain why they are different.

To leave a comment for the author, please follow the link and comment on their blog: R-exercises. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)