use*_*166 0 plot regression r data-visualization ggplot2
我正在学习如何将多级建模应用于传统的方差分析研究设计。我想使用 ggplot2 为每个处理绘制拟合回归线。我想根据我拟合的模型绘制回归线,而不是让 ggplot2 绘制,因为我想看看估计如何根据模型的变化而不同。我知道我可以自己计算系数和斜率,但由于模型相对复杂,我正在寻找更容易绘图的方法。
这是我正在处理的研究设计类型的示例代码。我发现 sjPlot 包 ( http://www.strengejacke.de/sjPlot/sjp.lm/ ) 提供了非常漂亮的图,它显示了每个测试时间的每个处理的回归线和散点图上的实际观察。这正是我想用 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)
Run Code Online (Sandbox Code Playgroud)
或者,像这样:
sjp.lm(fit, type = "pred",
vars = c("test_timing", "treatment"),facet.grid = T)
Run Code Online (Sandbox Code Playgroud)
如果您能教我如何使用 ggplot2 包制作与此图像类似的图,我将不胜感激。谢谢!
事实上,sjPlot只是从ggeffects调用一个函数。ggeffects再次只返回一个带有边际效应/预测值的数据框。所以你可以使用这个数据框来构建你自己的 ggplot 对象,或者简单地使用plot()- 函数来创建一个绘图(一个 ggplot 对象)。
有一个包小插图描述了如何使用 ggplot 构建自己的图:https ://cran.r-project.org/web/packages/ggeffects/vignettes/marginaleffects.html
此外,帮助文件(请参阅https://cran.r-project.org/web/packages/ggeffects/ggeffects.pdf)充满了有关如何使用 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
Run Code Online (Sandbox Code Playgroud)
# note that predictors are categorical, so no straight line
ggplot(me, aes(x = x, y = predicted, colour = group)) +
geom_line()
Run Code Online (Sandbox Code Playgroud)
# plot dots
ggplot(me, aes(x = x, y = predicted, colour = group)) +
geom_point()
Run Code Online (Sandbox Code Playgroud)
# 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))
Run Code Online (Sandbox Code Playgroud)
# 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)
Run Code Online (Sandbox Code Playgroud)
# the simple way
plot(me)
Run Code Online (Sandbox Code Playgroud)
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))
Run Code Online (Sandbox Code Playgroud)
或者,也是简单的方法:
plot(me, rawdata = TRUE)
Run Code Online (Sandbox Code Playgroud)
另请参阅?plot.ggeffects不同的绘图选项,例如,用于jitter = FALSE消除原始数据点的抖动等...