9

考虑以下引导程序:

library(MASS)
library(boot)

# c)
set.seed(1)
boot.fn= function(data, index) mean(data[index])
output=boot(Boston$medv, boot.fn, 1000)

如果我们运行print(output),我们会得到

Call:
boot(data = Boston$medv, statistic = boot.fn, R = 1000)


Bootstrap Statistics :
    original      bias    std. error
t1* 22.53281 0.008517589   0.4119374

但是,当我检查output对象时,我找不到代表引导统计的值。和在boot 返回的实际对象original中在哪里?biasstd. erroroutput

4

5 回答 5

12

它们由对象计算print.boot而不存储在boot对象中。查看getAnywhere(print.boot)详情。

您可以自己计算这些值,也可以使用capture.output.

对于您的示例:

#original:
output$t0
#bias:
mean(output$t)-output$t0
#se: 
sd(output$t)
于 2013-11-13T20:28:15.057 回答
3

该命令似乎有效:

apply(output$t,2,sd)[1]
于 2020-09-24T15:04:44.770 回答
1

您可以使用摘要提取标准错误:

se <- summary(output)$bootSE
于 2019-01-14T07:32:51.900 回答
1
output_tab <- t(rbind(
  # this grabs the original coefficients
  output$t0, 
  # bootstrapped coefficients' standard errors
  apply(output$t, 2, function(x) sd(x, ra.rm = TRUE))  
))  

output_tab
#         [,1]      [,2]
#[1,] 22.53281 0.4106622

于 2021-03-24T17:09:47.313 回答
0

提取 se 使用capture.output

library(stringr)

x <- capture.output(output) # store the output as text
x <- str_extract(x ,"^t1.*$") # grab the line that starts with t1
x <- x[!is.na(x)] # remove all the lines we don't need
se <- as.numeric(unlist(str_extract_all(x, '[0-9.]+$'))) # extract the final value (se)
#[1] 0.4119374
于 2017-12-29T23:02:50.587 回答