简体   繁体   English

使用 plot_models (r) 重新排序 sjPlot 中的森林 model 变量

[英]Reordering forested model variables in sjPlot with plot_models (r)

I have a group of models that I am plotting together in R using the plot_models function from the sjPlot package.我有一组模型,我使用plot_models package 中的sjPlot function 在 R 中一起绘制模型。 The models are plotting confidence intervals for similar felm regressions that use different datasets.这些模型正在绘制使用不同数据集的类似felm回归的置信区间。 All the models share the same variables of interest, and their confidence intervals occupy the same ranges.所有模型共享相同的感兴趣变量,并且它们的置信区间占据相同的范围。 I have successly plotted all of the models, however, I now cannot order them how would be ideal.我已经成功地绘制了所有模型,但是,我现在无法命令它们如何是理想的。

My code is the following:我的代码如下:

placebo_models <- paste0("outcome", 1:13, " ~ Variable1 + Variable2 | fe_variable |0| fe_variable") |> lapply(\(x) felm(as.formula(x), data = df))


models_list <- list(placebo_models[[13]], placebo_models[[12]], placebo_models[[11]], placebo_models[[10]], placebo_models[[9]], placebo_models[[8]], placebo_models[[7]], placebo_models[[6]], placebo_models[[5]], placebo_models[[4]], placebo_models[[3]], placebo_models[[2]], placebo_models[[1]])
                  
testplot <- plot_models(models_list, colors = c("RED"), show.values = TRUE, p.threshold = 0.05, spacing = 0.8, dot.size = 1, digits = 5, title = "Placebo Models", axis.labels = c("Variable1", "Variable2")) + scale_color_discrete(labels = c("m1", "m2", "m3", "m4", "m5", "m6", "m7", "m8", "m9", "m10", "m11", "m12", "m13"))

My current plot output is the following:我当前的 plot output 如下:

阴谋

Ideally, I would like to change this model to report in order (descending from the top) m1's Variable 1 confidence interval, followed by m1's Variable 2 confidence interval, before going to m2 (Variable1, then Variable2), m3 (Variable1, then Variable2), etc.理想情况下,我想更改此 model 以按顺序报告(从顶部下降)m1 的变量 1 置信区间,然后是 m1 的变量 2 置信区间,然后再转到 m2(变量 1,然后是变量 2),m3(变量 1,然后是变量 2) ), ETC。

Is there a way to reorder the plotted lines such that this is possible?有没有办法重新排序绘制的线,这样就可以了? The variables are identical across all the models, so it should fit, as long as it can be restructured.所有模型的变量都是相同的,所以它应该适合,只要它可以重组。 Can the variables of different models be reordered and inter-mixed within the same plot like so?不同模型的变量可以像这样在同一个 plot 中重新排序和混合吗?

I am unfortunately not able to share the original data, but this visualization should explain the problem.不幸的是,我无法分享原始数据,但这个可视化应该可以解释这个问题。 Many thanks!非常感谢!

The order in which elements appear in your legend is based on the order you feed them to plot_models .元素出现在图例中的顺序取决于您将它们提供给plot_models的顺序。 The order in which the y-axis appears is the order in which they are placed in your first model. y 轴出现的顺序是它们在您的第一个 model 中的放置顺序。

Check it out.一探究竟。 This is the example published by sjPlot::plot_models() .这是sjPlot::plot_models()发布的示例。 First, as it's presented in help, then how you can change the order of the legend items or the plotted items.首先,如帮助中所述,然后如何更改图例项或绘图项的顺序。 As for the y-axis, the ability to rearrange it may be impacted by your model.至于 y 轴,重新排列它的能力可能会受到 model 的影响。 You can rearrange the order in which you call the independent variables in many regression models without changing the results.您可以重新排列在许多回归模型中调用自变量的顺序,而无需更改结果。 I run a few checks here to show that this lm is not impacted by variable order.我在这里进行了一些检查,以表明此lm不受可变顺序的影响。

library(sjPlot)

data(efc)

# fit three models
fit1 <- lm(barthtot ~ c160age + c12hour + c161sex + c172code, 
           data = efc)
fit2 <- lm(neg_c_7 ~ c160age + c12hour + c161sex + c172code, 
           data = efc)
fit3 <- lm(tot_sc_e ~ c160age + c12hour + c161sex + c172code, 
           data = efc)

# plot multiple models
plot_models(fit1, fit2, fit3, grid = TRUE)

# plot multiple models with legend labels and
# point shapes instead of value labels
plot_models(
  fit1, fit2, fit3,
  axis.labels = c(
    "Carer's Age", "Hours of Care", "Carer's Sex", 
    "Educational Status"
  ),
  m.labels = c("Barthel Index", "Negative Impact", 
               "Services used"),
  show.values = FALSE, show.p = FALSE, p.shape = TRUE
)

在此处输入图像描述

If I wanted to rearrange the legend Dependent Variables , I need to reorder the plots in the call.如果我想重新排列图例Dependent Variables ,我需要重新排序调用中的图。 So I'll reference the model order.所以我将参考 model 订单。 The labels need to be in the same order as the plots.标签需要与绘图的顺序相同。

plot_models( fit3, fit2, fit1, axis.labels = c( "Carer's Age", "Hours of Care", "Carer's Sex", "Educational Status" ), m.labels = rev("Barthel Index", "Negative Impact", "Services used"), show.values = FALSE, show.p = FALSE, p.shape = TRUE ) plot_models(fit3,fit2,fit1,axis.labels = c(“看护者年龄”,“护理时间”,“看护者性别”,“教育状况”),m.labels = rev(“巴塞尔指数”,“负面影响” ", "使用的服务"), show.values = FALSE, show.p = FALSE, p.shape = TRUE)

在此处输入图像描述

Next, I've rearranged the call for fit1 and returned to the original plotting order, you'll see that this plot has a different order on the y-axis, but the legend matches the legend in the first plot.接下来,我重新安排了对fit1的调用并返回到原始绘图顺序,您会看到这个 plot 在 y 轴上的顺序不同,但图例与第一个 plot 中的图例匹配。

fit1a <- lm(barthtot ~ c172code + c161sex + c12hour + c160age, 
           data = efc)
plot_models(
  fit1a, fit2, fit3,
  axis.labels = rev(c(
    "Carer's Age", "Hours of Care", "Carer's Sex", 
    "Educational Status")),
  m.labels = c("Barthel Index", "Negative Impact", 
               "Services used"),
  show.values = FALSE, show.p = FALSE, p.shape = TRUE
)

# validate models are identical with R2 and Fstat
summary(fit1)[[8]] # [1] 0.2695598 
summary(fit1a)[[8]] # [1] 0.2695598 

summary(fit1)[[10]][1] # value # 75.28364  
summary(fit1a)[[10]][1] # value # 75.28364 

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM