Open dfrankow opened 4 years ago
I thought I'd see if other ways to compute (other vce-s) would produce different results for "data".
However, other vce values didn't work for me with or without the "data" argument:
> summary(margins(M1, vce="bootstrap"))
Error in `*tmp*`[["call"]] : this S4 class is not subsettable
> summary(margins(M1, vce="simulation"))
Error in `[[<-`(`*tmp*`, "model", value = NULL) :
[[<- defined for objects of type "S4" only for subclasses of environment
> summary(margins(M1, data=Lucy, vce="bootstrap"))
Error in `*tmp*`[["call"]] : this S4 class is not subsettable
> summary(margins(M1, data=Lucy, vce="simulation"))
Error in `[[<-`(`*tmp*`, "model", value = NULL) :
[[<- defined for objects of type "S4" only for subclasses of environment
Not sure why yet. I might file this as a separate issue around lme4.
Yet some of the average marginal effects change with a different dataset.
> mtcars$make <- stringr::str_extract(string = row.names(mtcars), pattern = "\\w+\\b")
> M1 <- lmer(mpg ~ cyl * hp + wt + (1|make), data = mtcars)
boundary (singular) fit: see ?isSingular
> summary(M1)
Linear mixed model fit by REML ['lmerMod']
Formula: mpg ~ cyl * hp + wt + (1 | make)
Data: mtcars
REML criterion at convergence: 153.1
Scaled residuals:
Min 1Q Median 3Q Max
-1.4915 -0.6308 -0.2750 0.5424 1.9096
Random effects:
Groups Name Variance Std.Dev.
make (Intercept) 0.000 0.000
Residual 5.027 2.242
Number of obs: 32, groups: make, 22
Fixed effects:
Estimate Std. Error t value
(Intercept) 52.017520 4.916935 10.579
cyl -2.742125 0.800228 -3.427
hp -0.163594 0.052122 -3.139
wt -3.119815 0.661322 -4.718
cyl:hp 0.018954 0.006645 2.852
Correlation of Fixed Effects:
(Intr) cyl hp wt
cyl -0.846
hp -0.903 0.688
wt -0.055 -0.365 -0.029
cyl:hp 0.946 -0.789 -0.979 0.025
convergence code: 0
boundary (singular) fit: see ?isSingular
> fixef(M1)
(Intercept) cyl hp wt cyl:hp
52.01752019 -2.74212515 -0.16359434 -3.11981472 0.01895364
> margins(M1, data = mtcars[mtcars$am == 0, ])
Average marginal effects
cyl hp wt
0.2954 -0.03192 -3.12
> margins(M1, data = mtcars)
Average marginal effects
cyl hp wt
0.03814 -0.04632 -3.12
> margins(M1, data = sample_n(mtcars, 20))
Average marginal effects
cyl hp wt
-0.02133 -0.05177 -3.12
Thanks for your work on the margins package.
We would like to estimate average treatment effects on a different population than our survey population. One technique to do this is poststratification (see here). The basic idea as I understand it is to make a model with the data we have, stratifying by variables we believe to be explanatory (e.g., age, sex, etc. for surveys), then use that model to predict on the population we are interested in, and make prediction averages weighted by the target population sizes for those strata (see here).
Can we pass a model and a new target population into margins to do that? When I tried supplying a different population as
data
, the predictions seemed the same.For example (from here):
The output:
Margins agrees with the fixed effects from the model.
I'd expect at least the error to be smaller if we had more data (e.g.,
Lucy
instead of the smallerSLucy
). However, all terms including error are exactly the same.Perhaps I just misunderstand the nature of margins on fixed effect models?
By the way, the blog post does poststratification by doing the reweighting with some matrix math:
However, that won't give me error terms like margins would.