[R] Kruskal Walllis test

peter dalgaard pdalgd at gmail.com
Tue Aug 3 17:35:36 CEST 2010


On Aug 3, 2010, at 1:47 PM, imrib wrote:

> 
> Hi all
> My data table (g) contains a continues data column (plant.height) and other
> columns (columns 8 to 57), 
> 
> each with number of levels of different factors.  ANOVA test was done and
> the p-values were extracted 
> 
> as follos:
> 
> a <- function(x) anova(lm(plant.height ~ x))$"Pr(>F)"[1]
> 
> r<- apply(g[,8:57],2,a)

This looks like an invitation to disaster. apply() will coerce g[,8:57] to a matrix, losing all factor definitions. As it happens, lm() will survive this, because

> lm(0:1~c("a","b"))

Call:
lm(formula = 0:1 ~ c("a", "b"))

Coefficients:
 (Intercept)  c("a", "b")b  
           0             1  

Warning message:
In model.matrix.default(mt, mf, contrasts) :
  variable 'c("a", "b")' converted to a factor

But kruskal.test dies on the similar construction, essentially because

> is.finite("a")
[1] FALSE
................

Try it with lapply(g[,8:57], kw) instead.

-pd


> 
> If I try to do a Kruskal-Wallis test :
> 
> kw <- function(x) kruskal.test(plant.height ~ x)$"p.value"
> 
> r.kw <- apply(g[,8:57],2,kw)
> 
> I get the following error message:
> 
> Error in kruskal.test.default(c(0.16, 0, 0.007, 0.078, 0, 0.08, 0.19,  :
> 
> all group levels must be finite
> 
> Why do I get this error ? (the values in c() are the plant.height values)
> 
> Thanks
> 
> 
> -- 
> View this message in context: http://r.789695.n4.nabble.com/Kruskal-Walllis-test-tp2311712p2311712.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list