簡體   English   中英

如何為 R 中的分類數據生成自舉置信區間?

[英]How to generate bootstrapped confidence intervals for categorical data in R?

我正在嘗試為 R 中的分類數據的正態分布數據構建簡單的 95% 引導置信區間。 R boot.ci中的常規Bootstrap 置信區間似乎不適用於分類變量

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
#
# ...

暫無
暫無

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

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