[R] Friedman test
peter dalgaard
pdalgd at gmail.com
Thu Sep 24 16:44:21 CEST 2015
On 24 Sep 2015, at 15:07 , Agustin Lobo <aloboaleu at gmail.com> wrote:
> I would like to make sure that I'm using friedman.test() correctly,
> because I have to reformat my data set to fulfil the requirement
> "exactly one observation in y for each combination of levels of groups
> and blocks" mentioned
> in the help page of function friedman.test().
>
> Assuming data from
> http://www.inside-r.org/packages/cran/muStat/docs/mu.kruskal.test
>
> treatments <- factor(rep(c("Trt1", "Trt2", "Trt3"), each=4))
> people <- factor(rep(c("Subj1", "Subj2", "Subj3", "Subj4"), 3))
> y <- c(0.73,0.76,0.46,0.85,0.48,0.78,0.87,0.22,0.51,0.03,0.39,0.44)
> midata <- data.frame(treatments,people,y)
> midata[1:10,]
>
> I would do as follows:
>
> Test among treatments
> friedman.test(y ~ treatments|people,data=midata)
> Test among subjects
> friedman.test(y ~ people|treatments,data=midata)
>
> Is this correct?
Seems so. The tricky bit is to get the factors to replicate in the right way (notice also the gl() function, by the way).
A way to check is
> matrix(c(0.73,0.76,0.46,0.85,0.48,0.78,0.87,0.22,0.51,
+ 0.03,0.39,0.44), ncol=3)
[,1] [,2] [,3]
[1,] 0.73 0.48 0.51
[2,] 0.76 0.78 0.03
[3,] 0.46 0.87 0.39
[4,] 0.85 0.22 0.44
> with(midata,tapply(y,list(people, treatments), mean))
Trt1 Trt2 Trt3
Subj1 0.73 0.48 0.51
Subj2 0.76 0.78 0.03
Subj3 0.46 0.87 0.39
Subj4 0.85 0.22 0.44
You could also have used this construction:
> m <- matrix(c(0.73,0.76,0.46,0.85,0.48,0.78,0.87,0.22,0.51,
+ 0.03,0.39,0.44), ncol=3)
> as.data.frame(as.table(m))
Var1 Var2 Freq
1 A A 0.73
2 B A 0.76
3 C A 0.46
4 D A 0.85
5 A B 0.48
6 B B 0.78
7 C B 0.87
8 D B 0.22
9 A C 0.51
10 B C 0.03
11 C C 0.39
12 D C 0.44
It can be elaborated: Adding (named) dimnames to the matrix/table will get names and levels right for the two factors, and you might also want responseName="y".
-pd
--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
More information about the R-help
mailing list