i'm trying to replicate the output of margins female, atmeans
in R shown here:
https://stats.idre.ucla.edu/stata/dae/using-margins-for-predicted-probabilities/
i can re-create the same setup shown on the ucla page with
library(foreign)
library(margins)
x <- read.dta( "https://stats.idre.ucla.edu/stat/data/hsbdemo.dta" )
x$honors <- as.numeric( x$honors == 'enrolled' )
and this code matches the logit honors i.female read line on ucla's page
this_model <- glm( honors ~ female + read , data = x , family = binomial() )
summary(this_model)
from here, i'm confused about how to modify
summary(margins(this_model))
so that i'm replicating the stata atmeans
parameter. my goal is to reproduce the output shown on the ucla page, but in R instead of stata:
margins female, atmeans
Adjusted predictions Number of obs = 200
Model VCE : OIM
Expression : Pr(honors), predict()
at : 0.female = .455 (mean)
1.female = .545 (mean)
read = 52.23 (mean)
------------------------------------------------------------------------------
| Delta-method
| Margin Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
female |
0 | .1127311 .0350115 3.22 0.001 .0441097 .1813524
1 | .2804526 .0509114 5.51 0.000 .1806681 .3802371
------------------------------------------------------------------------------
the R margins
library help pages discusses the atmeans
command, but it's not obvious to me how to implement this:
atmeans: calculate marginal effects at the mean (MEMs) of a dataset rather than the default behavior of calculating average marginal effects (AMEs)
Stata’s atmeans argument is not implemented in margins() for various reasons, including because it is possible to achieve the effect manually through an operation like
data$var <- mean(data$var, na.rm = TRUE)
and passing the modified data frame tomargins(x, data = data)
.
data
andvar
is the variable that you're interested in. You'd replace the raw values ofvar
with the average of the values ofvar
. That's what the linedata$var <- mean(data$var, na.rm = TRUE)
accomplishes. Then you just supplydata
tomargins
. Can you clarify what part you're having trouble with? $\endgroup$