【问题标题】:Chi-square test on contingency table (4x2) in RR中列联表(4x2)的卡方检验
【发布时间】:2019-06-18 09:15:26
【问题描述】:

我在 R 中的 4x2 列联表上计算卡方检验时遇到问题。我的脚本如下所示:

# Read data
read.table("Mortality_test.txt")

# Assign a name to the data
mortality<- read.table("Mortality_test.txt", ,col.names=c('treatment','dead'), header=TRUE, sep="\t", na.strings="NA", dec=",", strip.white=TRUE)

table(mortality)

当我运行table(mortality) 时,我得到一个列联表,如下所示:

    dead
treatment no yes
     A    63   7
     B    61   9
     C    68   2
     D    63   7

我现在的问题是,我想比较不同治疗(A、B、C、D)之间的死亡人数是否存在统计差异。如果我没记错的话,我可以用桌子上的卡方检验来做到这一点。但是,我不确定下一步该采取哪一步。

【问题讨论】:

    标签: r chi-squared contingency


    【解决方案1】:

    您有一个函数 chisq.test 来对列联表执行 chi 测试。

    这里,你的桌子

    dead <- read.table(text = "treatment no yes
                       A    63   7
                       B    61   9
                       C    68   2
                       D    63   7",header = T)
    
    > dead
      treatment no yes
    1         A 40  15
    2         B 61   9
    3         C 68   2
    4         D 63   7
    

    你需要取no和yes两列:

    chisq.test(dead[,2:3])
    
    
            Pearson's Chi-squared test
    
        data:  dead[, 2:3]
        X-squared = 4.6996, df = 3, p-value = 0.1952
    

    这在治疗之间没有区别。要查看另一个不同的示例:

    dead <- read.table(text = "treatment no yes
                       A    55   12
                       B    61   9
                       C    68   2
                       D    63   7",header = T)
    

    A 处理真正不同的地方:

        Pearson's Chi-squared test
    
    data:  dead[, 2:3]
    X-squared = 8.4334, df = 3, p-value = 0.03785
    

    【讨论】:

    • 这很简单。非常感谢您的快速回答:-)
    【解决方案2】:

    我们可以通过简单地在table() 上应用summary() 来方便地进行卡方检验。

    示例

    with(mtcars, table(cyl, gear))
    #    gear
    # cyl  3  4  5
    #   4  1  8  2
    #   6  2  4  1
    
    summary(with(mtcars, table(cyl, gear)))
    # Number of cases in table: 32 
    # Number of factors: 2 
    # Test for independence of all factors:
    #         Chisq = 18.036, df = 4, p-value = 0.001214
    #         Chi-squared approximation may be incorrect
    

    注意:“卡方近似可能不正确”是由于本例中只有 32 个观测值。

    使用您的数据summary(table(mortality)) 应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-26
      • 1970-01-01
      • 2021-08-04
      • 2017-01-03
      • 2020-10-12
      • 1970-01-01
      相关资源
      最近更新 更多