c(1, 2, 3, 4, 5) 的单个引导样本可以通过 sample(c(1, 2, 3, 4, 5), 5, TRUE) 进行采样。许多可以使用replicate 绘制:
# this was your original code
set.seed(1)
n <- 100
x <- rnorm(n)
y <- 5 + 3*x + rnorm(n, mean = 0, sd = 0.3)
df <- data.frame(x=x,y=y)
# before we do many bootstrap samples, first define what happens in each one of them
one_regr <- function(){
mysample <- sample(1:n, n, TRUE) # which observations pairs are in this
# bootstrap sample?
my_df <- df[mysample, ]
slope <- lm(y ~x, data = my_df)$coefficients[2]
return(slope)
}
# test run -- works!
one_regr()
# now we can do many replicates with replicate()
many_slopes <- replicate(5000, one_regr())
hist(many_slopes, breaks = 30)
quantile(many_slopes, c(.025, .975))
加法1:
如果您不仅对斜率进行采样,而且对截距以及 in 进行采样,则可以从中获得更多乐趣
one_regr <- function(){
mysample <- sample(1:n, n, TRUE) # which observation pairs are in this
# bootstrap sample?
my_df <- df[mysample, ]
return( lm(y ~x, data = my_df)$coefficients )
}
many_lines <- replicate(5000, one_regr())
intercepts <- many_lines[1,]
slopes <- many_lines[2,]
plot(df$x, df$y, main = "data and 500 regression lines")
for(i in 1:5000){ # just draw the regression lines
abline(a = intercepts[i], b = slopes[i], col = "#00009001")
}
plot(slopes, intercepts, main = "bootstrapped coefficients",
pch = 16, col = "#00009010")
quantile(slopes, c(.025, .975))
加法2:
https://www.investopedia.com/terms/s/standard-error.asp 状态:
统计的标准误差 (SE) 是统计样本总体的近似标准差。
所以斜率的标准误差是我们自举斜率的标准差:
> sd(slopes)
[1] 0.02781162
粗略地说,这应该是 95%-CI 宽度的 4 倍:
> sd(slopes)
[1] 0.02781162
> abs(2.95 - 3.05)/4
[1] 0.025