2012-08-10 58 views
3

我是R新手,正在嘗試對大量頻率數據文件的標準錯誤進行一些自舉估計。我的引導程序在單個數據點上工作正常,但我無法弄清楚如何保存輸出。理想情況下,我想只將標準錯誤寫入新文件。將引導程序輸出寫入文件

這是我到目前爲止已經試過:

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

你可以計算在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 
+0

謝謝。這工作完美。 – Ashley 2012-09-10 01:35:09

+0

有人可以解釋爲什麼你會得到標準錯誤,雖然你在'apply(b $ t,2,sd)'函數中調用標準偏差嗎? – Mikko 2012-11-23 10:45:48

+0

Bootstrapping可用於模擬難以測量多次的參數的誤差。自舉複製的標準偏差是測量參數的自舉(模擬)標準誤差。 – Ashley 2013-01-05 02:24:31

1

引導::: 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))