StudentFlyer
StudentFlyer

Reputation: 33

R is an unused argument in Boot, despite being there?

I am trying to compare different models using the same data. I am running R version 3.6.1 (which seems to have problems of its own— especially in downloading packages). This is what I am trying to do:

bs <- function(formula=model1, data=my_data, R=1000) 
{
 d <- my_data[R=1000,] 
  fit <- lm(formula=model1, data=my_data)
  return(coef(fit)) 
} 

results <- boot(data=my_data, statistic=bs, R=1000, formula=model1)

plot(results, index=1) # intercept 
plot(results, index=2) # wt 
plot(results, index=3) # disp 


boot.ci(results, type="bca", index=1) # intercept 
boot.ci(results, type="bca", index=2) # wt 
boot.ci(results, type="bca", index=3) # disp

EVERY TIME I try to run this code, I get this error:

Error in `[.data.table`(my_data, R = 1000, ) : unused argument (R = 1000)

If I remove the R variable from the last line, I get this error:

Error in index.array(n, R, sim, strata, m, L, weights) : argument "R" is missing, with no default

I'm neither a statistician nor a programmer, just a confused biologist hoping to understand what I'm looking at— how can the argument be unused AND be missing without a default? What am I doing wrong?

Thank you for your help!!

Upvotes: 0

Views: 1576

Answers (1)

StupidWolf
StupidWolf

Reputation: 46908

R is the number of bootstraps and used only when you call boot. The function bs does not need to know which bootstrap it is on. And in your original function, it is not very clear whether you are using the function variables or calling from global.

I show below how bs should be written, and it should work for you:

bs <- function(my_data,inds,formula=model1) 
{
 d <- my_data[inds,] 
  fit <- lm(formula=formula, data=d)
  return(coef(fit)) 
} 

results <- boot(data=mtcars, statistic=bs, R=10, formula=as.formula(mpg~.))
> results

ORDINARY NONPARAMETRIC BOOTSTRAP


Call:
boot(data = mtcars, statistic = bs, R = 10, formula = as.formula(mpg ~ 
    .))


Bootstrap Statistics :
        original        bias    std. error
t1*  12.30337416 -19.268069883 37.18417307
t2*  -0.11144048   0.311423945  1.27584819
t3*   0.01333524   0.012433796  0.01697679
t4*  -0.02148212  -0.006148059  0.02917990
t5*   0.78711097   0.778926590  3.57621690
t6*  -3.71530393  -1.632978225  3.32856394
t7*   0.82104075   0.963507105  1.53123787
t8*   0.31776281  -1.230288420  3.27278936
t9*   2.52022689   0.157071809  3.31151260
t10*  0.65541302   0.084539332  1.83846998
t11* -0.19941925   0.131494452  1.10284335

Upvotes: 1

Related Questions