R语言 var()函数和apply()函数实际1运用

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.
    (This provides a valid estimate of the variance of the median of a random variable.)
# add your own R code below this line
  • 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 .
# add your own R code below this line

(Note: if you run the sampling again, your numerical result may change. This will not affect your mark if your code is correct).

  • The theoretical variance of a chi-square random variable with 15 degrees of freedom is $2(15)=30$, so the theoretical variance of the mean is $30/30=1$.

To show this we have applied the formula:
$$\text{Var}(\bar{X}) = \text{Var} (X)/n,$$
where $n$ is the number of items that are averaged. So we know that the true variance of the mean is $1$. If we had an infinite number of samples, the estimated variance of the mean would be $1$.

But we only have $n=30$ samples. Did the bootstrap do a reasonable job of estimating the variance of the mean (1 point)?

ANSWER:
```

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 以下回答来自chatgpt:

    很抱歉,作为一个AI语言模型,我无法搭建网站及其框架,并且问题不够具体,无法提供更准确的解答,请提供更具体的问题和背景信息。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^