Christoph Bier
2003-Nov-07 13:55 UTC
[R] chisq.test error: x and y must have at least 2 levels
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? BTW to the german speaking readers: What's the R pendant to or translation for "Konfigurationsfrequenzanalyse (KFA)"? TIA Best regards, Christoph _____________________ ? (I got help from a colleague to do this =)) v007.s <- fbhint.spss1[58:219] v007.1.s <- fbhint.spss1[360:521] name7.s<-names(v007.s) name71.s<-names(v007.1.s) bln<-length(name7.s) for (i in 1:bln) { v1<-name7.s[i] v11<-name71.s[i] print(paste("v1=",v1,"v11=",v11)) d1<-fb.12.hint[[v1]] d2<-fb.12.hint[[v11]] res<-chisq.test(d1,d2) print(res) } -- Christoph Bier, Dipl.Oecotroph., Email: bier at wiz.uni-kassel.de Universitaet Kassel, FG Oekologische Lebensmittelqualitaet und Ernaehrungskultur \\ Postfach 12 52 \\ 37202 Witzenhausen Tel.: +49 (0) 55 42 / 98 -17 21, Fax: -17 13
Peter Dalgaard
2003-Nov-07 14:26 UTC
[R] chisq.test error: x and y must have at least 2 levels
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
Marc Schwartz
2003-Nov-07 14:32 UTC
[R] chisq.test error: x and y must have at least 2 levels
On Fri, 2003-11-07 at 07:55, Christoph Bier wrote:> 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?'levels' indicates how many levels are defined for the factor. However, it is possible that the actual data present may contain only a subset of the levels. In this case, your factors contain defined levels for both "ja" and "nein", however in the subset of data that you are analyzing, only "nein" appears (and of course NA in the second, though NA is not defined as a level). Hence that particular cross-tabulation of factors contains one level each, yielding a table with a single cell. Presumably, your original dataset contains both values prior to subsetting. See ?factor and ?levels for more information.> BTW to the german speaking readers: What's the R pendant to or > translation for "Konfigurationsfrequenzanalyse (KFA)"? >I am not a German speaker, however: Analysis of Configuration Frequencies (CFA)? There is a package 'cfa' on CRAN. HTH, Marc Schwartz