[R] chisq.test error: x and y must have at least 2 levels

Spencer Graves spencer.graves at pdf.com
Fri Nov 7 16:29:53 CET 2003


As Peter said, the standard chi-square for a 2x2 table won't work for 
your data.  However, if you tabulate the numbers first and then ask for 
the chi-square, you get a warning, not an error: 

 > contable <- array(c(42, 0,0,0), dim=c(2,2))
 > chisq.test(contable)

        Pearson's Chi-squared test with Yates' continuity correction

data:  contable
X-squared = Inf, df = 1, p-value = < 2.2e-16

Warning message:
Chi-squared approximation may be incorrect in: chisq.test(contable)
 >

##### If you are willing to make stronger assumptions, then you can get 
a finite number for chi-square: 

 > chisq.test(as.vector(contable), p = rep(0.25, 4))

        Chi-squared test for given probabilities

data:  as.vector(contable)
X-squared = 126, df = 3, p-value = < 2.2e-16

##### However, you have to be careful with this, because the following 
did NOT give me what I expected: 

 > chisq.test(contable, p = rep(0.25, 4))

        Pearson's Chi-squared test with Yates' continuity correction

data:  contable
X-squared = Inf, df = 1, p-value = < 2.2e-16

Warning message:
Chi-squared approximation may be incorrect in: chisq.test(contable, p = 
rep(0.25, 4))
 >
      Hope this helps.  spencer graves

Peter Dalgaard wrote:

>Christoph Bier <christoph.bier at web.de> writes:
>
>  
>
>>Hi,
>>
>>I use a little script¹ to make a chi-square-test on 162 factors (it
>>makes no difference if I take the numeric variant of the factors). At
>>factor nr. 4 is stops with an error:
>>
>>[1] "v1= V7.KARTM v11= V7.KAR1M"
>>Error in chisq.test(d1, d2) : x and y must have at least 2 levels
>>
>>But x and y /have/ two levels ("nein", "ja"):
>>
>> > fbhint.spss1$V7.KARTM
>>  [1] nein nein nein nein nein nein nein nein nein nein nein nein nein
>>nein nein
>>[16] nein nein nein nein nein nein nein nein nein nein nein nein nein
>>nein nein
>>[31] nein nein nein nein nein nein nein nein nein nein nein nein nein
>>nein nein
>>[46] nein nein nein nein
>>Levels: nein ja
>>
>> > fbhint.spss1$V7.KAR1M
>>  [1] nein nein nein nein nein nein nein nein nein nein nein nein nein
>>nein nein
>>[16] nein <NA> nein nein nein nein nein nein nein nein nein nein <NA>
>>nein <NA>
>>[31] nein nein nein nein nein nein nein nein nein nein nein nein <NA>
>><NA> nein
>>[46] <NA> nein nein <NA>
>>Levels: nein ja
>>
>>Or is there another meaning of 'levels' that doesn't correspond to the
>>one returned above?
>>    Any hints what's going wrong/which mistake(s) I make?
>>    
>>
>
>Well, the error message might be slightly beside the point, but the
>issue would seem to be that there are no "ja"'s inside either vector.
>I.e. it first reduces each factor to those levels that are actually
>present, then checks whether there are at least two levels.
>
>You can't do a chisquare test on a table that looks like this
>
>      nein   ja
>nein    42    0
>  ja     0    0
>
>  
>




More information about the R-help mailing list