如何在不更改底层数据的情况下更改 plot_model 中的 facets 顺序?

huangapple go评论108阅读模式
英文:

How can I change the order of facets in plot_model without changing the underlying data?

问题

我使用sjPlot的plot_model函数生成了一个预测的边际效应图,该图显示了在名为"height"的变量的选定水平上的预测结果,以明确它们是-1标准差、均值和+1标准差。不幸的是,由于字母排序的原因,当"height"包括负值和正值,并且均值是一个比-1标准差更接近零的负值时,facet(子图)将不会按照前面列出的方式排序。因此,我需要以某种方式重新排序它们。由于"height"实际上是数值型的,我仅手动选择了"height"的特定值来显示在图中,因此我认为我无法通过重新排序底层数据来实现这一点,因为该变量不是因子。除非将其转换为因子并重新排序所有水平?但我希望有一种更方便的解决方案,不需要修改底层数据。

英文:

I have a predicted marginal effects plot made with plot_model from sjPlot, displaying the predictions at selected levels of the variable height which I named to clarify they are -1 SD, mean, and +1 SD, respectively. Unfortunately, due to alphabetical ordering, when height includes both negative and positive values and the mean is a negative value closer to zero than -1 SD, the facets will not be ordered in the way listed before. Thus, I have to reorder them somehow. Since height is actually numeric and I only manually selected specific values of height to display in the plot, I don't see that I could achieve this by reordering the underlying data, since the variable is not a factor. Except maybe converting it to a factor and reordering all the levels? But I hope there's a more convenient solution that doesn't require modifying the underlying data.

library(dplyr)
library(sjPlot)

#specifying the model
model = lm(mass ~ gender*species*height, data = starwars[starwars$species == "Human" | starwars$species == "Droid" | starwars$species == "Twi'lek",])

#plotting predictions at three specified levels of height, let's pretend they're -1 SD, mean and +1 #SD
plot = plot_model(model, type = "pred", terms = c("species", "gender", "height [-2, -1, 1]"), ci.lvl=0.95) 


#renaming facets to specify that they represent -1 SD, mean, and +1 SD
plot$data$facet <- ifelse(plot$data$facet == "height = -2", "height = -2 (-1 SD)",
                             ifelse(plot$data$facet == "height = -1", "height = -1 (mean)",
                                    ifelse(plot$data$facet == "height = 1", "height = 1 (+1 SD)",  99)))

如何在不更改底层数据的情况下更改 plot_model 中的 facets 顺序?

答案1

得分: 2

你可以通过将数据中的facet变量转换为具有所需顺序的因子来实现这一点。

library(dplyr)
library(sjPlot)

# 指定模型
model = lm(mass ~ gender*species*height, data = starwars[starwars$species == "Human" | starwars$species == "Droid" | starwars$species == "Twi'lek",])

# 在三个指定的身高水平上绘制预测,假设它们是 -1 SD、平均和 +1 SD
plot = plot_model(model, type = "pred", terms = c("species", "gender", "height [-2, -1, 1]"), ci.lvl=0.95) 
#> 在预测的时候出现警告: prediction from rank-deficient fit; attr(*, "non-estim") has doubtful cases

# 重新设置facet变量的水平顺序
plot$data$facet <- factor(case_when(
  plot$data$facet == "height = -2" ~ "height = -2 (-1 SD)", 
  plot$data$facet == "height = -1" ~ "height = -1 (mean)", 
  plot$data$facet == "height = 1" ~ "height = 1 (+1 SD)", 
), levels=c("height = -2 (-1 SD)", "height = -1 (mean)", "height = 1 (+1 SD)"))

plot

如何在不更改底层数据的情况下更改 plot_model 中的 facets 顺序?<!-- -->

<sup>创建于2023-06-22,使用 reprex v2.0.2</sup>

英文:

You can do this by making the facet variable in the data a factor with the levels in the order you want.

library(dplyr)
library(sjPlot)

#specifying the model
model = lm(mass ~ gender*species*height, data = starwars[starwars$species == &quot;Human&quot; | starwars$species == &quot;Droid&quot; | starwars$species == &quot;Twi&#39;lek&quot;,])

#plotting predictions at three specified levels of height, let&#39;s pretend they&#39;re -1 SD, mean and +1 #SD
plot = plot_model(model, type = &quot;pred&quot;, terms = c(&quot;species&quot;, &quot;gender&quot;, &quot;height [-2, -1, 1]&quot;), ci.lvl=0.95) 
#&gt; Warning in predict.lm(model, newdata = data_grid, type = &quot;response&quot;, se.fit =
#&gt; se, : prediction from rank-deficient fit; attr(*, &quot;non-estim&quot;) has doubtful
#&gt; cases

plot$data$facet &lt;- factor(case_when(
  plot$data$facet == &quot;height = -2&quot; ~ &quot;height = -2 (-1 SD)&quot;, 
  plot$data$facet == &quot;height = -1&quot; ~ &quot;height = -1 (mean)&quot;, 
  plot$data$facet == &quot;height = 1&quot; ~ &quot;height = 1 (+1 SD)&quot;, 
), levels=c(&quot;height = -2 (-1 SD)&quot;, &quot;height = -1 (mean)&quot;, &quot;height = 1 (+1 SD)&quot;))

plot

如何在不更改底层数据的情况下更改 plot_model 中的 facets 顺序?<!-- -->

<sup>Created on 2023-06-22 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月22日 20:21:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定