Ashley
Ashley

Reputation: 133

Write bootstrap output to a file

I'm new to R and am trying to do some bootstrap estimates of standard errors for a large file of frequency data. I have the bootstrap working fine on a single data point, but I can't figure out how to save the output. Ideally, I would like to write only the standard error to a new file.

Here is what I've tried so far:

x = c(1,2,3,4,5,6,7,8,9,10)
samplemean = function(x, d){return(mean(x[d]))}
b = boot(x, samplemean, R=1000)
b
ORDINARY NONPARAMETRIC BOOTSTRAP

Call:
boot(data = x, statistic = samplemean, R = 1000)

Bootstrap Statistics :
    original  bias    std. error
t1*      5.5 -0.0356   0.9145759

Upvotes: 3

Views: 1886

Answers (2)

IRTFM
IRTFM

Reputation: 263451

The boot:::print.boot function uses this expression to calculate the "std. error" that it reports for the ordinary bootstrap:

sqrt(apply(t, 2L, function(t.st) var(t.st[!is.na(t.st)])))

If you want to write it to a file, then you can pass b$t to it and use cat:

cat( sqrt(apply(b$t, 2L, function(t.st) var(t.st[!is.na(t.st)]))), file="seboot.txt")

(There had been some earlier processing in the function along these lines that take out the NA values):

t <- matrix(boot.out$t[, index], nrow = nrow(boot.out$t))
allNA <- apply(t, 2L, function(t) all(is.na(t)))
t <- matrix(t[, !allNA], nrow = nrow(t))

Upvotes: 1

dickoa
dickoa

Reputation: 18437

You can compute the standard errors using the t (replication) slot in your boot object.

require(boot)

x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

samplemean <- function(x, d) {return(mean(x[d]))}

set.seed(123)
b <- boot(x,samplemean,R=1000)
b
## Bootstrap Statistics :
##     original  bias    std. error
## t1*      5.5 -0.0232     0.90887

## str(b) first...
apply(b$t, 2, sd)
##[1] 0.90887

Upvotes: 2

Related Questions