leeper / margins

An R Port of Stata's 'margins' Command
https://cloud.r-project.org/package=margins
Other
260 stars 39 forks source link

margins() not working with nested random effects in lmer() #182

Open cknotz opened 2 years ago

cknotz commented 2 years ago

Dear Thomas,

first, thanks for putting together and maintaining this package - would be difficult to work without it!

A possible bug: I have an issue with getting marginal effects from a linear mixed model that is estimated with lmerTest::lmer() (the same with lme4::lmer()) and which includes nested random effects.

To give an example:

library(lmerTest)
library(margins)

# Example data: 96 students clustered in 10 schools
mlmdata <- haven::read_dta("https://stats.idre.ucla.edu/stat/examples/imm/imm10.dta")

# Factor variable used below for nested random effects:
mlmdata$parented_fac <- as.factor(mlmdata$parented)

This, without nesting, works:

m1 <- lmerTest::lmer(math ~ parented_fac + (1|schid),
                         data = mlmdata)
summary(m1)
margins(m1)

This, with nesting, does not:

m2 <- lmerTest::lmer(math ~ parented_fac + (1|schid) + (1|schid:parented_fac),
                     data = mlmdata)
summary(m2)
margins(m2)

The error I get is:

Error in levelfun(r, n, allow.new.levels = allow.new.levels) : 
  new levels detected in newdata

I get the same issue when I work with my actual data (which I cannot share at the moment). I also get the same issue when I try to get margins from an interactive model with at().

traceback()

16: stop("new levels detected in newdata")
15: levelfun(r, n, allow.new.levels = allow.new.levels)
14: (function (r, n) 
    levelfun(r, n, allow.new.levels = allow.new.levels))(dots[[1L]][[2L]], 
        dots[[2L]][[2L]])
13: mapply(FUN = f, ..., SIMPLIFY = FALSE)
12: Map(function(r, n) levelfun(r, n, allow.new.levels = allow.new.levels), 
        re[names(new_levels)], new_levels)
11: mkNewReTrms(object, rfd, re.form, na.action = na.action, allow.new.levels = allow.new.levels)
10: predict.merMod(model, newdata = out, type = type, re.form = re.form, 
        ...)
9: predict(model, newdata = out, type = type, re.form = re.form, 
       ...)
8: prediction.merMod(model = model, data = d0, type = type, calculate_se = FALSE, 
       ...)
7: prediction(model = model, data = d0, type = type, calculate_se = FALSE, 
       ...)
6: dydx.factor(data = data, model = model, varslist$fnames[i], type = type, 
       fwrap = FALSE, as.data.frame = as.data.frame, ...)
5: marginal_effects.lmerMod(model = model, data = data, variables = variables, 
       type = type, eps = eps, varslist = varslist, ...)
4: marginal_effects(model = model, data = data, variables = variables, 
       type = type, eps = eps, varslist = varslist, ...)
3: build_margins(model = model, data = data_list[[i]], variables = variables, 
       type = type, vcov = vcov, vce = vce, iterations = iterations, 
       unit_ses = unit_ses, eps = eps, varslist = varslist, ...)
2: margins.lmerMod(m2)
1: margins(m2)

SessionInfo()

R version 4.1.2 (2021-11-01)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 10.16

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] margins_0.3.26 lmerTest_3.1-3 lme4_1.1-27.1  Matrix_1.4-0  

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.8          nloptr_1.2.2.3      pillar_1.6.5        compiler_4.1.2      forcats_0.5.1       tools_4.1.2        
 [7] boot_1.3-28         lifecycle_1.0.1     tibble_3.1.6        gtable_0.3.0        nlme_3.1-153        lattice_0.20-45    
[13] pkgconfig_2.0.3     rlang_0.4.12        rstudioapi_0.13     cli_3.1.1           DBI_1.1.2           curl_4.3.2         
[19] yaml_2.2.1          haven_2.4.3         dplyr_1.0.7         generics_0.1.1      vctrs_0.3.8         hms_1.1.1          
[25] grid_4.1.2          tidyselect_1.1.1    data.table_1.14.2   glue_1.6.1          R6_2.5.1            fansi_1.0.2        
[31] prediction_0.3.14   minqa_1.2.4         tzdb_0.2.0          readr_2.1.1         ggplot2_3.3.5       purrr_0.3.4        
[37] magrittr_2.0.1      scales_1.1.1        ellipsis_0.3.2      MASS_7.3-54         splines_4.1.2       assertthat_0.2.1   
[43] colorspace_2.0-2    numDeriv_2016.8-1.1 utf8_1.2.2          munsell_0.5.0       crayon_1.4.2      

I hope you can help! Many thanks in any case for your time!

Best, Carlo