Take the 2-minute tour ×
Cross Validated is a question and answer site for people interested in statistics, machine learning, data analysis, data mining, and data visualization. It's 100% free, no registration required.

I want to compare two groups (vectors) having different lengths to see if their distributions differ considerably. However, when I use chisq.test, I get an error due to inequalities of lengths:

> chisq.test(factor(Group1), factor(Group2))

results in:

Error in chisq.test(factor(Group1[, col]), factor(Group2[, col])) : 
'x' and 'y' must have the same length

Could someone help me with this? Thanks.

share|improve this question
2  
This seems an odd thing to be doing. Can you forget the code and more clearly explain what you're trying to do, in specific terms, and why you're trying to do it? What's in Group1 and Group2? –  Glen_b Feb 18 at 3:22
    
@Glen_b, I am working on a very large dataset of patients who belong to one of five groups. I want to test if groups are not so much different than each other in terms of age, race, sex, smoking level, weight, and some other factors. Some factors are numerical and some are categorical. I used ANOVA to compare mean differences between groups for numerical factors. Also for categorical factors, I want to use chi-squared test to compare groups' distributions. However, I do not know how to do chi-sq test in R when the two vectors have different lengths. –  user3319828 Feb 18 at 3:30
    
Cont- Assume that the vectors are the values of "race" column for different groups. –  user3319828 Feb 18 at 3:34
2  
Surely 'not so much different' is a question about effect size (how different are they?) not hypothesis test ("is our sample large enough to pick up even unimportant differences?"). Why a hypothesis test? –  Glen_b Feb 18 at 3:37

1 Answer 1

up vote 4 down vote accepted

I think (as mentioned in comments) that a hypothesis test doesn't really answer the question you say you're interested in. (It also lacks power if any of the factors are ordered).

The thing is, 'not so much different' relates to a question about effect size (how different are they?) not a hypothesis test ("is our sample large enough to pick up even unimportant differences?").

You say you have very large sample sizes. This will cause you to reject as different distributions that are quite similar (since you'll have enough power to pick up tiny differences). Is that really what you mean to do? Or would you rather be able to say 'actually, they're fairly similarly distributed' when that's the case?


The direct answer to the question is you use table on the pair of factors you want to test (e.g. Group and race) and then use that as input to chisq.test e.g.

# make up some data
set.seed(32892917)
mydata <- data.frame(group=as.factor(sample(1:5,199,replace=TRUE)),
                     race=as.factor(sample(1:3,199,replace=TRUE)))

# look at the table:
 (mytab <- with(mydata,table(group,race)) )
     race
group  1  2  3
    1 16 19 11
    2 14 15 13
    3  9 14 20
    4 12 13 11
    5 11 11 10

(you'll note that each group is a different size - e.g. group 1 has 46 people, group 5 has 32)

 # do the chi-square 
 chisq.test(mytab)

    Pearson's Chi-squared test

data:  mytab
X-squared = 6.0928, df = 8, p-value = 0.6368
share|improve this answer
    
Thanks so much Glen_b. –  user3319828 Feb 18 at 4:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.