在R中处理方差分析类型的研究设计时,我如何绘制带有观察值的拟合模型?

在R中处理方差分析类型的研究设计时,我如何绘制带有观察值的拟合模型?,r,plot,ggplot2,regression,data-visualization,R,Plot,Ggplot2,Regression,Data Visualization,我正在学习如何将多层次建模应用于传统的方差分析研究设计。 我想使用ggplot2为每个治疗绘制拟合回归线。我想根据我拟合的模型绘制回归线,而不是让ggplot2来绘制,因为我想看看根据不断变化的模型,估计值是如何不同的。我知道我可以自己计算系数和斜率,但由于模型相对复杂,我正在寻找更容易绘制的方法 这是我正在处理的研究设计类型的示例代码。 我发现sjPlot包()提供了非常好的图,它显示了每个测试时间内每个处理的回归线以及散点图上的实际观察值。这正是我想用ggplot制作的 require(t

我正在学习如何将多层次建模应用于传统的方差分析研究设计。 我想使用ggplot2为每个治疗绘制拟合回归线。我想根据我拟合的模型绘制回归线,而不是让ggplot2来绘制,因为我想看看根据不断变化的模型,估计值是如何不同的。我知道我可以自己计算系数和斜率,但由于模型相对复杂,我正在寻找更容易绘制的方法

这是我正在处理的研究设计类型的示例代码。 我发现sjPlot包()提供了非常好的图,它显示了每个测试时间内每个处理的回归线以及散点图上的实际观察值。这正是我想用ggplot制作的

 require(tidyverse)
 require(sjPlot)
 set.seed (100)
 dat <- data_frame(
    participant_id = c(c(1:15), c(1:15)),
    treatment = c(sample (letters [1:3], 15, replace = T), sample (letters [1:3], 15, replace = T)),
    test_timing = c(sample(letters [1:3], 15, replace = T),sample(letters [1:3], 15, replace = T)),
    learning_gain = (runif(30, min = 1, max = 20))
)

fit <- lm (learning_gain ~ treatment * test_timing -1, data = dat)
sjp.lm(fit, type = "pred", 
vars = c("test_timing", "treatment"),facet.grid = F)


如果您能教我如何使用ggplot2软件包制作类似于此图像的绘图,我将不胜感激。谢谢大家!

emmeans包非常有用:

pr <- emmeans::emmeans(fit, ~treatment * test_timing)
ggplot(summary(pr), 
       aes(test_timing, emmean, color = treatment, ymin = lower.CL, ymax = upper.CL)) +
  geom_pointrange(position = position_dodge(0.2), size = 1)
pr实际上,sjPlot只是从ggeffects调用一个函数。ggeffects同样只返回一个带有边缘效果/预测值的数据帧。因此,您可以使用此数据帧构建自己的ggplot对象,或者只需使用
plot()
-函数创建打印(ggplot对象)

有一个包vignette,描述了如何使用ggplot构建自己的绘图:

此外,帮助文件(请参阅)中有很多关于如何使用ggplot创建绘图的示例。也许这对你有帮助

以下是您的具体案例的示例,但请注意,您不会看到直线,因为您的变量是分类的:

require(tidyverse)
require(ggeffects)
set.seed (100)
dat <- data_frame(
  participant_id = c(c(1:15), c(1:15)),
  treatment = c(sample (letters [1:3], 15, replace = T), sample (letters [1:3], 15, replace = T)),
  test_timing = c(sample(letters [1:3], 15, replace = T),sample(letters [1:3], 15, replace = T)),
  learning_gain = (runif(30, min = 1, max = 20))
)

# fit model
fit <- lm (learning_gain ~ treatment * test_timing -1, data = dat)

# compute marginal effects
me <- ggpredict(fit, c("test_timing", "treatment"))

# see results
me

# A tibble: 9 x 5
      x predicted conf.low conf.high group
  <dbl>     <dbl>    <dbl>     <dbl> <fct>
1  1.00      9.81   -0.472      20.1 a    
2  1.00      9.29    4.15       14.4 b    
3  1.00     13.2     5.95       20.5 c    
4  2.00     14.1     8.93       19.2 a    
5  2.00     11.6     7.00       16.2 b    
6  2.00      7.23   -3.06       17.5 c    
7  3.00     13.7     7.77       19.6 a    
8  3.00     13.5     8.33       18.6 b    
9  3.00     10.9     6.66       15.1 c    

点图

带误差条的点图

plot()-来自ggeffects包的函数

原始观察
ggpredict()
将原始数据作为属性返回给返回值,因此您也可以绘制原始观测值:

raw <- attr(me, "rawdata")

ggplot(me, aes(x = x, y = predicted, colour = group)) +
  geom_point(position = position_dodge(.2)) +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0, position = position_dodge(.2)) +
  # using jitter for observations here
  geom_jitter(data = raw, mapping = aes(x = x, y = response, colour = group))


另请参见
?plot.ggeffects
,了解不同的绘图选项,例如,使用
jitter=FALSE
删除原始数据点的抖动等。

仅从该绘图来看,我猜
sjPlot
包已经使用了ggplot。应该可以使用标准的ggplot函数(如
theme()
)修改生成的绘图,如果您查看
sjp.lm
的代码,您将了解绘图是如何组合在一起的。感谢您的快速响应,@Marius!好主意!我检查了sjPlot源代码,发现
sjPlot
使用了
ggeffect
包。虽然我检查了
ggeffect
(即
ggpredict()
)的源代码,但我无法准确理解如何复制,因为我是一名初学者。如果我能听到我是如何描绘这一点的,我将不胜感激,特别是对于这个传统的治疗效果研究设计。谢谢。其他人可能会给出一个完整的答案,但对于这样的方差分析,步骤基本上是a)生成一个数据帧,包含预测因子
治疗
测试时间
b)使用
预测()
使用已安装的型号和新的数据框c)使用ggplot绘制数据框。感谢您告诉我该过程。好的,在等待有人提供一种全面的方法来绘制此图的同时,我将开始学习如何使用
predict()
函数。再次感谢。:)我觉得你的模型很奇怪,为什么它是从b到c再到a的线性关系??它们是类别,如图所示。非常感谢您的建议,@Axeman。这是一个非常巧妙的情节!看起来用这种方法绘制的CI比用sjPlot绘制的CI宽得多。你对此有什么想法吗?谢谢你的回复。你说得对,我编的样本数据不好。我所说的分类测试时间安排是指“预测试”、“即时后测试”和“延迟后测试”。不知何故,sjPlot在最后绘制了测试计时“a”,这让人更加困惑。我仍然不明白为什么它们会线性相关,而不管顺序如何。这对我来说是个合理的情节。还有用于与连续变量交互的
emmeans::emtrends
。您可以添加额外的
geom
函数,这些函数具有
data=dat
。例如,
+geom_point(data=dat)
。非常感谢,@Daniel!这是非常整洁的!!这些图表非常漂亮,清楚地显示了数据的特征。我还感谢您提供了示例代码,这清楚地揭示了
ggpredict()
的功能。我也会检查链接。如果你不介意我问的话,我还想知道我们是否可以在这些图上画出实际观察结果。非常感谢。我添加了两个例子来展示如何绘制原始观测值。这正是我一直在寻找的!!!非常感谢你,丹尼尔。我对你感激不尽!!我希望你今天过得愉快!
# note that predictors are categorical, so no straight line
ggplot(me, aes(x = x, y = predicted, colour = group)) +
  geom_line()
# plot dots
ggplot(me, aes(x = x, y = predicted, colour = group)) +
  geom_point()
# add CI, need jitter
ggplot(me, aes(x = x, y = predicted, colour = group)) +
  geom_point(position = position_dodge(.2)) +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0, position = position_dodge(.2))
# facets
ggplot(me, aes(x = x, y = predicted, colour = group)) +
  geom_point(position = position_dodge(.2)) +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0, position = position_dodge(.2)) +
  facet_grid(~group)
# the simple way
plot(me)
raw <- attr(me, "rawdata")

ggplot(me, aes(x = x, y = predicted, colour = group)) +
  geom_point(position = position_dodge(.2)) +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0, position = position_dodge(.2)) +
  # using jitter for observations here
  geom_jitter(data = raw, mapping = aes(x = x, y = response, colour = group))
plot(me, rawdata = TRUE)