簡體   English   中英

如何使用 R 中的 bootstrap 方法計算 beta 回歸擬合值的置信區間

[英]How to calculate confidence intervals for fitted values of beta regression using the bootstrap method in R

我正在嘗試在 R 中引導我的betareg模型的擬合值。我已經閱讀了許多其他問題和網站,例如https://stats.stackexchange.com/questions/234254/confidence-intervals-for-beta-regression/234256# 234256如何在 R 中引導 Beta 回歸模型的預測和置信水平https: //stats.stackexchange.com/questions/86432/how-do-i-predict-with-standard-errors-using-betareg-package -in-r 但是,這些都沒有為我提供可以用於我的數據的答案。 顯然,計算betareg模型擬合值的 CI 並不像我想象的那么簡單。 我知道我可以使用boot包中的bootboot.ci函數,但我真的不明白我應該如何編寫statistics函數以及如何將其合並到boot.ci函數中。 此外,我已經試過confint從功能betaboost包,但只給出了平均,在這里我想找到我的擬合值的CI的95%CI,這樣我就可以與模型一起繪制的CI。 我希望有人能告訴我如何使用 bootstrap 方法來找到擬合值的 95% CI。 非常感謝幫助!

我正在研究 X 對 Y 的影響,兩個比例。 數據+模型看起來像這樣。

圖形

我的 R 腳本

library(dplyr)
library(ggplot2)
library(betareg)
rm(list = ls())

df <- data.frame(propX = c(0.7, 0.671, 0.6795, 0.79, 0.62, 0.62, 0.6413, 0.089, 0.4603, 0.04, 0.0418, 0.46, 0.5995, 0.532, 0.65, 0.6545, 0.74, 0.74, 0.02, 0.02, 0, 0, 0, 0.45, 0.8975, 0.92, 0.898, 0.89, 0.86, 0.69, 0.755, 0.775, 0.585, 0.585, 0.55),
                 propY = c(0.666666666666667, 0.40343347639485, 0.7, 0, 0, 0.0454545454545455, 0.25, 0.707070707070707, 0.629213483146067, 0.882352941176471, 0.942857142857143, 0.451612903225806, 0.0350877192982456, 0.5, 0.484375, 0, 0.0208333333333333, 0.240740740740741, 0.804568527918782, 0.666666666666667, 1, 1, 1, 0.552238805970149, 0.2, 0, 0, 0, 0, 0, 0.12972972972973, 0.0894117647058824, 0.576158940397351, 0, 0),
                 pointWeight = c(3,233,10,89,4,22,44,99,89,17,35,341,57,36,128,39,144,54,394,12,46,229,55,67,5,28,2,160,124,294,555,425,302,116,48))

df$propY <- (((df$propY*(length(df$propY)-1))+0.5)/length(df$propY)) # Transform the data so all data is (0,1)
mybetareg <- betareg(propY ~ propX, data = df, weights = pointWeight, link = "logit")
minoc <- min(df$propX)
maxoc <- max(df$propX)
new.x <- expand.grid(propX = seq(minoc, maxoc, length.out = 1000))
new.y <- predict(mybetareg, newdata = new.x)

# I would like to calculate 95% CI for new.y using the bootstrap method

new.y <- data.frame(new.y)
addThese <- data.frame(new.x, new.y)
addThese <- rename(addThese, propY = new.y)
ggplot(df, aes(x = propX, y = propY)) +
  geom_point(aes(size = pointWeight)) +
  geom_smooth(data = addThese, stat = 'identity') + # here I could then add aes(ymin = lwr, ymax = upr)
  scale_x_continuous(breaks = seq(0,1,0.2), limits = c(0,1)) +
  scale_y_continuous(breaks = seq(0,1,0.2), limits = c(0,1)) +
  theme_bw()

經過反復試驗,我開始使用不同的方法來分析比例數據,即帶有betar系列( mgcv包)的gamgam包)。 這產生與betareg完全相同的結果,但它提供了更多選項,例如隨機效應和標准誤差。 分析后,我預測擬合值及其 SE,從中計算 95% 置信區間。 以下腳本應生成具有置信區間的圖形,只需填寫您的變量和數據集。

mygam = gam(y ~ x, family=betar(link="logit"), data = df, weights = pointWeight)
min <- min(df$x)
max <- max(df$x)
new.x <- expand.grid(x = seq(min, max, length.out = 1000))
new.y <- predict(mygam, newdata = new.x, se.fit = TRUE, type="response")
new.y <- data.frame(new.y)
addThese <- data.frame(new.x, new.y)
addThese <- rename(addThese, y = fit, SE = se.fit)
addThese <- mutate(addThese, lwr = y - 1.96 * SE, upr = y + 1.96 * SE) # calculating the 95% confidence interval
ggplot(df, aes(x = x, y = y)) +
  geom_point(aes(size = pointWeight)) +
  geom_smooth(data = addThese, aes(ymin = lwr, ymax = upr), stat = 'identity')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM