r - 用于引导 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))
}
解决方案
There are two issues here:
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.
For (standard) bootstrap, the bootstrap samples are always as large as the original sample. So
chiN
is not necessary and would be set tosampleN
.
推荐阅读
- python - 对 Spark 数据框中的行进行洗牌
- postgresql - 合并联合中的行,将空列替换为具有值的列
- schema.org - JSON-LD 中的 LocalBusiness 和 Organization 模式
- filter - 需要帮助过滤数据 - Google 表格(见说明)
- uml - 如图所示,我们可以在扩展点上使用包含吗
- javascript - Firebase `不是函数`firebase 5.11.1 及更高版本出错
- c++ - UE4中如何将FText转换为float
- linux - bash 脚本中的多线程
- javascript - 如何使用 JavaScript 将 mySQL 查询到 HTML 页面?
- javascript - 如何使用 post 方法在两个跨度之间添加代码?