我正在尝试在卡方检验中绘制零分布图。在R中,可以使用蒙特卡罗模拟来获取经验p值的代码:
chisq.test(d,simulate.p.value=TRUE,B=10000)
但它没有返回分布图。有没有办法让R返回测试的模拟值?
我正在尝试在卡方检验中绘制零分布图。在R中,可以使用蒙特卡罗模拟来获取经验p值的代码:
chisq.test(d,simulate.p.value=TRUE,B=10000)
但它没有返回分布图。有没有办法让R返回测试的模拟值?
如果你查看chisq.test
函数定义(大约在capture.output(chisq.test)
的第56行),你会看到模拟部分:
if (simulate.p.value && all(sr > 0) && all(sc > 0)) {
setMETH()
tmp <- .Call(C_chisq_sim, sr, sc, B, E)
STATISTIC <- sum(sort((x - E)^2/E, decreasing = TRUE))
PARAMETER <- NA
PVAL <- (1 + sum(tmp >= almost.1 * STATISTIC))/(B +
1)
}
## Some data
x <- as.table(rbind(c(762, 327, 468), c(484, 239, 477)))
dimnames(x) <- list(gender = c("F", "M"),
party = c("Democrat","Independent", "Republican"))
sr <- rowSums(x)
sc <- colSums(x)
n <- sum(x)
E <- outer(sr, sc, "*")/n
v <- function(r, c, n) c * r * (n - r) * (n - c)/n^3
V <- outer(sr, sc, v, n)
dimnames(E) <- dimnames(x)
B = 2000
tmp <- .Call(stats:::C_chisq_sim, sr, sc, B, E)
STATISTIC <- sum(sort((x - E)^2/E, decreasing = TRUE))
almost.1 <- 1 - 64 * .Machine$double.eps
PVAL <- (1 + sum(tmp >= almost.1 * STATISTIC))/(B + 1)
tmp
包含您想要的输出。变量 PVAL
匹配输出结果。chisq.test(x, simulate.p.value = T, B=2000)$p.value