It looks to me from reading the help on Matlab's Kruskal Wallis test as if it would be easy to stack up your different responses and have an indicator for group, using the kruskalwallis(x,group)
syntax.
You ask about doing it in R in comments.
Here's the first example from the R help on kruskal.test
, done first using three groups placed into a list, and the second way by stacking them and constructing a group variable:
> x <- c(2.9, 3.0, 2.5, 2.6, 3.2) # normal subjects
> y <- c(3.8, 2.7, 4.0, 2.4) # with obstructive airway disease
> z <- c(2.8, 3.4, 3.7, 2.2, 2.0) # with asbestosis
> kruskal.test(list(x, y, z))
Kruskal-Wallis rank sum test
data: list(x, y, z)
Kruskal-Wallis chi-squared = 0.7714, df = 2, p-value = 0.68
> ## Equivalently,
> x <- c(x, y, z)
> g <- factor(rep(1:3, c(5, 4, 5)),
+ labels = c("Normal subjects",
+ "Subjects with obstructive airway disease",
+ "Subjects with asbestosis"))
> kruskal.test(x, g)
Kruskal-Wallis rank sum test
data: x and g
Kruskal-Wallis chi-squared = 0.7714, df = 2, p-value = 0.68
Then there's the formula interface:
> kruskal.test(x~g)
Kruskal-Wallis rank sum test
data: x by g
Kruskal-Wallis chi-squared = 0.7714, df = 2, p-value = 0.68
Now for random data without names (sample sizes 30 and 10):
> kruskal.test(list(rgamma(30,4,.1),rgamma(10,4,.2)))
Kruskal-Wallis rank sum test
data: list(rgamma(30, 4, 0.1), rgamma(10, 4, 0.2))
Kruskal-Wallis chi-squared = 4.3795, df = 1, p-value = 0.03637
Now for a whole bunch of different-sized groups:
> kruskal.test(list(rgamma(30,4,.1),rgamma(10,4,.2),rgamma(5,4,.3),
rgamma(6,4,.25),rgamma(8,4,.28)))
Kruskal-Wallis rank sum test
data: list(rgamma(30, 4, 0.1), rgamma(10, 4, 0.2), rgamma(5, 4, 0.3),
rgamma(6, 4, 0.25), rgamma(8, 4, 0.28))
Kruskal-Wallis chi-squared = 16.8088, df = 4, p-value = 0.002105
--
I'm sorry I seem to have missed the question inside this comment before: "How does R deal with empty values?" -- you seem to be assuming the values will be stored in a rectangular array and since the groups are of different size, there will be missing values. Do I have that right?
Well, two issues:
1) R has the data value NA
to represent missing values. It is of any type (or rather, there's one of each type).
e.g.:
x <- c(2.9, 3.0, 2.5, 2.6, 3.2)
y <- c(3.8, 2.7, 4.0, NA, 2.4)
cbind(x,y)
produces:
x y
[1,] 2.9 3.8
[2,] 3.0 2.7
[3,] 2.5 4.0
[4,] 2.6 NA
[5,] 3.2 2.4
2) R has data structures suited to non-rectangular data (like lists) that will allow you to avoid this problem in any case. For example, depending on what you're trying to do, you could read each row into a vector and put those vectors into a list; the differences in length wouldn't matter. They could then be handled with any of the above methods.
--
R can read in a variety of formats and a variety of ways of indicating missing values, or it can do formatted reads (and so identify them that way), so your ascii files should present no problems. It's relatively straightforward.
Or, if you already have your data in matlab, the R.matlab
package should help.
Personally I'd just go with reading the data in. If you have something more complex than the usual sort of thing (covered in the help on read.table
and variants or scan
) you can always look at the relevant manual.
If you can show the kind of format of your files I may be able to give more detailed help.
Note that there are various documents for matlab or octave users to help them with R.