0

I am trying to build simple 95% bootrapped confidence interval for normally distributed data in R for categorical data. The regular Bootstrap Confidence Intervals in Rboot.ci does not seem to work for categorical variables

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)
Rspacer
  • 2,369
  • 1
  • 14
  • 40
  • 1
    It's not clear what statistic you are trying to find confidence intervals for. – Andrew Gustar Oct 21 '20 at 15:15
  • I'm trying to run general linear models i.e. normally distributions. Does that help? – Rspacer Oct 21 '20 at 15:22
  • Is "count" your dependent variable and "dose" your independent variable? Are you running Poisson regression and want to bootstrap the predicted values? Why has your example a variable `count` that is sampled from a normal distribution? Counts are not normal distributed. – Roland Oct 21 '20 at 15:41
  • I am using this a dummy example. Yes, dose in the independent variable and count dependent. Can you please show me an example with poisson distribution? – Rspacer Oct 21 '20 at 15:45
  • You want to use `dose` to predict `count`? Then shouldn't you need to have the distribution of `dose` to make a bootstrap? – Ricardo Semião e Castro Oct 21 '20 at 17:05

1 Answers1

1
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
#
# ...
Roland
  • 127,288
  • 10
  • 191
  • 288