使用Bootstrap方法计算标准误差的R代码

8

我有一个值的数组:

> 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

我想使用boot包来计算数据的标准误差。以下是我使用的命令:http://www.ats.ucla.edu/stat/r/faq/boot.htm
library(boot)
boot(df, mean, R=10)

我遇到了这个错误:

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

有人能帮我找出问题吗?谢谢


1
你对 c 的函数定义是什么?基础的 c 函数不适合引导程序。 - Frank
3个回答

16

如果您正在进行均值引导,则可以按照以下步骤操作:

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
如果您只将mean作为参数输入,那么您将会收到与您之前获得的错误类似的错误提示。
bootMean <- boot(x,mean,100)
Error in mean.default(data, original, ...) : 
  'trim' must be numeric of length one

我正在使用bootstrap函数,想知道是否有一种方法可以自动从boot调用中提取标准误差?似乎没有“bootMean”的子集可以调用以单独提取各个统计数据。 - jclifto8

5

我之前从未真正使用过Boot,因为我不明白它会带来什么好处。

鉴于标准误差的定义如下:

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

我认为您可以简单地使用以下函数来完成此操作:

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
}

您可以通过计算自己的期望值(因为您获得了一些样本实现的分布)来进行计算:
# Mean standard error
mean(custom.boot(times=1000))
[1] 0.08998023

几年以后...
我认为这样更好:
mean(replicate(times, sd(sample(df, replace=T))/sqrt(length(df))))

1

c函数对于boot来说不够用。如果你查看boot的帮助文档,就会发现你的函数必须能够接收数据和索引。因此,你需要编写自己的函数。此外,它应该返回你想要标准误差的值,比如平均值。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接