r-创建一个循环来运行统计测试



我需要运行一个统计测试并计算p_hat_6,使用N=10个样本,每个样本的大小从X到uniform(0,14(为N=10。这是我在mu=6:时计算p_hat的原始循环

pvalue <- rep(0,10)
reject <- 0
alpha <- 0.05
N <- 10
for (n in seq_along(pvalue)){
pvalue[n] <- wilcox.test(runif(10,0,14), mu=6)$p.value
reject[n] <- ifelse(pvalue[n] > alpha,0,1)
}
p_hat_6 <- (sum(reject))/N
p_hat_6

使用相同的N和N,我需要再重复5次,并进行以下更改:

(1) Data from X ∼ uniform(0, 16) and compute p_hat_7 (mu=7)
(2) Data from X ∼ uniform(0, 18) and compute p_hat_8 (mu=8)
(3) Data from X ∼ uniform(0, 20) and compute p_hat_9 (mu=9)
(4) Data from X ∼ uniform(0, 22) and compute p_hat_10 (mu=10)
(5) Data from X ∼ uniform(0, 24) and compute p_hat_11 (mu=11)

当间隔每次增加2,μ每次增加1时,我如何循环?

我们可以创建一个函数


fun1 <- function(N, pvalue, reject, max_val, mu_val) {
for (n in seq_along(pvalue)){
pvalue[n] <- wilcox.test(runif(N ,0, max_val), mu=mu_val)$p.value
reject[n] <- ifelse(pvalue[n] > alpha,0,1)
}
p_hat_6 <- (sum(reject))/N
return(p_hat_6)
}

并将其用于Map

Map(fun1, max_val = seq(14, 24, by = 2), mu_val = 6:11,
MoreArgs = list(N = 10, pvalue = pvalue, reject = reject))   

-输出

[[1]]
[1] 0
[[2]]
[1] 0.2
[[3]]
[1] 0.2
[[4]]
[1] 0
[[5]]
[1] 0.1
[[6]]
[1] 0.2

或围绕函数创建外循环

mu_val <- 6:11
max_val <- seq(14, 24, by = 2)
out <- numeric(length(mu_val))
for(i in seq_along(mu_val)) {
out[i] <- fun1(N = 10, pvalue = pvalue, 
reject = reject, max_val = max_val[i], mu_val = mu_val[i])
}
out
[1] 0.1 0.1 0.1 0.1 0.3 0.0

数据

N <- 10
pvalue <- rep(0, N)
reject <- rep(0, N)
alpha <- 0.05

相关内容

  • 没有找到相关文章

最新更新