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

Peter Dalgaard p.dalgaard at biostat.ku.dk
Fri Nov 7 15:26:59 CET 2003


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

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list