简体   繁体   English

如何使用 R 中的 bootstrap 方法计算 beta 回归拟合值的置信区间

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

I am trying to bootstrap fitted values of my betareg model in R. I have read many other questions and websites such as https://stats.stackexchange.com/questions/234254/confidence-intervals-for-beta-regression/234256#234256 , How to Bootstrap Predictions and Levels of Confidence for Beta Regression Model in R , https://stats.stackexchange.com/questions/86432/how-do-i-predict-with-standard-errors-using-betareg-package-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 However, none of these have provided me with an answer I could use for my data.但是,这些都没有为我提供可以用于我的数据的答案。 Apparently, calculating the CI for the fitted values of a betareg model is not as straight forward as I thought it would be.显然,计算betareg模型拟合值的 CI 并不像我想象的那么简单。 I understand that I can use the boot and boot.ci functions from the boot package, but I don't really understand how I should write the statistics function and how to incorporate it in the boot.ci function.我知道我可以使用boot包中的bootboot.ci函数,但我真的不明白我应该如何编写statistics函数以及如何将其合并到boot.ci函数中。 Additionally, I have tried the confint function from the betaboost package, but that only gives the 95% CI of the mean, where I am trying to find the CI for my fitted values, so that I can plot the CI together with the model.此外,我已经试过confint从功能betaboost包,但只给出了平均,在这里我想找到我的拟合值的CI的95%CI,这样我就可以与模型一起绘制的CI。 I'm hoping somebody could show me how to use the bootstrap method to find the 95% CI of the fitted values.我希望有人能告诉我如何使用 bootstrap 方法来找到拟合值的 95% CI。 Help is much appreciated!非常感谢帮助!

I am investigating the influence of X on Y, both proportions.我正在研究 X 对 Y 的影响,两个比例。 The data + model looks like this.数据+模型看起来像这样。

图形

My R script我的 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()

After some trial and error I started working with a different approach to analyse proportional data, namely gam ( gam package) with the betar family ( mgcv package).经过反复试验,我开始使用不同的方法来分析比例数据,即带有betar系列( mgcv包)的gamgam包)。 This yields exactly the same results as betareg , but it offers more options, such as random effects and standard errors.这产生与betareg完全相同的结果,但它提供了更多选项,例如随机效应和标准误差。 After the analysis I predict the fitted values and their SE, from which I calculate the 95% confidence interval.分析后,我预测拟合值及其 SE,从中计算 95% 置信区间。 The following script should produce a graph with a confidence interval, just fill in your variables and dataset.以下脚本应生成具有置信区间的图形,只需填写您的变量和数据集。

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.

相关问题 使用 predictNLS 围绕 R 中的拟合值创建置信区间? - Using predictNLS to create confidence intervals around fitted values in R? 为时间序列回归中的拟合值差异生成渐近置信区间 - Generate asymptotic confidence intervals for difference of fitted values in time series regression 如何引导线性回归并估计 R 中的置信区间? - How to bootstrap a linear regression and estimate confidence intervals in R? 如何引导R中Beta回归模型的预测和置信度 - How to Bootstrap Predictions and Levels of Confidence for Beta Regression Model in R 如何计算R中的滚动自举值和置信区间 - How to calculate rolling bootstrapped values and confidence intervals in R 如何在R中使用线性回归和置信区间? - How to work with linear regression and confidence intervals in R? 线性回归:用拟合参数的标准误差相关系数计算置信区间和预测区间 - Linear regression: calculate confidence and prediction intervals with the standard errors of the fitted parameters the correlation coefficient 如何在 R 中使用置信区间格式化 beta0 和 beta1? - How to format beta0 and beta1 with confidence intervals in R? R 中的 Bootstrap 置信区间 - Bootstrap Confidence Intervals in R 如何为稳健的回归模型计算拟合值 - How to calculate fitted values for robust regression models
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM