如何制作混合模型的森林情节

发布于 2025-02-05 06:08:06 字数 251 浏览 4 评论 0原文

如何为混合模型共同制作的森林图及其相应的置信区间。 但是,我尝试了此代码

Model = lme (fixed = score~ Age+Sex+yearsofeducation+walkspeed,
random = ~1|ID, 
data=DB,
na.action = na.omit, method = "ML", 
)
plot_summs (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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没有伤那来痛 2025-02-12 06:08:06

我将其称为“系数情节”,而不是“森林情节”。 您比较许多不同研究中相同效应的估计值时,荟萃分析中使用了森林图。

(当 对您的(我不清楚为什么要提到或(=赔率比?),这些通常来自逻辑回归...?)

library(nlme)
mtcars <- transform(mtcars, cylgear = interaction(cyl, gear))
m1 <- lme(mpg ~ disp + hp + drat + wt + qsec,
    random = ~1|cylgear,
    data = mtcars)

系数图:dotwhisker,

您可以获得您想要的 直接从dotwhisker软件包中,但它不会对效果进行排序(据我所知,或者不容易):

library(dotwhisker)
library(broom.mixed)  ## required to 'tidy' (process) lme fits
dwplot(m1, effects = "fixed")

系数图:tidyverse

我通常自己进行处理,因为我更喜欢提高灵活性。

library(tidyverse)
tt <- (m1
    ## extract estimates and CIs
    |> tidy(effects = "fixed", conf.int = TRUE)
    ## usually *don't* want to compare intercept (dwplot does this automatically)
    |> filter(term != "(Intercept)")
    ## scale parameters by 2SD - usually necessary for comparison
    |> dotwhisker::by_2sd(data = mtcars)
    ## take only the bits we need, rename some (cosmetic)
    |> select(term, estimate, lwr = conf.low, upr = conf.high)
    ## order terms by estimate value
    |> mutate(across(term, ~reorder(factor(.), estimate)))
)

gg0 <- (ggplot(tt,
               aes(estimate, term))
    + geom_pointrange(aes(xmin = lwr, xmax = upr))
    + geom_vline(xintercept = 0, lty = 2)
)
print(gg0)

唯一剩下的/可能性的棘手问题是,如果您具有相似幅度的正和负系数,该怎么办。如果您想按绝对值进行排序,那么

|> mutate(across(term, ~reorder(factor(.), estimate, 
 FUN = function(x) mean(abs(x)))

尽管这有点丑陋。

如果您喜欢Tidyverse,则可以替换forcats :: fct_reorder作为重新排序

I would call this a "coefficient plot", not a "forest plot". (A forest plot is used in meta-analyses, when you are comparing the magnitude of estimates of the same effect from many different studies.)

example setup

This is a slightly silly example, but should be close enough to yours (not clear to me why you're mentioning OR (= odds ratios?), these are typically from a logistic regression ... ?)

library(nlme)
mtcars <- transform(mtcars, cylgear = interaction(cyl, gear))
m1 <- lme(mpg ~ disp + hp + drat + wt + qsec,
    random = ~1|cylgear,
    data = mtcars)

coefficient plots: dotwhisker

You could get approximately what you want directly from the dotwhisker package, but it won't sort effects (or not easily, as far as I know):

library(dotwhisker)
library(broom.mixed)  ## required to 'tidy' (process) lme fits
dwplot(m1, effects = "fixed")

coefficient plots: tidyverse

I usually do the processing myself, as I prefer increased flexibility.

library(tidyverse)
tt <- (m1
    ## extract estimates and CIs
    |> tidy(effects = "fixed", conf.int = TRUE)
    ## usually *don't* want to compare intercept (dwplot does this automatically)
    |> filter(term != "(Intercept)")
    ## scale parameters by 2SD - usually necessary for comparison
    |> dotwhisker::by_2sd(data = mtcars)
    ## take only the bits we need, rename some (cosmetic)
    |> select(term, estimate, lwr = conf.low, upr = conf.high)
    ## order terms by estimate value
    |> mutate(across(term, ~reorder(factor(.), estimate)))
)

gg0 <- (ggplot(tt,
               aes(estimate, term))
    + geom_pointrange(aes(xmin = lwr, xmax = upr))
    + geom_vline(xintercept = 0, lty = 2)
)
print(gg0)

enter image description here

The only remaining/possibility tricky question here is what to do if you have positive and negative coefficients of similar magnitude. If you want to sort by absolute value then

|> mutate(across(term, ~reorder(factor(.), estimate, 
 FUN = function(x) mean(abs(x)))

although this gets a bit ugly.

If you like the tidyverse you can substitute forcats::fct_reorder for reorder.

风吹过旳痕迹 2025-02-12 06:08:06

我只是为Ben Bolker的出色答案添加了一个选项:使用models ummummary软件包。 (免责声明:我是作者。)

使用该软件包,您可以使用modelplot()函数来创建森林图,而coef_map参数to重命名和重新排序系数。如果您要估计logit模型并想要优势比,则可以使用endentiate参数。

您在coef_map向量中插入系数的顺序将它们从底部到顶部分类。例如:

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"))

“”

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"))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文