R Codes
# load the library
library(margins)
library(dplyr)
library(knitr)
library(stargazer)
# Setting up the Stata engine
library(Statamarkdown)
options(width = 100)
# call the data mtcars from base R
data("mtcars")
# fit a model
fit1 <- lm(mpg ~ cyl + hp + wt, data = mtcars)
# print the marginal effects in a table
summary(margins(fit1)) %>%
knitr::kable(caption = 'Summary of marginal effects of OLS without interactions in R')
Summary of marginal effects of OLS without interactions in
R
cyl |
-0.9416168 |
0.5509168 |
-1.709181 |
0.0874173 |
-2.0213939 |
0.1381603 |
hp |
-0.0180381 |
0.0118763 |
-1.518838 |
0.1288033 |
-0.0413151 |
0.0052389 |
wt |
-3.1669731 |
0.7405770 |
-4.276359 |
0.0000190 |
-4.6184774 |
-1.7154688 |
Stata Codes
* clear the memory
clear all
cls
* load the data
insheet using "F:\mtcars.csv", comma clear
* create a log file
* log using homework3, replace
quietly reg mpg cyl hp wt
margins, dydx(*)
. * clear the mem. clear all
. cls
. * load the data
. insheet using "F:\mtcars.csv", comma clear
(12 vars, 32 obs)
. * create a log file
. * log using homework3, replace
. quietly reg mpg cyl hp wt
. margins, dydx(*)
Average marginal effects Number of obs = 32
Model VCE: OLS
Expression: Linear prediction, predict()
dy/dx wrt: cyl hp wt
------------------------------------------------------------------------------
| Delta-method
| dy/dx std. err. t P>|t| [95% conf. interval]
-------------+----------------------------------------------------------------
cyl | -.9416166 .5509165 -1.71 0.098 -2.070118 .1868846
hp | -.0180381 .0118763 -1.52 0.140 -.0423655 .0062893
wt | -3.166973 .740576 -4.28 0.000 -4.683975 -1.649972
------------------------------------------------------------------------------