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) 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.
I would suggest to you the following: 1) Run the same thing, but with a loop instead of apply 2) add the to loop a printing that shows you on what cycle of the loop the function breaks 3) see if that vector has any Inf or NA values (although in general I think you are using a numeric instead of a factor on the function, which might be the source of the problem) 4) if you can't figure what is wrong there - use "dput", and put that vector on text, and e-mail it back so we could see if we can reproduce the error and find a reason for it. 5) Report if you found a solution on yourself - so others would benefit from your experience. Cheers :) Tal ----------------Contact Details:------------------------------------------------------- Contact me: Tal.Galili@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- On Tue, Aug 3, 2010 at 2:47 PM, imrib <bisrael@agri.huji.ac.il> 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) > > 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@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. >[[alternative HTML version deleted]]
The apply function coerces the factor results to a character array apply(g,2,class) # gives character The kruskal.test function doesn't take character vector as the group argument. kruskal.test(as.character(plant.height) ~ as.character(g[,8])) #doesn't work kruskal.test(plant.height ~ as.character(g[,8])) #doesn't work kruskal.test(as.character(plant.height) ~ g[,8]) #works You'd better change the kw function. kw <- function(x) kruskal.test(plant.height ~ as.factor(x))$"p.value" ----- A R learner. -- View this message in context: http://r.789695.n4.nabble.com/Kruskal-Walllis-test-tp2311712p2312063.html Sent from the R help mailing list archive at Nabble.com.
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
Reasonably Related Threads
- Saving kruskal wallis p-values
- Kruskal Wallis Post hoc
- extracting results from wilcox_test (package::coin)
- using functions with multiple arguments in the "apply" family
- Multiple Comparisons-Kruskal-Wallis-Test: kruskal{agricolae} and kruskalmc{pgirmess} don't yield the same results although they should do (?)