Chamil Rathnayake
Chamil Rathnayake

Reputation: 129

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

I am trying to run chi-square tests for multiple distributions (against the same expected distribution). I wonder how I can increase the efficiency of my code by integrating all the tests into one function. I appreciate some support.

    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
#

Best, Chamil

Upvotes: 0

Views: 418

Answers (1)

Onyambu
Onyambu

Reputation: 79188

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

Upvotes: 2

Related Questions