Exercise answers for the Friedman test

EX1

library(readr)
rat <- read_csv("E:/XJTLU/3/S2/ENV222/Friedman test/rats.csv")
library(tidyr)
longrat <- gather(rat, Temp, Speed, -Animal)

friedman.test(longrat$Speed, longrat$Temp, longrat$Animal)
## 
##  Friedman rank sum test
## 
## data:  longrat$Speed, longrat$Temp and longrat$Animal
## Friedman chi-squared = 21.95, df = 4, p-value = 0.0002051
pairwise.wilcox.test(longrat$Speed, longrat$Temp, 
                     p.adj="none", 
                     paired=TRUE, 
                     exact=FALSE, 
                     correct=FALSE)
## 
##  Pairwise comparisons using Wilcoxon signed rank test 
## 
## data:  longrat$Speed and longrat$Temp 
## 
##      ...1  X20C  X24C  X27C 
## X20C 0.028 -     -     -    
## X24C 0.028 0.453 -     -    
## X27C 0.028 0.026 0.027 -    
## X32C 0.028 0.027 0.026 0.336
## 
## P value adjustment method: none
summary(rat)
##       ...1          Animal          X20C            X24C           X27C      
##  Min.   :1.00   Min.   :1.00   Min.   :25.00   Min.   :25.0   Min.   :18.00  
##  1st Qu.:2.25   1st Qu.:2.25   1st Qu.:29.50   1st Qu.:27.5   1st Qu.:21.00  
##  Median :3.50   Median :3.50   Median :32.50   Median :31.0   Median :26.50  
##  Mean   :3.50   Mean   :3.50   Mean   :32.33   Mean   :31.5   Mean   :25.83  
##  3rd Qu.:4.75   3rd Qu.:4.75   3rd Qu.:35.50   3rd Qu.:36.0   3rd Qu.:29.75  
##  Max.   :6.00   Max.   :6.00   Max.   :39.00   Max.   :38.0   Max.   :34.00  
##       X32C      
##  Min.   :19.00  
##  1st Qu.:20.50  
##  Median :23.00  
##  Mean   :24.83  
##  3rd Qu.:29.25  
##  Max.   :33.00
#graph
library(ggplot2)
ggplot(longrat)+
  geom_point(aes(y=Speed,x=Temp,fill=Temp,shape=Temp,color=Temp))+
  geom_hline(aes(yintercept=32.33),color="red",linetype="dashed")+
  geom_hline(aes(yintercept=31.5),color="green",linetype="dashed")+
  geom_hline(aes(yintercept=25.83),color="blue",linetype="dashed")+
  geom_hline(aes(yintercept=24.83),color="purple",linetype="dashed")

#ANOVA
shapiro.test(rat$X20C)
## 
##  Shapiro-Wilk normality test
## 
## data:  rat$X20C
## W = 0.99065, p-value = 0.9907
shapiro.test(rat$X24C)
## 
##  Shapiro-Wilk normality test
## 
## data:  rat$X24C
## W = 0.92461, p-value = 0.5392
shapiro.test(rat$X27C)
## 
##  Shapiro-Wilk normality test
## 
## data:  rat$X27C
## W = 0.95015, p-value = 0.7415
shapiro.test(rat$X32C)
## 
##  Shapiro-Wilk normality test
## 
## data:  rat$X32C
## W = 0.8807, p-value = 0.2723
longrat$Animal <- as.factor(longrat$Animal)
longrat$Temp <- as.factor(longrat$Temp)
longrat$Speed <- as.numeric(longrat$Speed)

str(longrat)
## tibble [30 x 3] (S3: tbl_df/tbl/data.frame)
##  $ Animal: Factor w/ 6 levels "1","2","3","4",..: 1 2 3 4 5 6 1 2 3 4 ...
##  $ Temp  : Factor w/ 5 levels "...1","X20C",..: 1 1 1 1 1 1 2 2 2 2 ...
##  $ Speed : num [1:30] 1 2 3 4 5 6 39 29 36 25 ...
attach(longrat)
library(ez)
model <- ezANOVA(longrat, dv = Speed, wid = Animal, within = Temp, type = 2,detailed = T)
model
## $ANOVA
##        Effect DFn DFd     SSn SSd         F            p p<.05       ges
## 1 (Intercept)   1   5 16708.8 440 189.87273 3.613664e-05     * 0.9624441
## 2        Temp   4  20  3295.2 212  77.71698 6.770175e-12     * 0.8348196
## 
## $`Mauchly's Test for Sphericity`
##   Effect          W         p p<.05
## 2   Temp 0.03552377 0.3019756      
## 
## $`Sphericity Corrections`
##   Effect       GGe        p[GG] p[GG]<.05       HFe       p[HF] p[HF]<.05
## 2   Temp 0.4663298 1.787575e-06         * 0.7330824 3.39521e-09         *

EX2

cancer <- read_csv("E:/XJTLU/3/S2/ENV222/Friedman test/cancer.csv")
longcancer <- gather(cancer, time, PSA, -Patient_no)

friedman.test(longcancer$PSA, longcancer$time, longcancer$Patient_no)
## 
##  Friedman rank sum test
## 
## data:  longcancer$PSA, longcancer$time and longcancer$Patient_no
## Friedman chi-squared = 4.0787, df = 3, p-value = 0.2531
pairwise.wilcox.test(longcancer$PSA, longcancer$time, 
                     p.adj="none", 
                     paired=TRUE, 
                     exact=FALSE, 
                     correct=FALSE)
## 
##  Pairwise comparisons using Wilcoxon signed rank test 
## 
## data:  longcancer$PSA and longcancer$time 
## 
##                  ...1  First_PSA_ng_ml Second_PSA_ng_ml
## First_PSA_ng_ml  0.066 -               -               
## Second_PSA_ng_ml 0.110 0.214           -               
## Third_PSA_ng_ml  0.374 0.374           0.327           
## 
## P value adjustment method: none
summary(cancer)
##       ...1     Patient_no First_PSA_ng_ml Second_PSA_ng_ml Third_PSA_ng_ml 
##  Min.   :1   Min.   :1    Min.   :0.620   Min.   :0.300    Min.   : 0.100  
##  1st Qu.:3   1st Qu.:3    1st Qu.:1.800   1st Qu.:1.670    1st Qu.: 0.420  
##  Median :5   Median :5    Median :1.800   Median :2.100    Median : 2.540  
##  Mean   :5   Mean   :5    Mean   :2.424   Mean   :2.947    Mean   : 3.604  
##  3rd Qu.:7   3rd Qu.:7    3rd Qu.:3.700   3rd Qu.:3.950    3rd Qu.: 4.950  
##  Max.   :9   Max.   :9    Max.   :3.900   Max.   :7.680    Max.   :13.500
#graph
library(ggplot2)
ggplot(longcancer)+
  geom_point(aes(y=PSA,x=time,fill=time,shape=time,color=time))+
  geom_hline(aes(yintercept=2.424),color="red",linetype="dashed")+
  geom_hline(aes(yintercept=2.947),color="green",linetype="dashed")+
  geom_hline(aes(yintercept=3.604),color="blue",linetype="dashed")

#ANOVA
shapiro.test(cancer$First_PSA_ng_ml)
## 
##  Shapiro-Wilk normality test
## 
## data:  cancer$First_PSA_ng_ml
## W = 0.84226, p-value = 0.06119
shapiro.test(cancer$Second_PSA_ng_ml)
## 
##  Shapiro-Wilk normality test
## 
## data:  cancer$Second_PSA_ng_ml
## W = 0.91631, p-value = 0.3626
shapiro.test(cancer$Third_PSA_ng_ml)
## 
##  Shapiro-Wilk normality test
## 
## data:  cancer$Third_PSA_ng_ml
## W = 0.80817, p-value = 0.0253
longcancer$Patient_no <- as.factor(longcancer$Patient_no)
longcancer$time <- as.factor(longcancer$time)
longcancer$PSA <- as.numeric(longcancer$PSA)

str(longcancer)
## tibble [36 x 3] (S3: tbl_df/tbl/data.frame)
##  $ Patient_no: Factor w/ 9 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 1 ...
##  $ time      : Factor w/ 4 levels "...1","First_PSA_ng_ml",..: 1 1 1 1 1 1 1 1 1 2 ...
##  $ PSA       : num [1:36] 1 2 3 4 5 6 7 8 9 3.9 ...
attach(longcancer)
library(ez)
model <- ezANOVA(longcancer, dv = PSA, wid = Patient_no, within = time, type = 2, detailed = T)
model
## $ANOVA
##        Effect DFn DFd       SSn      SSd         F            p p<.05       ges
## 1 (Intercept)   1   8 439.46134 116.8805 30.079372 0.0005843641     * 0.6201755
## 2        time   3  24  33.51381 152.2662  1.760801 0.1815276068       0.1107307
## 
## $`Mauchly's Test for Sphericity`
##   Effect          W           p p<.05
## 2   time 0.04575848 0.001044556     *
## 
## $`Sphericity Corrections`
##   Effect       GGe    p[GG] p[GG]<.05       HFe     p[HF] p[HF]<.05
## 2   time 0.5523613 0.210953           0.6786445 0.2028351