简体   繁体   English

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

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

I am trying to plot 95% confidence intervals on some simulated values but am running into so issues when i am trying to plot the CIs using the geom_ribbon() argument.我试图在一些模拟值上绘制 95% 的置信区间,但是当我尝试使用 geom_ribbon() 参数绘制 CI 时遇到了这样的问题。 The trouble I'm having it that my model does not show the CIs when i plot them, like so;我遇到的麻烦是,当我绘制它们时,我的模型没有显示 CI,就像这样; 在此处输入图像描述

I have included all of my code below if anyone knows where i have gone wrong here;如果有人知道我在这里出错的地方,我已经在下面包含了我的所有代码;

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')

In a comment to the question, it's asked why not to logit transform the predicted values.在对该问题的评论中,有人问为什么不对预测值进行 logit 转换。 The reason why is that the type of prediction asked for is "response" .原因是要求的预测类型是"response" From the documentation , my emphasis.文档中,我强调。

type类型
the type of prediction required.所需的预测类型。 The default is on the scale of the linear predictors;默认值在线性预测变量的范围内; the alternative "response" is on the scale of the response variable .另一种“响应”在响应变量的范围内 Thus for a default binomial model the default predictions are of log-odds (probabilities on logit scale) and type = "response" gives the predicted probabilities.因此,对于默认二项式模型,默认预测是对数赔率(logit 标度上的概率),type = "response" 给出预测概率。 The "terms" option returns a matrix giving the fitted values of each term in the model formula on the linear predictor scale. "terms" 选项返回一个矩阵,给出模型公式中每个项在线性预测尺度上的拟合值。

There is a good way to answer, to show the code.有一个很好的回答方法,显示代码。

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')

Created on 2022-05-29 by the reprex package (v2.0.1)reprex 包于 2022-05-29 创建 (v2.0.1)

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

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