Bootstrap code reporting no error or standard error

I wrote this code in R:

library(boot)

bs <- function(formula, data, indices) {
  d <- data[indices,] # allows boot to select sample 
  fit <- lm(formula, data=d)
 return(coef(fit)) 
}
results <- boot(data=z, statistic=bs, 
 R=1000, formula=z[,1]~z[,2])

      

I am trying to make a random x-sample using a dataframe for data that contains my response and my predictor, however my results are returned without bias and without std.

Bootstrap Statistics :
      original  bias    std. error
t1* 83.5466254       0           0
t2* -0.6360426       0           0

      

Can anyone spot the problem?

+3


source to share


1 answer


Your formula is wrong. When you use z[,1]~z[,2]

, you are literally specifying a formula that has the first column z

as the answer and the second column z

as the independent variables. Note that it z

never changes. This is the parameter data=

that changes. Also, the formula syntax does not work with such positional indices. You need to use variable names. Here are some sample data

z <- data.frame(a=runif(50), b=runif(50))

      

Note how this doesn't work.

results <- boot(data=z, statistic=bs, 
 R=10, formula=z[,1]~z[,2])
results

# Bootstrap Statistics :
#       original  bias    std. error
# t1* 0.45221233       0           0
# t2* 0.08818014       0           0

      

it just reconfigures the same values ​​over and over, the same as when using the fill dataset



lm(a~b, z)

# Coefficients:
# (Intercept)            b  
#     0.45221      0.08818  

      

Do you want to

results <- boot(data=z, statistic=bs, 
 R=10, formula=a~b)
results

# Bootstrap Statistics :
#       original      bias    std. error
# t1* 0.45221233  0.01024794  0.08853861
# t2* 0.08818014 -0.01546608  0.16376128

      

This allows the function to boot

traverse on a different dataset each time, and since the vector values ​​of the vector are not included in the formula that specifically references z

data.frame, you will get updated values.

+2


source







All Articles