chitesting <- data.frame(TQ=c(22,5,9,10,24,11,4,6,9),
P=c(14,2,2,16,3,46,3,2,12))
chisq.test(chitesting)
chitesting$observed
chitesting$expected
已经创建了这个数据框,但当我尝试查看观察到的/预期的分数时,结果显示为 NULL!对 r 非常陌生,因此将不胜感激任何帮助!
最佳答案
您需要将调用 chisq.test()
函数的结果存储在新变量中,因为 chisq.test()
函数不存储这些结果传入数据框中的值:
chitesting <- data.frame(TQ=c(22,5,9,10,24,11,4,6,9), P=c(14,2,2,16,3,46,3,2,12))
chi_result <- chisq.test(chitesting)
print("chi_result:")
print(chi_result)
print("chi_result$observed:")
print(chi_result$observed)
print("chi_result$expected:")
print(chi_result$expected)
输出:
Warning message:
In chisq.test(chitesting) : Chi-squared approximation may be incorrect
[1] "chi_result:"
Pearson's Chi-squared test
data: chitesting
X-squared = 49.299, df = 8, p-value = 5.572e-08
[1] "chi_result$observed:"
TQ P
[1,] 22 14
[2,] 5 2
[3,] 9 2
[4,] 10 16
[5,] 24 3
[6,] 11 46
[7,] 4 3
[8,] 6 2
[9,] 9 12
[1] "chi_result$expected:"
TQ P
[1,] 18.0 18.0
[2,] 3.5 3.5
[3,] 5.5 5.5
[4,] 13.0 13.0
[5,] 13.5 13.5
[6,] 28.5 28.5
[7,] 3.5 3.5
[8,] 4.0 4.0
[9,] 10.5 10.5
关于r - 我正在尝试在 r 中进行 chi2 测试,但它没有向我显示观察到的值,我不确定为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75864917/