Home > Back-end >  Unequal Group Chi Square Test in R?
Unequal Group Chi Square Test in R?

Time:12-29

Does anyone know if there is a package and/or function in R that will perform a Chi-square test when the numbers of samples per group are not equal?

For example, there are a total of 1490 subjects, 721 men and 769 women. Of the trait I am wanting to compare, 83 men and 25 women have it. Are these differences significant?

In R, using the chisq.test function, I get an error stating that "x and y must have the same length".

I am at a loss for what else to try. There does not seem to be an analogous question on this or other forums. Thanks for any help you might be able to provide.

CodePudding user response:

I'm guessing you're supplying the chisq.test function with two vectors of different length, as if you made one "male" vector of length 721 and one "female" vector of length 769.

The data structures and function calls to run the test are straightforward:

df <- data.frame(sex = c(rep("M", 721), rep("F", 769)),
                 trait = c(rep(1, 83), rep(0, 721-83),
                           rep(1, 25), rep(0, 769-25)))
 
chisq.test(df$sex, df$trait)
   
      0   1
  F 744  25
  M 638  83
 
    Pearson's Chi-squared test with Yates' continuity correction

data:  df$sex and df$trait
X-squared = 36.553, df = 1, p-value = 1.486e-09
  • Related