簡體   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