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
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.
Load the mtcars dataset. Build a linear regression of mpg on wt, qsec, am, and hp.
Print the coefficients from the linear model in the previous exercise.
Using Margins package find marginal effects.
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?
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.
Using your favorite dataset (mine is field.goals from the nutshell package), construct a logistic regression.
Explain why marginal effects for a logit model more complex than for a linear model?
For the next two exercises, you may use either package. Calculate the marginal effects with respect to the mean.
Calculate the average marginal effects.
If these marginal effects are different, explain why they are different.