如何在不重复R代码的情况下运行多个卡方检验?

时间:2018-08-09 15:52:12

标签: r chi-squared

我正在尝试针对多个分布(针对相同的预期分布)进行卡方检验。我想知道如何通过将所有测试集成到一个函数中来提高代码效率。感谢您的支持。

    exp <- c(0.26,0.74) #defines the expected distribution

    obsof <- c(57, 2051) #defines the observed distribution
    obsto <- c(47, 235)
    obsand <- c(58, 344)
    obsthe <- c(42, 293)
    obsin <- c(27, 134)


    test1 <- chisq.test(obsof, p=exp) #defines the test
    test1
    test1$p.value #the test is significant if the p value is less than or equal to 0.05
    test1$stdres #returns standardized residuals

    test2 <- chisq.test(obsto, p=exp)
    test2
    test2$p.value

    test3 <- chisq.test(obsand, p=exp)
    test3
    test3$p.value
    test3$stdres

最好, Chamil

1 个答案:

答案 0 :(得分:2)

lapply(mget(ls(pattern="^obs.*")),chisq.test,p=exp)