0

I have a vector of 19 values. I want to calculate confidence intervals for each value using the bootstrap method. I use the following code:

library(boot)
alpha <- 0.9
B <- 1000
p_hat<-s$a     #vector with 19 values
intervals <- matrix(0, ncol = 2, nrow = length(p_hat))
 my_function <- function(data, index) {
   return(median(data[index]))
for (i in 1:length(p_hat)) {
 boot_samples <- boot(p_hat[i], my_function, R = B)
 intervals[i,] <- boot.ci(boot_samples, type = "bca", conf = .9)
 }
}
results <- data.frame(p_hat, intervals)
print(results)

I get 0 instead of confidence intervals


Data

This is the vector posted in comment.

p_hat <- c(0, 0, 6.70881, 14.16335, 26.08988, 41.33073, 57.23204, 
           74.02023, 88.54585, 95.48473, 98.97599, 99.90797, 
           99.94741, 100, 100, 100, 100, 100, 100)
Olesia
  • 1
  • 1
  • it would be easier to help you if you post a reproducible example. try `B <- 4999`. bca confidence intervals fails with low replications. – Eric Feb 25 '23 at 13:51
  • @Eric Thank you. I tried with 4999, but the problem remained. My vector contains the following values : 0.00000 0.00000 6.70881 14.16335 26.08988 41.33073 57.23204 74.02023 88.54585 95.48473 98.97599 99.90797 99.94741 100.00000 100.00000 100.00000 100.00000 100.00000 100.00000 – Olesia Feb 25 '23 at 14:31
  • This doesn't make sense, for each `i` there is only one value in `p_hat[i]` and you cannot bootstrap one number. (I mean, you can but its mean equals that one number). – Rui Barradas Feb 25 '23 at 16:14
  • Also, put the function definition outside the `for` loop, you are recreating it over and over again. – Rui Barradas Feb 25 '23 at 16:18
  • @RuiBarradas it doesn't work for me anyway. Could you please help intervals <- matrix(0, ncol = 2, nrow = length(p_hat)) my_function <- function(data, index) { return(median(data[index])) for (i in 1:length(p_hat)) { boot_samples <- boot(p_hat[i], my_function, R = B) intervals[i,] <- boot.ci(boot_samples, type = "bca", conf = .9) } } – Olesia Feb 25 '23 at 16:45
  • Please don't post code or data as comments, edit the question with that code. And yes, that's what I mean with my second comment. And in my first comment I explained ***the main objection*** to what you are doing. You cannot bootstrap values one by one. You can bootstrap the ***vector*** mean, the the mean value of each point individually. – Rui Barradas Feb 25 '23 at 16:47
  • @RuiBarradas sorry, but I still don't understand how I can build confidence intervals for each point in the vector – Olesia Feb 25 '23 at 16:59
  • You cannot. What is an interval around one point? A confidence interval assumes the existence of variability in the data, measured by the standard error. ***One*** point does not vary, the concept of confidence interval does not apply. (Note: there is a typo in the end of my previous comment, *"the the"* is wrong, it should be *"not the mean value of each point individually."* – Rui Barradas Feb 25 '23 at 17:02

1 Answers1

0

Are you looking for something like this?

library(boot)

p_hat <- c(0, 0, 6.70881, 14.16335, 26.08988, 41.33073, 57.23204, 
           74.02023, 88.54585, 95.48473, 98.97599, 99.90797, 
           99.94741, 100, 100, 100, 100, 100, 100)

boot_mean <- function(x, i) mean(x[i])

alpha <- 0.9
b <- boot(p_hat, boot_mean, R = 5000)
ci <- boot.ci(b, conf = alpha)
#> Warning in boot.ci(b, conf = alpha): bootstrap variances needed for studentized
#> intervals

intervals <- lapply(ci[4:7], \(x) {
  ci <- setNames(tail(x[1,], 2), c("lower", "upper"))
  c(x[1, 1], ci)
})
do.call(rbind, intervals)
#>         conf    lower    upper
#> normal   0.9 53.94825 83.14039
#> basic    0.9 54.25572 83.31273
#> percent  0.9 53.78274 82.83975
#> bca      0.9 52.50008 82.01524

Created on 2023-02-25 with reprex v2.0.2

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66