2013-08-20 19 views
8

Ho questa matrice di valori:R calcolare l'errore standard utilizzando bootstrap

> df 
[1] 2 0 0 2 2 0 0 1 0 1 2 1 0 1 3 0 0 1 1 0 0 0 2 1 2 1 3 1 0 0 0 1 1 2 0 1 3 
[38] 1 0 2 1 1 2 2 1 2 2 2 1 1 1 2 1 0 0 0 0 0 0 0 0 0 0 1 0 1 1 0 1 0 0 0 0 0 
[75] 0 0 0 0 0 1 1 0 1 1 1 1 3 1 3 0 1 2 2 1 2 3 1 0 0 1 

voglio usare scarpone pacchetto per calcolare l'errore standard dei dati. http://www.ats.ucla.edu/stat/r/faq/boot.htm

Così, ho usato questo comando per perseguire:

library(boot) 
boot(df, mean, R=10) 

e ho ottenuto questo errore:

Error in mean.default(data, original, ...) : 
'trim' must be numeric of length one 

Qualcuno può aiutarmi a capire il problema? Grazie

+1

Qual è la sua definizione di funzione per 'C'? La funzione base 'c' non è adatta per il bootstrap. – Frank

risposta

11

Se si bootstrap il mezzo si può fare come segue:

set.seed(1) 
library(boot) 
x<-rnorm(100) 
meanFunc <- function(x,i){mean(x[i])} 
bootMean <- boot(x,meanFunc,100) 
>bootMean 

ORDINARY NONPARAMETRIC BOOTSTRAP 


Call: 
boot(data = x, statistic = meanFunc, R = 100) 


Bootstrap Statistics : 
    original  bias std. error 
t1* 0.1088874 0.002614105 0.07902184 

Se basta inserire l'mean come argomento si otterrà l'errore come quello che hai:

bootMean <- boot(x,mean,100) 
Error in mean.default(data, original, ...) : 
    'trim' must be numeric of length one 
1

La funzione c non è sufficiente per boot. Se guarderai l'aiuto per boot, vedrai che la tua funzione deve essere in grado di ricevere i dati e un indice. Quindi, è necessario scrivere la propria funzione. Inoltre, dovrebbe restituire il valore di cui si desidera l'errore standard, come la media.

3

Non ho mai usato veramente l'avvio, dal momento che non capisco cosa porterà in tavola.

Dato che l'errore standard è definita come:

sd(sampled.df)/sqrt(length(df))

Credo che si può semplicemente utilizzare la seguente funzione per ottenere questo fatto:

custom.boot <- function(times, data=df) { 
    boots <- rep(NA, times) 
    for (i in 1:times) { 
    boots[i] <- sd(sample(data, length(data), replace=TRUE))/sqrt(length(data)) 
    } 
    boots 
} 

È quindi possibile calcolare il valore atteso per te (dal momento che ottieni una distribuzione di alcuni esempi di realizzazione):

# Mean standard error 
mean(custom.boot(times=1000)) 
[1] 0.08998023 

Alcuni anni più tardi ...

penso che questo è più bello:

mean(replicate(times, sd(sample(df, replace=T))/sqrt(length(df))))