Plot random effects from lmer (lme4 package) using qqmath or dotplot: How to make it look fancy?

前端 未结 4 1938
耶瑟儿~
耶瑟儿~ 2020-12-07 08:46

The qqmath function makes great caterpillar plots of random effects using the output from the lmer package. That is, qqmath is great at plotting the intercepts from a hiera

4条回答
  •  有刺的猬
    2020-12-07 08:52

    Another way to do this is to extract simulated values from the distribution of each of the random effects and plot those. Using the merTools package, it is possible to easily get the simulations from a lmer or glmer object, and to plot them.

    library(lme4); library(merTools)       ## for lmer(), sleepstudy
    fit <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy)
    randoms <- REsim(fit, n.sims = 500)
    

    randoms is now an object with that looks like:

    head(randoms)
    groupFctr groupID        term       mean     median       sd
    1   Subject     308 (Intercept)   3.083375   2.214805 14.79050
    2   Subject     309 (Intercept) -39.382557 -38.607697 12.68987
    3   Subject     310 (Intercept) -37.314979 -38.107747 12.53729
    4   Subject     330 (Intercept)  22.234687  21.048882 11.51082
    5   Subject     331 (Intercept)  21.418040  21.122913 13.17926
    6   Subject     332 (Intercept)  11.371621  12.238580 12.65172
    

    It provides the name of the grouping factor, the level of the factor we are obtaining an estimate for, the term in the model, and the mean, median, and standard deviation of the simulated values. We can use this to generate a caterpillar plot similar to those above:

    plotREsim(randoms)
    

    Which produces:

    One nice feature is that the values that have a confidence interval that does not overlap zero are highlighted in black. You can modify the width of the interval by using the level parameter to plotREsim making wider or narrower confidence intervals based on your needs.

提交回复
热议问题