Shaun
Shaun

Reputation: 959

Bootstrap in R - how do I extract the numbers in table/output

How do I extract a simple set of numbers from this output. Here is some dummy code for bootstrap.

df <- mtcars

library(boot)

meanfun <- function(data, i){
  d <- data[i, ]
  return(mean(d))   
}

set.seed(1)
bs_mean <- boot( mtcars[, "qsec", drop = FALSE], statistic=meanfun, R=5000)

Then I run this to get this output

    > bs_mean

ORDINARY NONPARAMETRIC BOOTSTRAP


Call:
boot(data = mtcars[, "qsec", drop = FALSE], statistic = meanfun, 
    R = 5000)


Bootstrap Statistics :
    original       bias    std. error
t1* 17.84875 -0.002087562   0.3145629

There is the data I need in the bottom table under "Bootstrap Statistics". All I want to do is extract that bit of data, the "original", "bias" and "std. error". I've tried to find it but no success. I know can do this but it only gets me the original.

> bs_mean$t0
[1] 17.84875

Surely there is a way to extract that table of numbers? I've tried attribute() to get at the numbers. I've had many difficulties with R getting these simple table out of analyses. Someone mentioned attribute() helps, but is there any easier way to find and extract the data within an output?. Any help is greatly appreciated. Many thanks

Upvotes: 0

Views: 883

Answers (1)

AndrewGB
AndrewGB

Reputation: 16856

The easiest way to just use summary on the boot object. Here, I used the same data from above with set.seed(1).

summary(bs_mean)

Output

     R original    bootBias bootSE bootMed
1 5000   17.849 -0.00063844 0.3084  17.843

Original Output

ORDINARY NONPARAMETRIC BOOTSTRAP


Call:
boot(data = mtcars[, "qsec", drop = FALSE], statistic = meanfun, 
    R = 5000)


Bootstrap Statistics :
    original        bias    std. error
t1* 17.84875 -0.0006384375    0.308402

If you want just the 3 columns, then we can select those columns and rename them.

library(tidyverse)

summary(bs_mean) %>% 
  dplyr::select(original, bias = bootBias, `std. error` = bootSE)

Output

  original        bias std. error
1   17.849 -0.00063844     0.3084

Upvotes: 2

Related Questions