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

如何在不重复R代码的情况下运行多个卡方检验?,r,chi-squared,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)

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

    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

exp
lappy(mget(ls)(pattern=“^obs.”)),chisq.test,p=exp)
lappy(mget(ls)(pattern=“^obs.”)),chisq.test,p=exp)
lappy(mget(ls(pattern=“^obs.”)),chisq.test,p=exp)
,尽管您的exp总和不等于1。所以我真的不知道你是如何运行Test1的。对不起,我修复了exp.@onyanbu。它确实有效,非常感谢。
lappy(mget(ls(pattern=“^obs.”)),chisq.test,p=exp)
尽管您的exp加起来不是1。所以我真的不知道你是如何运行Test1的。对不起,我修复了exp.@onyanbu。这很有效,真的非常感谢。