如何向plot_model添加CI(置信区间)的”whiskers”(线条)和垂直线?

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

How to add CI whiskers and vertical lines to plot_model?

问题

我正在绘制一个具有两个分类变量的模型,每个变量都有两个类别。我试图实现类似以下的效果:

如何向plot_model添加CI(置信区间)的”whiskers”(线条)和垂直线?

而实际上我得到了这样的结果:

如何向plot_model添加CI(置信区间)的”whiskers”(线条)和垂直线?

这是我的代码:

install.packages("sjstats")
library(sjstats)
install.packages("sjPlot")
library(sjPlot)

plot_model(my_model, type = "pred", terms = c("Language", "Switching"),
           axis.title = "Response Times (log)", legend.title = "Condition",
           colors = "Set2", dot.size = 3, line.size = 1.5, ci = TRUE, ci.lvl = 0.95)

抱歉,我无法分享模型或其中的一个小的可复现部分,因此无法使用 dput 进行子集操作,也无法在这里分享整个数据集。

我的问题主要在于我用来显示图形的箱线图和置信区间的参数,但它们似乎由于某种原因不起作用。此外,有关如何获取垂直线的建议吗?vline 参数似乎也无效...

编辑:
在使用neilfws建议的下面代码编辑后,我得到了以下结果:

如何向plot_model添加CI(置信区间)的”whiskers”(线条)和垂直线?

英文:

I'm plotting a model with two categorical variables, each with 2 categories. My attempt is to have something like the below:

如何向plot_model添加CI(置信区间)的”whiskers”(线条)和垂直线?

Instead what I have is this:

如何向plot_model添加CI(置信区间)的”whiskers”(线条)和垂直线?

Here is my code:

install.packages("sjstats")
library(sjstats)
install.packages("sjPlot")
library(sjPlot)

plot_model(my_model, type = "pred", terms = c("Language", "Switching"),
           axis.title = "Response Times (log)", legend.title = "Condition",
           colors = "Set2", dot.size = 3, line.size = 1.5, ci = TRUE, ci.lvl = 0.95)

Sorry for not being able to share the model or a small reproducible part of it, but I'm not able to subset from it using dput and I'm not yet able to share the whole dataset on here.

My issue is more in the arguments that I'm using to show the whiskers of the plot the confidence intervals but they don't seem to work for some reason. Also, any suggestions on how to get the vertical line? vline arguments don't work either...

EDIT:
After editing with the suggested code below by neilfws, this is what I get:

如何向plot_model添加CI(置信区间)的”whiskers”(线条)和垂直线?

答案1

得分: 1

以下是您要翻译的内容:

"plot_model"的输出与给定的参数一样,符合预期。这种类型的模型不会绘制盒须图。

但是,"plot_model"的输出是一个ggplot对象,因此您可以通过添加geoms来修改它。

这是一个简单的示例。如果您想要更适合您的数据的代码,需要共享一些数据

library(sjPlot)
library(ggplot2)
library(dplyr)

fit <- mtcars %>%
  mutate(am = factor(am)) %>%
  glm(vs ~ am, ., family = 'binomial')

plot_model(fit, type = "pred", terms = "am") + 
geom_errorbar(
    aes(ymin = conf.low, ymax = conf.high),
    width = 0.25) + 
geom_line()

结果:

如何向plot_model添加CI(置信区间)的”whiskers”(线条)和垂直线?

英文:

The output from plot_model is as expected with the given arguments. The function does not plot whiskers for this kind of model.

However, the output from plot_model is a ggplot object, so you can modify it by adding geoms.

Here is a simple example. You'll need to share some data if you want code more suited to your data.

library(sjPlot)
library(ggplot2)
library(dplyr)

fit &lt;- mtcars %&gt;% 
  mutate(am = factor(am)) %&gt;% 
  glm(vs ~ am, ., family = &#39;binomial&#39;)

plot_model(fit, type = &quot;pred&quot;, terms = &quot;am&quot;) + 
geom_errorbar(
    aes(ymin = conf.low, ymax = conf.high),
    width = 0.25) + 
geom_line()

Result:

如何向plot_model添加CI(置信区间)的”whiskers”(线条)和垂直线?

huangapple
  • 本文由 发表于 2023年5月15日 06:31:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249949.html
匿名

发表评论

匿名网友

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

确定