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.

**What's next:**

- Explore all our (>1000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below

Marten says

Trying to install and load “Margins” produces an error. Is this package on CRAN or did you mean “margins”?