【问题标题】:How to plot multiple prediction estimates with prediction intervals (linear regression)如何使用预测区间绘制多个预测估计值(线性回归)
【发布时间】:2020-11-22 10:16:49
【问题描述】:

我有一个数据框,其中包含两个分类变量(二进制)的预测和预测区间,我想将它们绘制在一个图中。

数据框(df)示例:

  block condition response      fit      lwr      upr
1     1    reward yes        3388.629 2089.910 4687.348
2     2    reward yes        3372.682 2074.191 4671.173
....

选项为:奖励+是,奖励+否,无奖励+是,无奖励+否。

我想获得这种类型的图表,但包括所有选项(我认为是四行):

我尝试更改以下代码但没有成功:

library("ggplot2")
p <- ggplot(df, aes(?, ?)) +
  geom_point() +
  stat_smooth(method = lm)
# 3. Add prediction intervals
p + geom_line(aes(y = lwr), color = "red", linetype = "dashed")+
    geom_line(aes(y = upr), color = "red", linetype = "dashed")

我想使用 ggplot 但我不知道如何将所有 4 行都放在那里。任何帮助都会很棒!

【问题讨论】:

  • 如果我理解正确的话,从你的图中,你需要信心和预测带。您的参数和预测带的置信度为您的预测。这是正确的吗?
  • 显示的数据框中没有您的数据。你需要那些来绘制点
  • 不,我只想显示预测带,而不是置信区间。
  • 我没有包含整个数据框,因为它非常大..

标签: r ggplot2 linear-regression intervals prediction


【解决方案1】:

您在问题中包含的图是两个连续变量相互绘制的图,因此您不能将这种类型的图用于您的数据。你说你有两个分类变量,但实际上有三个:blockconditionresponse

如果您要绘制两个分类变量的上限、下限和拟合值,在 ggplot 中执行此操作的自然方法是使用geom_errorbar,其中一个分类变量位于 x 轴上,另一个由颜色表示这些条在每个 x 轴位置“躲避”,因此它们彼此分开。

对于如何处理不同的块,您有几种选择。如果你有一个小数字,那么你可以把每个块放在它自己的方面。如果您有大量数据,您可能需要聚合数据以平均出块。

您提供的数据框的两行不足以说明其中哪一个更好,所以让我们组成一个类似的数据集。这里我们假设有 4 个区块:

set.seed(1)

df <- data.frame(block     = rep(1:4, 4), 
                 condition = rep(rep(c("reward", "no reward"), each = 4), 2),
                 response  = rep(c("yes", "no"), each = 8),
                 fit       = rep(c(3400, 3200, 3300, 2900), each = 4) + 
                             rnorm(16, 0, 10),
                 lwr       = rep(c(2080, 1900, 2000, 1600), each = 4) + 
                             rnorm(16, 0, 10),
                 upr       = rep(c(4700, 4500, 4600, 4200), each = 4) + 
                             rnorm(16, 0, 10))

df
#>    block condition response      fit      lwr      upr
#> 1      1    reward      yes 3393.735 2079.838 4703.877
#> 2      2    reward      yes 3401.836 2089.438 4699.462
#> 3      3    reward      yes 3391.644 2088.212 4686.229
#> 4      4    reward      yes 3415.953 2085.939 4695.850
#> 5      1 no reward      yes 3203.295 1909.190 4496.057
#> 6      2 no reward      yes 3191.795 1907.821 4499.407
#> 7      3 no reward      yes 3204.874 1900.746 4511.000
#> 8      4 no reward      yes 3207.383 1880.106 4507.632
#> 9      1    reward       no 3305.758 2006.198 4598.355
#> 10     2    reward       no 3296.946 1999.439 4597.466
#> 11     3    reward       no 3315.118 1998.442 4606.970
#> 12     4    reward       no 3303.898 1985.292 4605.567
#> 13     1 no reward       no 2893.788 1595.218 4193.112
#> 14     2 no reward       no 2877.853 1604.179 4192.925
#> 15     3 no reward       no 2911.249 1613.587 4203.646
#> 16     4 no reward       no 2899.551 1598.972 4207.685

因此,我们的列名和顺序与您自己的数据相同,而数字列的值大致相似。

绘制这个的明显方法是 ggplot 是:

library(ggplot2)

ggplot(within(df, block <- paste("Block", block)),
       aes(condition, fit, color = response, group = response)) +
  geom_errorbar(aes(min = lwr, max = upr), size = 1.5, 
                width = 0.25, position = position_dodge()) +
  geom_point(position = position_dodge(width = 0.25), color = "black") +
  facet_wrap(.~block, nrow = 2) +
  theme_bw()

如果你想聚合块,你可以获得一个面板:

library(ggplot2)
library(dplyr)

df %>%
  group_by(condition, response) %>%
  summarise(across(c("fit", "lwr", "upr"), mean)) %>%
  ggplot(aes(condition, fit, color = response, group = response)) +
  geom_errorbar(aes(min = lwr, max = upr), size = 1.5, 
                width = 0.25, position = position_dodge()) +
  geom_point(position = position_dodge(width = 0.25), color = "black") +
  theme_bw()


编辑

另一种方法是在 x 轴和平面上绘制块 condition

ggplot(within(df, block <- paste("Block", block)),
       aes(block, fit, color = response, group = response)) +
  geom_errorbar(aes(min = lwr, max = upr), size = 1.5, 
                width = 0.25, position = position_dodge()) +
  geom_point(position = position_dodge(width = 0.25), color = "black") +
  facet_grid(condition~.) +
  theme_bw()

【讨论】:

  • 非常感谢艾伦,我确实忘记了块变量。有 14 个不同的块,我还想可视化块的效果。是否有可能在不绘制 14 个不同的图的情况下以某种方式做到这一点?
  • 因为如果我使用你建议的最后一个图,我会丢失块效应中的信息,因为它是聚合的。对吗?
  • @Eggplant 是的,正如我在问题末尾显示的那样,您可以将块聚合在一起,尽管这样做会丢失一些信息。另一种选择是按条件分面并将块放在 x 轴上
  • 是的,在 X 轴上放置块将是最好的解决方案!我修改了这样的代码:df %&gt;% group_by(condition, response, block) %&gt;% summarise(across(c("fit", "lwr", "upr"), mean)) %&gt;% ggplot(aes(block, fit, color = condition, group = response)) + geom_errorbar(aes(min = lwr, max = upr), size = 1.5, width = 0.25, position = position_dodge()) + geom_point(position = position_dodge(width = 0.25), color = "black") + theme_bw() 我觉得它看起来很不错! (除非你要说我在代码中做错了什么:'))
猜你喜欢
  • 1970-01-01
  • 2019-01-22
  • 1970-01-01
  • 2018-02-05
  • 1970-01-01
  • 2021-01-19
  • 1970-01-01
  • 2020-06-16
  • 1970-01-01
相关资源
最近更新 更多