Jeff S
Jeff S

Reputation: 21

How to retrieve Bootstrap Statistics in the form of a data frame?

When I use boot() for a series of regression coefficients, I get the display of Bootstrap Statistics as shown below. Is there a way of retrieving these Bootstrap Statistics in the form of a data frame?

bs<-function(formula, data, indices) {
  d<-data[indices,]
  fit1<-lm(formula,data=d)
  r<-residuals(fit1)
  taus<-c(0.1, 0.2, 0.3, 0.4, 0.5)
  fit2<-rq(srs~r, tau=taus, data=d)
  return(coef(fit2))

library(boot)
results<-boot(data=earli, statistic=bs, R=1000, formula=Freq~prepreg_bmi+mat_age)             
results
Bootstrap Statistics :
      original      bias    std. error
t1*  39.000000 -0.06213177   0.4923311
t2*   0.000000  0.81613987   0.9480617
t3*  39.994080  0.20734506   0.5515006
t4*   1.978584 -0.27895713   1.0157747
t5*  41.494479  0.08356320   0.6923510
t6*   2.982711 -0.28310189   1.2957767
t7*  43.464062 -0.22307964   0.7879350
t8*   3.075496  0.31197213   1.4150300
t9*  44.974168  0.03526846   0.8638345
t10*  3.978100 -0.53116587   1.5490870

I tried results$t0 but that gave me only the intercepts and coefficients within a data frame.
I would like to get the standard errors within the data frame too.

Upvotes: 2

Views: 55

Answers (2)

jay.sf
jay.sf

Reputation: 73572

If we str the results,

> str(r)
List of 11
 $ t0       : num [1:2] -2.85 16.85
 $ t        : num [1:999, 1:2] -1.923 -4.077 -2.846 -3.615 0.308 
 ...

...

we can see the "original" means as a vector t0, and the bootstrap means as a matrix t with nrow according to number of repetitions.

Applying Rubin's rules―standard deviations of the bootstrap means give the bootstrap SEs―on the columns of t give the standard errors.

> cbind(original=r$t0, bias=r$t0 - colMeans(r$t), se=apply(r$t, 2, sd))
      original        bias       se
[1,] -2.846154 -0.03927004 1.523524
[2,] 16.846154  1.37269962 6.776568

Check:

> r

STRATIFIED BOOTSTRAP


Call:
boot(data = grav1, statistic = diff.means, R = 999, stype = "f", 
    strata = grav1[, 2])


Bootstrap Statistics :
     original      bias    std. error
t1* -2.846154  0.03927004    1.523524
t2* 16.846154 -1.37269962    6.776568

This applies to the standard type. There may be other bootstrap types where calculation is different (not tested). In this case check the source code of boot:::print.boot to see how the calculation is done.


Data:

See second example of ?boot::boot.

Upvotes: 1

Abdur Rohman
Abdur Rohman

Reputation: 2944

If the table of coefficients which contains standard error values is not available in results$..., you can use capture.output() to print theresults and then subset the table from there. But you may need to rename the columns.

Because you don't provide the data, it's difficult to suggest code that suits your results, but probably this works:

out <- capture.output(results)
n <- length(out)

tab <- read.table(text =out[10:n], header = TRUE)

colnames(tab) = c("t", "original", "bias", "std.error")

tab

Upvotes: 0

Related Questions