【问题标题】:R creating two-by-two table for chisq.testR 为 chisq.test 创建二乘二表
【发布时间】:2021-10-23 12:05:50
【问题描述】:
library(dslabs)
data("research_funding_rates")
research_funding_rates

“使用所有学科的总数,按获奖状态(获奖/未获奖)构建一个 2×2 的性别(男性/女性)表。”

我试过了

r_s <- research_funding_rates %>% mutate(not_awarded_men = applications_men - awards_men, not_awarded_women = applications_women - awards_women)
r_s_t <- r_s %>% summarise(not_awarded_women = sum(not_awarded_women), awarded_women = sum(awards_women), not_awarded_men = sum(not_awarded_men), awarded_men = sum(awards_men))
colnames(r_s_t) <- NULL
t <- as.table(matrix(r_s_t, ncol = 2, byrow=TRUE))
chisq.test(t)

但这给出了错误 sum(x) 出错,类型列表无效

str(t) 返回 4人名单 $ : 数字 1011 $ : 数字 1345 $ : 数字 177 $ : 数字 29 而不是表格

而这个简单的例子

sex_pet <- matrix(c(19, 34, 22, 26, 28, 6),nrow=2, byrow = TRUE, dimnames =list(sex=c("Male", "Female"), pet=c("Cats", "Dogs", "Other")))
sex_pet_table <- as.table(sex_pet)
chisq.test(sex_pet_table)

按预期工作。如何从上面的数据中获取 2x2 表来执行 chisq.test??

谢谢,

汉斯

【问题讨论】:

    标签: r chi-squared


    【解决方案1】:

    这是因为您的r_s_tdata.frame。您应该先将其转换为向量,然后将其传递给矩阵。

    t <- matrix(as.numeric(r_s_t), ncol = 2, byrow=TRUE)
    t
    #>      [,1] [,2]
    #> [1,] 1011  177
    #> [2,] 1345  290
    chisq.test(t)
    #> 
    #>  Pearson's Chi-squared test with Yates' continuity correction
    #> 
    #> data:  t
    #> X-squared = 3.8111, df = 1, p-value = 0.05091
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-16
      • 2020-08-20
      • 2015-09-24
      • 2014-07-25
      • 2016-06-20
      • 1970-01-01
      相关资源
      最近更新 更多