r - 绘制混合效应模型的预测区间

标签 r ggplot2 prediction modeling lme4

我为我的实验实现了一个混合效应模型,以了解错误率如何影响 react 时间。我现在想计算预测区间,然后绘制它们。

这是我的 df 的示例

    ppid error_rate      RT pNum
1   1_1_4   2.865371 0.43339    1
2  1_1_77  11.459301 0.45000    1
3  1_1_80   2.865371 0.38320    1
4  1_2_26   3.820155 0.49990    1
5  1_2_31   2.865371 0.56680    1
6  1_2_32   3.820155 0.58330    1
7  1_2_33   2.865371 0.50000    1
8  1_2_40   3.820155 0.44980    1
9  1_2_43   2.865371 0.56660    1
10 1_2_54  11.459301 0.46670    1
11 1_2_63   2.865371 0.43350    1
12 1_2_64   2.865371 0.46680    1
13 1_2_71   2.865371 0.54990    1
14 1_2_76   2.865371 0.48350    1
15 1_2_85   2.865371 0.53340    1
16 1_2_88   3.820155 0.43340    1
17 1_2_89   3.820155 0.53320    1
18  1_3_0   3.820155 0.45080    1
19  1_3_1   2.865371 0.45022    1
20 1_3_19   2.865371 0.46651    1

然后,我实现混合效应模型,为每个数据点生成一些预测区间,然后将原始数据与预测相结合:

library(lme4)
library(merTools)
library(ggplot2)

fit <- lmer(formula = RT ~ error_rate + (1 + error_rate | pNum), data = data)

pred <- cbind(data, predictInterval(fit, data))

然后我使用 ggplot 绘制它并得到以下图:

ggplot(pred) + 
  geom_line(aes(x = error_rate, y = fit)) +
  geom_ribbon(aes(x = error_rate, ymin = lwr, ymax = upr), alpha = .2) +
  geom_jitter(aes(x = error_rate, y = RT), alpha = .1) +
  ylab("RT")

image of the resulting ggplot code

我的图对我来说很有意义:黑线表示每个错误率的预测值,阴影区域表示间隔。但是我不确定为什么我的数据点内每个错误率级别的中间都会出现垂直直线?另外,我的水平预测线似乎很奇怪......有人知道为什么会这样,以及如何消除它吗?非常感谢!

最佳答案

使用一条线连接 error_rate 值而不使用垂直线的一种方法是绘制 y 变量的平均值适合。这是通过 stat_summary 完成的,如下所示。

ggplot(pred, aes(x = error_rate, y = fit)) + 
  stat_summary(fun.y = mean, geom = "line", show.legend = FALSE) + 
  geom_ribbon(aes(x = error_rate, ymin = lwr, ymax = upr), alpha = 0.2) +
  geom_jitter(aes(x = error_rate, y = RT), alpha = 0.1) +
  ylab("RT")

enter image description here

注意:在问题代码中,用 alpha = 0.2 绘制功能区,用 alpha = 0.1 绘制点。让这些点比基础预测带更不透明是否更有意义?因此要交换 alpha 值?

关于r - 绘制混合效应模型的预测区间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57591723/

相关文章:

r - 计算两个非零数字之间的零,然后通过 R 中的数字数组继续执行此操作

r - ggplot 与来自数据表 (DT) 的过滤数据和 react 性小部件来选择 x 和 y 轴

r - 在 R 中创建空间数据

r - 可以在R中使用旧版本的 'stats'软件包吗?

r - 在同一图表R上绘制两个图,ggplot2 par(mfrow())

r - 如何通过某些变量折叠数据框,对其他变量取平均值

python - 估计卡尔曼滤波器周围的置信区间

r - SVM 性能与 AUC 分数不一致

python - Python异常: error: [Errno 10054] An existing connection was forcibly closed by the remote host

r - 为 R 中的朴素贝叶斯分类选择特征