[英]R : Calculating p-value using simulations
我編寫了這段代碼,以對兩個隨機分布的觀測值x和y進行測試統計
mean.test <- function(x, y, B=10000,
alternative=c("two.sided","less","greater"))
{
p.value <- 0
alternative <- match.arg(alternative)
s <- replicate(B, (mean(sample(c(x,y), B, replace=TRUE))-mean(sample(c(x,y), B, replace=TRUE))))
t <- mean(x) - mean(y)
p.value <- 2*(1- pnorm(abs(quantile(T,0.01)), mean = 0, sd = 1, lower.tail =
TRUE, log.p = FALSE)) #try to calculate p value
data.name <- deparse(substitute(c(x,y)))
names(t) <- "difference in means"
zero <- 0
names(zero) <- "difference in means"
return(structure(list(statistic = t, p.value = p.value,
method = "mean test", data.name = data.name,
observed = c(x,y), alternative = alternative,
null.value = zero),
class = "htest"))
}
該代碼使用蒙特卡洛模擬生成測試統計量均值(x)-均值(y)的分布函數,然后計算p值,但顯然我錯過了定義該p值的原因,因為:
> set.seed(0)
> mean.test(rnorm(1000,3,2),rnorm(2000,4,3))
輸出應如下所示:
mean test
data: c(rnorm(1000, 3, 2), rnorm(2000, 4, 3))
difference in means = -1.0967, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0
但是我卻得到了這個:
mean test
data: c(rnorm(1000, 3, 2), rnorm(2000, 4, 3))
difference in means = -1.0967, p-value = 0.8087
alternative hypothesis: true difference in means is not equal to 0
有人可以向我解釋這個錯誤嗎?
據我所知,您的代碼中有很多錯誤和錯誤:
quantile(T, 0.01)
-這里T == TRUE
,因此您正在計算1。 s
。 mean(sample(c(x,y), B, replace=TRUE))
您想在這里做什么? c()
函數組合x
和y
。 采樣沒有意義,因為您不知道它們來自什么人群 t
,它應取決於方差(和樣本量)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.