To illustrate how to calculate the marginal effect of a categorical variable, I'll use the sprogram
data that is mentioned in the video from Stata. Here is the output from Stata including the marginal effect of summer
:
fracreg logit prate i.summer pdonations freemeals
------------------------------------------------------------------------------
| Robust
prate | Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
summer |
yes | .579492 .0517463 11.20 0.000 .4780711 .680913
pdonations | .0415092 .0108759 3.82 0.000 .0201928 .0628257
freemeals | -.5200508 .0912252 -5.70 0.000 -.6988489 -.3412526
_cons | 1.203022 .0719935 16.71 0.000 1.061917 1.344126
------------------------------------------------------------------------------
margins r.summer
--------------------------------------------------------------
| Delta-method
| Contrast Std. Err. [95% Conf. Interval]
-------------+------------------------------------------------
summer |
(yes vs no) | .0930492 .0089097 .0755864 .1105119
--------------------------------------------------------------
Here is the replication in R:
library(margins)
library(sandwich)
library(lmtest)
dat <- read.csv("sprogram.csv", sep=";")
# The model
mod <- glm(prate~summer + freemeals + pdonations, data = dat, family = quasibinomial())
To replicate Stata's standard errors (almost), we need to use robust standard errors:
print(coeftest(mod, vcov = vcovHC(mod, type = "HC1")), digits = 6)
Estimate Std. Error z value Pr(>|z|)
(Intercept) 1.2030216 0.0721018 16.68504 < 2.22e-16 ***
summeryes 0.5794920 0.0518241 11.18190 < 2.22e-16 ***
freemeals -0.5200508 0.0913621 -5.69219 1.2542e-08 ***
pdonations 0.0415092 0.0108923 3.81089 0.00013847 ***
Finally, we use margins
together with robust standard errors for the marginal effect of summer
:
res <- margins(mod, variable = "summer", type = "response", vcov = vcovHC(mod, type = "HC1"))
summary(res)
factor AME SE z p lower upper
summeryes 0.0930 0.0089 10.4488 0.0000 0.0756 0.1105
The results are virutally identical.