如何为R中的分类数据生成自举的置信区间?

问题描述

我正在为分类数据中的R中的正态分布数据建立简单的95%引导置信区间。常规Bootstrap Confidence Intervals in R boot.ci似乎不适用于分类变量

df <- data.frame(
  dose = rep(c("10","20","30","40","50"),times= 10),count = rnorm(50,1,0.1)
)    

df$dose <- as.factor(df$dose)

解决方法

df <- data.frame(
  dose = rep(c(10,20,30,40,50),times= 10)
)    
df$count <- rpois(50,df$dose)


df$dose <- factor(df$dose)
#I would consider if I can keep dose as numeric


fit <- glm(count ~ dose,data = df,family = "poisson")
summary(fit)

#bootstrap predictions
library(boot)
set.seed(42)
myboot <- boot(df,function(df,i) {
  fit <- glm(count ~ dose,data = df[i,])
  predict(fit,newdata = data.frame(dose = unique(df$dose)),type = "response")
},1e3)

lapply(seq_along(myboot$t0),function(i) boot.ci(myboot,index = i))
# [[1]]
# BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
# Based on 1000 bootstrap replicates
# 
# CALL : 
#   boot.ci(boot.out = myboot,index = i)
# 
# Intervals : 
#   Level      Normal              Basic         
# 95%   ( 9.57,13.93 )   ( 9.40,13.90 )  
# 
# Level     Percentile            BCa          
# 95%   ( 9.50,14.00 )   ( 9.54,14.00 )  
# Calculations and Intervals on Original Scale
#
# ...