One Factor Nonparametric Tests - R
Contents
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)
S | X | Y | |
---|---|---|---|
<int> | <chr> | <dbl> | |
1 | 1 | a | 14.312761 |
2 | 2 | b | 16.326529 |
3 | 3 | a | 8.113886 |
4 | 4 | b | 14.340093 |
5 | 5 | a | 13.245522 |
6 | 6 | b | 17.588067 |
7 | 7 | a | 8.889459 |
8 | 8 | b | 10.021004 |
9 | 9 | a | 13.282139 |
10 | 10 | b | 10.186564 |
11 | 11 | a | 14.295129 |
12 | 12 | b | 9.358157 |
13 | 13 | a | 8.716243 |
14 | 14 | b | 10.497703 |
15 | 15 | a | 15.976036 |
16 | 16 | b | 15.436113 |
17 | 17 | a | 13.384940 |
18 | 18 | b | 15.940214 |
19 | 19 | a | 15.292024 |
20 | 20 | b | 11.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)
S | X | Y | |
---|---|---|---|
<int> | <chr> | <dbl> | |
1 | 1 | a | 11.479751 |
2 | 1 | b | 10.860802 |
3 | 2 | a | 13.763133 |
4 | 2 | b | 9.609735 |
5 | 3 | a | 12.058958 |
6 | 3 | b | 9.229486 |
7 | 4 | a | 12.173675 |
8 | 4 | b | 12.112978 |
9 | 5 | a | 10.017150 |
10 | 5 | b | 10.366385 |
11 | 6 | a | 12.709974 |
12 | 6 | b | 11.641991 |
13 | 7 | a | 11.254307 |
14 | 7 | b | 9.541414 |
15 | 8 | a | 13.498748 |
16 | 8 | b | 8.973836 |
17 | 9 | a | 13.015273 |
18 | 9 | b | 8.015089 |
19 | 10 | a | 13.549128 |
20 | 10 | b | 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)
S | X | Y | |
---|---|---|---|
<int> | <chr> | <dbl> | |
1 | 1 | a | 16.906062 |
2 | 2 | b | 11.583856 |
3 | 3 | c | 9.821207 |
4 | 4 | a | 9.983235 |
5 | 5 | b | 12.259763 |
6 | 6 | c | 13.224217 |
7 | 7 | a | 16.168297 |
8 | 8 | b | 12.346154 |
9 | 9 | c | 10.083903 |
10 | 10 | a | 12.522738 |
11 | 11 | b | 10.437293 |
12 | 12 | c | 7.849846 |
13 | 13 | a | 14.581631 |
14 | 14 | b | 12.862453 |
15 | 15 | c | 9.060727 |
16 | 16 | a | 9.713943 |
17 | 17 | b | 11.409560 |
18 | 18 | c | 9.709308 |
19 | 19 | a | 16.478419 |
20 | 20 | b | 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)
S | X | Y | |
---|---|---|---|
<int> | <chr> | <dbl> | |
1 | 1 | a | 8.133783 |
2 | 1 | b | 8.862093 |
3 | 1 | c | 8.941638 |
4 | 2 | a | 8.737678 |
5 | 2 | b | 9.969701 |
6 | 2 | c | 12.963951 |
7 | 3 | a | 7.952228 |
8 | 3 | b | 7.105104 |
9 | 3 | c | 9.303468 |
10 | 4 | a | 11.941388 |
11 | 4 | b | 8.251948 |
12 | 4 | c | 9.893744 |
13 | 5 | a | 8.847477 |
14 | 5 | b | 7.753521 |
15 | 5 | c | 12.373811 |
16 | 6 | a | 8.096942 |
17 | 6 | b | 10.440534 |
18 | 6 | c | 8.758131 |
19 | 7 | a | 9.574017 |
20 | 7 | b | 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