Search code examples
rr-forestplot

How to make a forest plot for a mixed model


How to make a forest plots for mixed models co-effiecents and their corresponding confidence interval. I tried this code

Model = lme (fixed = score~ Age+Sex+yearsofeducation+walkspeed,
random = ~1|ID, 
data=DB,
na.action = na.omit, method = "ML", 
)
plot_summs (model)

However, I want the OR in the forest plots to be ordered in a descending fashion. Thanks for the help.


Solution

  • I’m just adding one more option to Ben Bolker’s excellent answer: using the modelsummary package. (Disclaimer: I am the author.)

    With that package, you can use the modelplot() function to create a forest plot, and the coef_map argument to rename and reorder coefficients. If you are estimating a logit model and want the odds ratios, you can use the exponentiate argument.

    The order in which you insert coefficients in the coef_map vector sorts them in the plot, from bottom to top. For example:

    library(lme4)
    library(modelsummary)
    
    mod <- lmer(mpg ~ wt + drat + (1 | gear), data = mtcars)
    
    modelplot(
        mod,
        coef_map = c("(Intercept)" = "Constant",
                     "drat" = "Rear Axle Ratio",
                     "wt" = "Weight"))