将引导输出写入文件

时间:2022-02-23 14:02:16

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.

我是R的新手,正在尝试对一个大的频率数据文件做一些引导估计的标准错误。我让引导程序在单个数据点上工作得很好,但是我不知道如何保存输出。理想情况下,我只希望将标准错误写入新文件。

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

2 个解决方案

#1


2  

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

可以使用boot对象中的t(复制)槽计算标准错误。

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

#2


1  

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

引导:::打印。boot函数使用此表达式计算其报告的普通引导程序的“std. error”:

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:

如果你想把它写进一个文件,那么你可以把b$t传递给它并使用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):

(函数中有一些较早的处理,沿着这些线取出NA值):

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))

#1


2  

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

可以使用boot对象中的t(复制)槽计算标准错误。

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

#2


1  

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

引导:::打印。boot函数使用此表达式计算其报告的普通引导程序的“std. error”:

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:

如果你想把它写进一个文件,那么你可以把b$t传递给它并使用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):

(函数中有一些较早的处理,沿着这些线取出NA值):

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))