Search code examples
rplotrandom-effects

Problem combining ranef plots of two models with grid.arrange()


I have two LMEs:

lme1 <- lmer(F1 ~ (phoneme|individual) + (1|word) + frequency, 
         data = nurse_female)
lme2 <- lmer(F2 ~ (phoneme|individual) + (1|word) + 
                   frequency + age + (1|zduration), 
         data = nurse_female)

I created simple dotplots dotplot(ranef(lme1)) of the random effects which creates a plot for each random predictor. I am however only interested in the phoneme|individual one which looks like this:enter image description here

Normally I would use grid.arrange() but I can't get it to only select the phoneme|individual plots. Do you know a way to do this?


Solution

  • (A reproducible example would be useful, I hope this example does what you want ...). I think the key here is to recognize that the dotplot.ranef.mer method returns a list of plots:

    library(lme4)
    fm1 <- lmer(angle ~ (1|recipe) + (1|recipe:replicate), cake, REML= FALSE)
    dd <- dotplot(ranef(fm1))
    length(dd) ## 2
    

    They're not necessarily in the same order as in the formula:

    names(dd) ## [1] "recipe:replicate" "recipe"          
    print(dd[["recipe"]])
    print(dd[["recipe:replicate"]])
    

    So you would want something like

    f <- function(m) dotplot(ranef(m))[["individual"]]
    gridExtra::grid.arrange(f(lme1),f(lme2))