How to perform Friedman test in R?


To perform Friedman test in R, we can follow the below steps −

  • First of all, create a matrix.
  • Then, use friedman.test function to perform the Friedman test.

Example1

Create the matrix

Let's create a data matrix as shown below −

 Live Demo

First<-sample(1:100,20)
Second<-sample(1:100,20)
Third<-sample(1:100,20)
Fourth<-sample(1:100,20)
Period<-matrix(c(First,Second,Third,Fourth),byrow=FALSE,nrow=20)
Period

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

     [,1] [,2] [,3] [,4]
[1,]  56   19   61  100
[2,]  16   1    89  82
[3,]  34   18   13  13
[4,]  85   85   35  55
[5,]  10   29   42  79
[6,]  99   52   11  92
[7,]  27   42   87  86
[8,]  42   26   6   63
[9,]  19   3    90  16
[10,] 48   80   93   4
[11,] 89   43   94  52
[12,] 46   62   54  30
[13,] 61   68   57  71
[14,] 5    2    64  64
[15,] 15   93   45  91
[16,] 86   61   28 36
[17,] 66   53   84 28
[18,] 80   90   73 33
[19,] 18   23   7 24
[20,] 26   73   95 2

Perform Friedman test

Using friedman.test function to perform the test on data in Period −

 Live Demo

First<-sample(1:100,20)
Second<-sample(1:100,20)
Third<-sample(1:100,20)
Fourth<-sample(1:100,20)
Period<-matrix(c(First,Second,Third,Fourth),byrow=FALSE,nrow=20)
friedman.test(Period)

Output

Friedman rank sum test
data: Period
Friedman chi-squared = 0.19797, df = 3, p-value = 0.9779

Example2

Create the matrix

Let's create a data matrix as shown below −

 Live Demo

Jan<-round(rnorm(20),2)
Feb<-round(rnorm(20),2)
Mar<-round(rnorm(20),2)
Apr<-round(rnorm(20),2)
May<-round(rnorm(20),2)
Time<-matrix(c(Jan,Feb,Mar,Apr,May),byrow=FALSE,nrow=20)
Time

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

Output

    [,1] [,2] [,3] [,4] [,5]
[1,] 1.30 -1.21 -0.26 0.94 1.19
[2,] 1.14 -0.77 -1.54 0.67 1.77
[3,] 0.18 -0.50 0.72 0.76 -1.58
[4,] 1.51 0.13 2.71 0.51 0.62
[5,] -1.10 0.09 0.47 0.25 0.26
[6,] 1.31 0.51 -1.79 -1.67 0.32
[7,] 0.00 0.59 0.20 0.55 -0.20
[8,] 1.25 -0.91 -1.34 1.33 0.07
[9,] -0.19 -1.39 0.09 -2.08 -1.63
[10,] 0.66 1.17 -0.66 -0.02 -0.07
[11,] 0.05 -1.31 0.55 0.68 0.97
[12,] 1.57 -2.80 1.00 -1.55 -1.90
[13,] -0.67 0.29 0.60 2.28 1.34
[14,] 0.43 -3.66 -0.23 0.41 0.69
[15,] -0.56 0.11 0.60 1.09 -1.34
[16,] 1.87 0.48 0.49 -1.00 0.91
[17,] 1.01 0.64 0.74 -0.88 1.20
[18,] 0.90 -0.43 -0.61 0.27 0.10
[19,] 1.98 -0.54 1.02 0.89 -0.52
[20,] 0.11 -1.29 2.31 -0.18 1.41

Perform Friedman test

Using friedman.test function to perform the test on data in Period −

 Live Demo

Jan<-round(rnorm(20),2)
Feb<-round(rnorm(20),2)
Mar<-round(rnorm(20),2)
Apr<-round(rnorm(20),2)
May<-round(rnorm(20),2)
Time<-matrix(c(Jan,Feb,Mar,Apr,May),byrow=FALSE,nrow=20)
friedman.test(Time)

Output

Friedman rank sum test
data: Time
Friedman chi-squared = 8.92, df = 4, p-value = 0.06313

Updated on: 14-Aug-2021

261 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements