r - 我正在尝试在 r 中进行 chi2 测试,但它没有向我显示观察到的值,我不确定为什么

标签 r

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/

相关文章:

r - 如何从 R 字符串中的多个列表中检测子字符串

r - 自适应移动平均线-R中的最佳性能

r - 在单词后获取文本--R 网页抓取

r - 将字符向量拆分成句子

R: Rgtk2: gwidgets: gWidgetsRGtk2

r - 散点图 : Show missing factor level(s) in legend

r - 生成总和为固定值的非负(或正)随机整数

r - `lme4` 中 BLUE 或 BLUP 差异的平均方差

r - 在 r 中解冻数据框的最佳方法

r - 如何使用 rscript 代码在 shiny 的 ui.R 中填充下拉菜单?