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

How can I run multiple chi-square tests without repeating R code?

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

    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
#

最好, 查米尔

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