我创建了一系列多峰分布,以测试Hartigan dip测试对单峰性的有效性。这些分布均具有2000个观测值,并使用一个或多个rnorm函数创建。
该代码的用途概述如下。
计算每个分布要存储在“统计”变量中的下垂统计
将p值提取到“ dipstatistic”变量中并存储以备后用
每次分配均经过此计算后,请参考此变量,以供日后评估
我相信我这里的问题在于指示在循环外部生成的变量。有人对如何纠正此问题有任何建议吗?
x1 <- rnorm(2000,4,.5)
x2 <- c(rnorm(1000,0,1),rnorm(1000,4,1))
x3 <- c(rnorm(667,0,1),rnorm(667,4,1),rnorm(666,8,1))
x4 <- c(rnorm(500,0,1),rnorm(500,4,1),rnorm(500,8,1),rnorm(500,12,1))
dip.test(x1, simulate.p.value = FALSE, B = 2000)
dip.test(x2, simulate.p.value = FALSE, B = 2000)
dip.test(x3, simulate.p.value = FALSE, B = 2000)
dip.test(x4, simulate.p.value = FALSE, B = 2000)
y=4
dipstatistic <- rep(0,y)
ID <- 1:y
for (i in 1:y) {
statistic <- dip.test("need to find way to identify variables" , simulate.p.value = FALSE, B = 2000)
dipstatistic[i] <- statistic$p.value
}
dipstatistic
如果将所有变量连接到列表中,则可以在此处使用sapply。
dipstatistic <- sapply(list(x1,x2,x3,x4), function(x) {
dip.test(x , simulate.p.value = FALSE, B = 2000)$p.value
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句