首页 > 解决方案 > 用于引导 ChiSq 中值估计的 R 代码检查

问题描述

我有一个家庭作业问题要编写一个函数,该函数引导它找到 df = 2 Chisq 分布中位数的 95% CI。我的函数似乎有效,但从 wiki 我得到中位数的公式为 k(1 - 2/9k)^3,对于 k = 2 产生 0.343。我的函数的 CI 估计为 (1.28, 1.51) 有很多大分布、样本量和模拟次数(100000)。所以理论上的答案不在那个区间的任何地方。有人可以告诉我我的代码在哪里失败了吗?

ChisqMedian_CI <- function(chiN, nsim, sampleN){
y <- rchisq(n=chiN, df=2)
medy_resample <- NULL
for (i in 1:nsim) {
  y_resample <- sample(y, replace=TRUE, size=sampleN)
  medy_resample[i] <- median(y_resample)
}
  LB <- quantile(medy_resample, probs=c(0.025))
  UB <- quantile(medy_resample, probs=c(0.975))
  return(c(LB, UB))
}

标签: rmedianmontecarloconfidence-intervalchi-squared

解决方案


There are two issues here:

  1. The median of a chi-squared distributed random variable is approximately k * (1 - 2 / (9k))^3. For k = 2, this is about 1.4. This is in line with your results.

  2. For (standard) bootstrap, the bootstrap samples are always as large as the original sample. So chiN is not necessary and would be set to sampleN.


推荐阅读