1

I have some trouble in calculating a confidence interval with bootstrap method using R. Here is a minimal example that I get stuck.

library(simpleboot) set.seed(123) random <- data.frame(x=runif(10), y=runif(10)) pi <- function(df){4*length(subset(df, x^2 + y^2 < 1)$x)/length(df$x)} pi.boot <- one.boot(random, pi, 1000) # I got an error here 

I got an error which says

Error in [.data.frame(x, idx) : undefined columns selected

Would you help me to find out what is wrong with it? Thank you.

3
  • 2
    Welcome to our site! This question seems to be purely about debugging code, which is off-topic here - see our help center. This site is usually better if your underlying question is inherently statistical, rather than about the code itself. Commented Apr 15, 2016 at 7:21
  • 3
    It seems to me the issue here isn't with your bootstrap but your function pi. Try breaking things down into an absolutely minimal reproducible example to work out which part of pi isn't working. Then if you haven't worked out the error by then, it'll be in a good form for posting on Stack Overflow. On the other hand, asking folk to debug your entire code because you haven't tried finding where the error is can be considered a bit lazy, but moreover it's unhelpful to future readers - if someone does have a issue with bootstraps they don't want to see a Q about subsetting in their search Commented Apr 15, 2016 at 7:29
  • Thank you for your comments. The real problem I am facing has more complicated data and function. So I thought this is simple enough (but meaningful) to reproduce the error. (In case of too simple function, I can avoid this error but such avoidance doesn't work in my case.) Anyway it seems better to ask this in Stack Overflow. Commented Apr 15, 2016 at 8:01

1 Answer 1

1

See if this is what you want:

x<-1:nrow(random) pif <- function(yt,dft){4*length(subset(dft[yt,], x^2 + y^2 < 1)$x)/length(dft[yt,]$x)} pi.boot <- one.boot(x, pif, 1000,dft=random) # no error #print(pi.boot) boot.ci(pi.boot) #hist(pi.boot) BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS Based on 1000 bootstrap replicates CALL : boot.ci(boot.out = pi.boot) Intervals : Level Normal Basic 95% ( 2.202, 4.194 ) ( 2.400, 4.400 ) Level Percentile BCa 95% ( 2.0, 4.0 ) ( 1.6, 3.6 ) Calculations and Intervals on Original Scale Warning : BCa Intervals used Extreme Quantiles Some BCa intervals may be unstable 
Sign up to request clarification or add additional context in comments.

Comments