繁体   English   中英

无法使用 ggplot 绘制置信区间,(geom_ribbon() 参数)

[英]Unable to plot confidence intervals using ggplot, (geom_ribbon() argument)

我试图在一些模拟值上绘制 95% 的置信区间,但是当我尝试使用 geom_ribbon() 参数绘制 CI 时遇到了这样的问题。 我遇到的麻烦是,当我绘制它们时,我的模型没有显示 CI,就像这样; 在此处输入图像描述

如果有人知道我在这里出错的地方,我已经在下面包含了我的所有代码;

set.seed(20220520)  
#simulating 200 values between 0 and 1 from a uniform distribution
x = runif(200, min = 0, max = 1) 

lam = exp(0.3+5*x)

y = rpois(200, lambda = lam)

#before we do this each Yi may contain zeros so we need to add a small constant
y <- y + .1 
#combining x and y into a dataframe so we can plot
df = data.frame(x, y)

#fitting a Poisson GLM
model2 <- glm(y ~ x, 
          data = df,
          family = poisson(link='log'))

#make predictions (this may be the same as predictions_mod2)
preds <- predict(model2, type = "response")

#making CI predictions
predictions_mod2 = predict(model2, df, se.fit = TRUE, type = 'response')

#calculate confidence intervals limit
upper_mod2 = predictions_mod2$fit+1.96*predictions_mod2$se.fit 
lower_mod2 = predictions_mod2$fit-1.96*predictions_mod2$se.fit

#transform the CI limit to get one at the level of the mean
upper_mod2 = exp(upper_mod2)/(1+exp(upper_mod2)) 
lower_mod2 = exp(lower_mod2)/(1+exp(lower_mod2))

#combining into a df
predframe = data.frame(lwr=lower_mod2,upr=upper_mod2, x = df$x, y = df$y)

#plot model with 95% confidence intervals using ggplot
ggplot(df, aes(x, y)) +
  geom_ribbon(data = predframe, aes(ymin=lwr, ymax=upr), alpha = 0.4) +
  geom_point() +
  geom_line(aes(x, preds2), col = 'blue')

在对该问题的评论中,有人问为什么不对预测值进行 logit 转换。 原因是要求的预测类型是"response" 文档中,我强调。

类型
所需的预测类型。 默认值在线性预测变量的范围内; 另一种“响应”在响应变量的范围内 因此,对于默认二项式模型,默认预测是对数赔率(logit 标度上的概率),type = "response" 给出预测概率。 "terms" 选项返回一个矩阵,给出模型公式中每个项在线性预测尺度上的拟合值。

有一个很好的回答方法,显示代码。

library(ggplot2, quietly = TRUE)

set.seed(20220520)  
#simulating 200 values between 0 and 1 from a uniform distribution
x = runif(200, min = 0, max = 1) 

lam = exp(0.3+5*x)

y = rpois(200, lambda = lam)

#before we do this each Yi may contain zeros so we need to add a small constant
y <- y + 0.1 
#combining x and y into a dataframe so we can plot
df = data.frame(x, y)

#fitting a Poisson GLM
suppressWarnings(
  model2 <- glm(y ~ x, 
                data = df,
                family = poisson(link='log'))
)
#make predictions (this may be the same as predictions_mod2)
preds <- predict(model2, type = "response")

#making CI predictions
predictions_mod2 = predict(model2, df, se.fit = TRUE, type = 'response')

#calculate confidence intervals limit
upper_mod2 = predictions_mod2$fit+1.96*predictions_mod2$se.fit 
lower_mod2 = predictions_mod2$fit-1.96*predictions_mod2$se.fit

#combining into a df
predframe = data.frame(lwr=lower_mod2,upr=upper_mod2, x = df$x, y = df$y)

#plot model with 95% confidence intervals using ggplot
ggplot(df, aes(x, y)) +
  geom_ribbon(data = predframe, aes(ymin=lwr, ymax=upr), alpha = 0.4) +
  geom_point() +
  geom_line(aes(x, preds), col = 'blue')

reprex 包于 2022-05-29 创建 (v2.0.1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM