将引导输出写入文件

时间:2012-08-10 23:41:27

标签: r statistics-bootstrap

我是R的新手,我正在尝试为大型频率数据文件做一些标准错误的自举估计。我让bootstrap在单个数据点上工作正常,但我无法弄清楚如何保存输出。理想情况下,我只想将标准错误写入新文件。

这是我到目前为止所尝试的内容:

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 个答案:

答案 0 :(得分:2)

您可以使用t对象中的boot(复制)广告位来计算标准错误。

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

答案 1 :(得分:1)

boot ::: print.boot函数使用此表达式计算它为普通引导程序报告的“标准错误”:

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

如果您想将其写入文件,则可以将b$t传递给它并使用cat

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

(沿着这些线的功能中有一些早期的处理取出了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))
相关问题