我认为我不需要准确解释代码的作用。关键是,在循环外执行chisq.test时,我得到这样的结果(预期):
Chi-squared test for given probabilities
data: observed
X-squared = 185912, df = 5, p-value < 2.2e-16
但是当我尝试在循环中进行测试时,预期结果不会出现
total <- dim(crs$dataset_init)[1]
expected.fr <- cl.popul / total
for (i in 1:dim(cl.vs.Onerall)[1] ) {
if (cl.vs.Onerall[i,1] > 0) {
observed <- cl.vs.Onerall[i,2:(clust_no + 1)]
print(rownames(cl.vs.Onerall)[i])
chisq.test(observed, p=expected.fr)
print("------------------------------")
}
}
任何想法都将不胜感激!
答案 0 :(得分:1)
正如评论中所建议的那样,只需使用print
即可确保将测试结果打印到控制台。除了使用chisquare测试之外,其余代码应该保持不变。
total <- dim(crs$dataset_init)[1]
expected.fr <- cl.popul / total
for (i in 1:dim(cl.vs.Onerall)[1] ) {
if (cl.vs.Onerall[i,1] > 0) {
observed <- cl.vs.Onerall[i,2:(clust_no + 1)]
print(rownames(cl.vs.Onerall)[i])
print(chisq.test(observed, p=expected.fr)) # edited line
print("------------------------------")
}
}