The following code draws a sample of size n = 30 from a chi-square distribution with
15 degrees of freedom, and then puts B = 200 bootstrap samples into a matrix M.
After that, the apply() function is used to calculate the median of each bootstrap sample, giving a bootstrap
sample of 200 medians.
```{r}
set.seed(117888)
data <- rchisq(30, 15)
M <- matrix(rep(0, 30 * 200), byrow = T, ncol = 30)
for (i in 1:200)
M[i, ] <- sample(data, 30, replace = T)
bootstrapmedians <- apply(M, 1, median)
• Use the var() function to calculate the variance of the bootstrapped medians .
• Use the apply() function to calculate bootstrapped means rather than bootstrapped medians, and
then use var() to calculate an estimate of the variance of the mean .
But we only have n = 30 samples. Did the bootstrap do a reasonable job of estimating the variance of the
mean .
引用chatGPT作答,这段代码从自由度为15的卡方分布中抽取大小为n = 30的样本,然后将B = 200个自助样本放入矩阵M中。之后,使用apply()函数计算每个自助样本的中位数,得到200个中位数的自助样本。
• 使用var()函数计算自助中位数的方差。
• 使用apply()函数计算自助均值而不是自助中位数,然后使用var()函数计算均值的方差估计。