**R-exercises**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

**leave a comment**for the author, please follow the link and comment on their blog:

**R-exercises**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.