<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/calculating-marginal-effects-exercises/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/09/04/calculating-marginal-effects/"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img sizes="(max-width: 300px) 100vw, 300px" src="https://i1.wp.com/www.r-exercises.com/wp-content/uploads/2017/09/city-408952_960_720-300x212.jpg?resize=300%2C212" alt="" class="wp-image-7838" data-recalc-dims="1">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.</p>
<p>Exercises in this section will be solved using the <code>Margins</code> and <code>mfx</code> packages. It is recommended to take a look at the concise and excellent documentation for these packages before continuing.</p>
<p>Answers to the exercises are available <a rel="nofollow" href="http://r-exercises.com/2017/09/04/calculating-marginal-effects-solutions/" target="_blank">here</a>.</p>
<p><strong>Exercise 1</strong><br>
Load the <em>mtcars</em> dataset. Build a linear regression of <em>mpg</em> on <em>wt</em>, <em>qsec</em>, <em>am</em>, and <em>hp</em>.</p>
<p><strong>Exercise 2</strong><br>
Print the coefficients from the linear model in the previous exercise.</p>
<p><strong>Exercise 3</strong><br>
Using Margins package find marginal effects.</p>
<p><strong>Exercise 4</strong><br>
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?</p>
<p><strong>Exercise 5</strong><br>
Using the <em>mtcars</em> dataset, built a linear regression similar to Exercise 1 except include an interaction term with <em>am</em> and <em>hp</em>. Find the marginal effects for this regression.</p>
<p><strong>Exercise 6</strong><br>
Using your favorite dataset (mine is <em>field.goals</em> from the <em>nutshell</em> package), construct a logistic regression.</p>
<p><strong>Exercise 7</strong><br>
Explain why marginal effects for a logit model more complex than for a linear model?</p>
<p><strong>Exercise 8</strong><br>
For the next two exercises, you may use either package. Calculate the marginal effects with respect to the mean.</p>
<p><strong>Exercise 9</strong><br>
Calculate the average marginal effects.</p>
<p><strong>Exercise 10</strong><br>
If these marginal effects are different, explain why they are different.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/05/11/introduction-to-copulas-exercises-part-1/" title="Introduction to copulas Exercises (Part-1)" target="_blank">Introduction to copulas Exercises (Part-1) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/03/11/multiple-regression-part-3-diagnostics/" title="Multiple Regression (Part 3) Diagnostics" target="_blank">Multiple Regression (Part 3) Diagnostics </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/05/17/introduction-to-copulas-exercises-part-2/" title="Introduction to copulas Exercises (Part-2)" target="_blank">Introduction to copulas Exercises (Part-2) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/" target="_blank">Explore all our (>1000) R exercises</a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/r-courses/" target="_blank">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.r-exercises.com/2017/09/04/calculating-marginal-effects/"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/_igC-3UIack" height="1"></a></div>