One Factor Nonparametric Tests - R#

Mann-Whitney U test#

  • Samples: 1

  • Levels: 2

  • Between or Within Subjects: Between

  • Reporting: “The median of ‘a’ was 13.26 (IQR = 5.01) and of ‘b’ was 14.98 (IQR = 6.19). This difference was statistically significant according to a Mann-Whitney U test (Z = -2.44, p < .05).”

# Example data
# df has subjects (S), one between-Ss factor (X) w/levels (a,b), and continuous response (Y)
df <- read.csv("data/1F2LBs.csv")
head(df, 20)
A data.frame: 20 × 3
SXY
<int><chr><dbl>
1 1a14.312761
2 2b16.326529
3 3a 8.113886
4 4b14.340093
5 5a13.245522
6 6b17.588067
7 7a 8.889459
8 8b10.021004
9 9a13.282139
1010b10.186564
1111a14.295129
1212b 9.358157
1313a 8.716243
1414b10.497703
1515a15.976036
1616b15.436113
1717a13.384940
1818b15.940214
1919a15.292024
2020b11.479020
library(coin) # for wilcox_test
df$S = factor(df$S) # Subject id is nominal (unused)
df$X = factor(df$X) # X is a 2-level factor
wilcox_test(Y ~ X, data=df, distribution="exact")
Loading required package: survival
	Exact Wilcoxon-Mann-Whitney Test

data:  Y by X (a, b)
Z = -2.4394, p-value = 0.01425
alternative hypothesis: true mu is not equal to 0

Wilcoxon Signed-Rank Test#

  • Samples: 1

  • Levels: 2

  • Between or Within Subjects: Within

  • Reporting: “The median of ‘a’ was 11.92 (IQR = 2.35) and of ‘b’ was 10.74 (IQR = 2.29). This difference was statistically significant according to a Wilcoxon signed-rank test (Z = 2.11, p < .05).”

# Example data
# df has subjects (S), one within-Ss factor (X) w/levels (a,b), and continuous response (Y)
df <- read.csv("data/1F2LWs.csv")
head(df, 20)
A data.frame: 20 × 3
SXY
<int><chr><dbl>
1 1a11.479751
2 1b10.860802
3 2a13.763133
4 2b 9.609735
5 3a12.058958
6 3b 9.229486
7 4a12.173675
8 4b12.112978
9 5a10.017150
10 5b10.366385
11 6a12.709974
12 6b11.641991
13 7a11.254307
14 7b 9.541414
15 8a13.498748
16 8b 8.973836
17 9a13.015273
18 9b 8.015089
1910a13.549128
2010b 9.171968
library(coin)
df$S = factor(df$S) # Subject id is nominal
df$X = factor(df$X) # X is a 2-level factor
wilcoxsign_test(Y ~ X | S, data=df, distribution="exact")
	Exact Wilcoxon-Pratt Signed-Rank Test

data:  y by x (pos, neg) 
	 stratified by block
Z = 2.1083, p-value = 0.03454
alternative hypothesis: true mu is not equal to 0

Kruskal-Wallis Test#

  • Samples: 1

  • Levels: ≥2

  • Between or Within Subjects: Between

  • Reporting: “The median of ‘a’ was 13.96 (IQR = 5.98), of ‘b’ was 11.50 (IQR = 3.49), and of ‘c’ was 9.62 (IQR = 1.61). These differences were statistically significant according to a Kruskal-Wallis test (χ2(2, N=60) = 17.77, p < .001).”

# Example data
# df has subjects (S), one between-Ss factor (X) w/levels (a,b,c), and continuous response (Y)
df <- read.csv("data/1F3LBs.csv")
head(df, 20)
A data.frame: 20 × 3
SXY
<int><chr><dbl>
1 1a16.906062
2 2b11.583856
3 3c 9.821207
4 4a 9.983235
5 5b12.259763
6 6c13.224217
7 7a16.168297
8 8b12.346154
9 9c10.083903
1010a12.522738
1111b10.437293
1212c 7.849846
1313a14.581631
1414b12.862453
1515c 9.060727
1616a 9.713943
1717b11.409560
1818c 9.709308
1919a16.478419
2020b 7.699005
library(coin)
df$S = factor(df$S) # Subject id is nominal (unused)
df$X = factor(df$X) # X is a 3-level factor
kruskal_test(Y ~ X, data=df, distribution="asymptotic")
	Asymptotic Kruskal-Wallis Test

data:  Y by X (a, b, c)
chi-squared = 17.767, df = 2, p-value = 0.0001387

Friedman Test#

  • Samples: 1

  • Levels: ≥2

  • Between or Within Subjects: Within

  • Reporting: “The median of ‘a’ was 8.37 (IQR = 1.71), of ‘b’ was 8.97 (IQR = 2.21), and of ‘c’ was 9.97 (IQR = 3.56). These differences were statistically significant according to a Friedman test (χ2(2, N=60) = 7.90, p < .05).”

# Example data
# df has subjects (S), one within-Ss factor (X) w/levels (a,b,c), and continuous response (Y)
df <- read.csv("data/1F3LWs.csv")
head(df, 20)
A data.frame: 20 × 3
SXY
<int><chr><dbl>
11a 8.133783
21b 8.862093
31c 8.941638
42a 8.737678
52b 9.969701
62c12.963951
73a 7.952228
83b 7.105104
93c 9.303468
104a11.941388
114b 8.251948
124c 9.893744
135a 8.847477
145b 7.753521
155c12.373811
166a 8.096942
176b10.440534
186c 8.758131
197a 9.574017
207b 9.172273
library(coin)
df$S = factor(df$S) # Subject id is nominal
df$X = factor(df$X) # X is a 3-level factor
friedman_test(Y ~ X | S, data=df, distribution="asymptotic")
	Asymptotic Friedman Test

data:  Y by X (a, b, c) 
	 stratified by S
chi-squared = 7.9, df = 2, p-value = 0.01925