Statistical test

検定の種類と選択方法 サンプルデータ 利用関数のヘルプ

検定の種類と選択方法
No. 検定 Type1 Type2 Type3 Type4
1 1標本t検定 平均値・代表値 パラメトリック検定 母平均の検定 ***
2 2標本t検定(対応なし) 平均値・代表値 パラメトリック検定 2群の平均値の差の検定 対応のない場合
3 2標本t検定(対応あり) 平均値・代表値 パラメトリック検定 2群の平均値の差の検定 対応のある場合
4 1元配置分散分析(対応なし) 平均値・代表値 パラメトリック検定 3群以上の平均値の差の検定 1要因
5 1元配置分散分析(対応あり) 平均値・代表値 パラメトリック検定 3群以上の平均値の差の検定 1要因
6 2元配置分散分析(対応なし) 平均値・代表値 パラメトリック検定 3群以上の平均値の差の検定 2要因
7 2元配置分散分析(1要因のみ対応あり) 平均値・代表値 パラメトリック検定 3群以上の平均値の差の検定 2要因
8 2元配置分散分析(2要因とも対応あり) 平均値・代表値 パラメトリック検定 3群以上の平均値の差の検定 2要因
9 多重比較 平均値・代表値 パラメトリック検定 3群以上の平均値の差の検定 各要因水準間の比較
10 ウィルコクソンの順位和検定 平均値・代表値 ノンパラメトリック検定 2群の代表値の差の検定 対応のない場合
11 ウィルコクソンの符号付順位検定 平均値・代表値 ノンパラメトリック検定 2群の代表値の差の検定 対応のある場合
12 クラスカルウォーリス検定 平均値・代表値 ノンパラメトリック検定 3群以上の代表値の差の検定 対応のない場合
13 フリードマン検定 平均値・代表値 ノンパラメトリック検定 3群以上の代表値の差の検定 対応のある場合
14 母比率の検定 比率 *** 母比率の検定 ***
15 母比率の検定(2項検定) 比率 *** 母比率の検定 ***
16 フィッシャーの正確確率検定 比率 *** 2群の比率の差の検定 対応のない場合
17 マクネマー検定 比率 *** 2群の比率の差の検定 対応のある場合
18 フィッシャーの正確確率検定 比率 *** 3群以上の比率の差の検定 対応のない場合
19 コクランのQ検定 比率 *** 3群以上の比率の差の検定 対応のある場合(2値型変数)
20 F検定 分散比 *** 2群の分散比 ***
21 バートレットの検定 分散比 *** 3群以上の分散比 ***
22 ルービンの検定 分散比 *** 3群以上の分散比 ***
23 Brunner-Munzel検定/ブルンナー=ムンツェル検定 平均値・代表値 ノンパラメトリック検定 2群の代表値の差の検定 対応のない場合

サンプルデータ
fx1 <- factor(c(rep("Blue", 30), rep("Red", 30)))
fx2 <- factor(rep(c(rep("A", 10), rep("B", 10), rep("C", 10)), 2))
y <- round(c(rnorm(n = 30, mean = 50, sd = 5), rnorm(n = 30, mean = 35, sd = 10)), 1)
id <- factor(rep(seq(10), 6))
paired <- data.frame(fx1, fx2, id, y)
[1] "paired"
    fx1 fx2 id    y
1  Blue   A  1 48.0
2  Blue   A  2 46.9
3  Blue   A  3 50.1
4  Blue   A  4 55.1
5  Blue   A  5 46.4
6  Blue   A  6 48.5
7  Blue   A  7 43.8
8  Blue   A  8 53.6
9  Blue   A  9 53.8
10 Blue   A 10 47.6
11 Blue   B  1 52.3
12 Blue   B  2 55.8
13 Blue   B  3 52.6
14 Blue   B  4 45.2
15 Blue   B  5 42.5
16 Blue   B  6 53.6
17 Blue   B  7 50.7
18 Blue   B  8 54.6
19 Blue   B  9 64.9
20 Blue   B 10 46.5
21 Blue   C  1 51.8
22 Blue   C  2 61.3
23 Blue   C  3 52.3
24 Blue   C  4 44.3
25 Blue   C  5 41.1
26 Blue   C  6 51.0
27 Blue   C  7 48.6
28 Blue   C  8 50.9
29 Blue   C  9 44.3
30 Blue   C 10 51.8
31  Red   A  1 45.4
32  Red   A  2 42.0
33  Red   A  3 36.0
34  Red   A  4 52.8
35  Red   A  5 55.0
36  Red   A  6 26.0
37  Red   A  7 39.2
38  Red   A  8 35.9
39  Red   A  9 37.1
40  Red   A 10 33.3
41  Red   B  1 50.9
42  Red   B  2 45.8
43  Red   B  3 37.6
44  Red   B  4 44.3
45  Red   B  5 40.8
46  Red   B  6 40.6
47  Red   B  7 22.6
48  Red   B  8 22.0
49  Red   B  9 24.4
50  Red   B 10 23.1
51  Red   C  1 57.1
52  Red   C  2 46.9
53  Red   C  3 22.2
54  Red   C  4 52.8
55  Red   C  5 35.6
56  Red   C  6 46.6
57  Red   C  7 29.8
58  Red   C  8 32.6
59  Red   C  9 41.5
60  Red   C 10 25.3
fx1 <- factor(c(rep("Blue", 40), rep("Red", 20)))
fx2 <- factor(c(rep("A", 14), rep("B", 25), rep("C", 21)))
y <- round(c(rnorm(n = 35, mean = 50, sd = 3), rnorm(n = 25, mean = 35, sd = 7)), 1)
id <- factor(seq(60))
unpaired <- data.frame(fx1, fx2, id, y)
[1] "unpaired"
    fx1 fx2 id    y
1  Blue   A  1 46.1
2  Blue   A  2 51.7
3  Blue   A  3 51.5
4  Blue   A  4 54.1
5  Blue   A  5 49.5
6  Blue   A  6 51.1
7  Blue   A  7 52.2
8  Blue   A  8 51.1
9  Blue   A  9 53.7
10 Blue   A 10 50.2
11 Blue   A 11 51.6
12 Blue   A 12 53.8
13 Blue   A 13 51.5
14 Blue   A 14 53.3
15 Blue   B 15 52.4
16 Blue   B 16 50.1
17 Blue   B 17 47.3
18 Blue   B 18 48.9
19 Blue   B 19 49.0
20 Blue   B 20 50.3
21 Blue   B 21 47.9
22 Blue   B 22 46.0
23 Blue   B 23 49.8
24 Blue   B 24 48.8
25 Blue   B 25 49.3
26 Blue   B 26 49.5
27 Blue   B 27 47.7
28 Blue   B 28 53.0
29 Blue   B 29 55.7
30 Blue   B 30 48.0
31 Blue   B 31 48.7
32 Blue   B 32 49.0
33 Blue   B 33 48.4
34 Blue   B 34 46.9
35 Blue   B 35 49.8
36 Blue   B 36 33.3
37 Blue   B 37 38.0
38 Blue   B 38 37.4
39 Blue   B 39 30.7
40 Blue   C 40 26.1
41  Red   C 41 25.9
42  Red   C 42 32.8
43  Red   C 43 40.9
44  Red   C 44 39.8
45  Red   C 45 44.6
46  Red   C 46 39.8
47  Red   C 47 48.4
48  Red   C 48 23.1
49  Red   C 49 42.1
50  Red   C 50 33.7
51  Red   C 51 42.9
52  Red   C 52 36.5
53  Red   C 53 32.4
54  Red   C 54 17.9
55  Red   C 55 42.3
56  Red   C 56 35.6
57  Red   C 57 38.9
58  Red   C 58 31.4
59  Red   C 59 20.4
60  Red   C 60 44.4

平均値・代表値:パラメトリック検定:母平均の検定:1標本t検定
x1 <- paired %>% filter(fx1 == "Blue" & fx2 == "A") %>% pull(y)
x1
x2 <- paired %>% filter(fx1 == "Red" & fx2 == "A") %>% pull(y)
x2
t.test(x1, mu = 50, alternative = "two")
t.test(x2, mu = 50, alternative = "less")
t.test(x2, mu = 50, alternative = "greater")
 [1] 48.0 46.9 50.1 55.1 46.4 48.5 43.8 53.6 53.8 47.6
 [1] 45.4 42.0 36.0 52.8 55.0 26.0 39.2 35.9 37.1 33.3

    One Sample t-test

data:  x1
t = -0.53122, df = 9, p-value = 0.6081
alternative hypothesis: true mean is not equal to 50
95 percent confidence interval:
 46.73978 52.02022
sample estimates:
mean of x 
    49.38 


    One Sample t-test

data:  x2
t = -3.4789, df = 9, p-value = 0.003475
alternative hypothesis: true mean is less than 50
95 percent confidence interval:
     -Inf 45.39697
sample estimates:
mean of x 
    40.27 


    One Sample t-test

data:  x2
t = -3.4789, df = 9, p-value = 0.9965
alternative hypothesis: true mean is greater than 50
95 percent confidence interval:
 35.14303      Inf
sample estimates:
mean of x 
    40.27 

平均値・代表値:パラメトリック検定:2群の平均値の差の検定:対応のない場合:2標本t検定(対応なし)
x1 <- unpaired %>% filter(id %in% factor(1:10)) %>% pull(y)
x2 <- unpaired %>% filter(id %in% factor(11:30)) %>% pull(y)
[1] "x1"
 [1] 46.1 51.7 51.5 54.1 49.5 51.1 52.2 51.1 53.7 50.2
[1] "x2"
 [1] 51.6 53.8 51.5 53.3 52.4 50.1 47.3 48.9 49.0 50.3 47.9 46.0 49.8 48.8 49.3 49.5 47.7 53.0 55.7 48.0
  • 等分散を仮定する場合
t.test(x = x1, y = x2, mu = 0, alternative = "two", paired = F, var.equal = T)

    Two Sample t-test

data:  x1 and x2
t = 0.98684, df = 28, p-value = 0.3322
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.9950481  2.8450481
sample estimates:
mean of x mean of y 
   51.120    50.195 
  • 等分散を仮定しない場合
t.test(x = x1, y = x2, mu = 0, alternative = "two", paired = F, var.equal = F)

    Welch Two Sample t-test

data:  x1 and x2
t = 1.0212, df = 19.837, p-value = 0.3194
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.9654147  2.8154147
sample estimates:
mean of x mean of y 
   51.120    50.195 

平均値・代表値:パラメトリック検定:2群の平均値の差の検定:対応のある場合:2標本t検定(対応あり)
x1 <- paired %>% filter(fx1 == "Blue", fx2 == "A", id %in% factor(1:10)) %>% pull(y)
x2 <- paired %>% filter(fx1 == "Red", fx2 == "A", id %in% factor(1:10)) %>% pull(y)
[1] "x1"
 [1] 48.0 46.9 50.1 55.1 46.4 48.5 43.8 53.6 53.8 47.6
[1] "x2"
 [1] 45.4 42.0 36.0 52.8 55.0 26.0 39.2 35.9 37.1 33.3
  • 等分散を仮定する場合(*結果は等分散仮定の引数(var.equal)に依らない)
t.test(x = x1, y = x2, mu = 0, alternative = "two", paired = T, var.equal = T)

    Paired t-test

data:  x1 and x2
t = 3.0499, df = 9, p-value = 0.0138
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
  2.352963 15.867037
sample estimates:
mean of the differences 
                   9.11 
  • 等分散を仮定しない場合(*結果は等分散仮定の引数(var.equal)に依らない)
t.test(x = x1, y = x2, mu = 0, alternative = "two", paired = T, var.equal = F)

    Paired t-test

data:  x1 and x2
t = 3.0499, df = 9, p-value = 0.0138
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
  2.352963 15.867037
sample estimates:
mean of the differences 
                   9.11 

平均値・代表値:パラメトリック検定:3群以上の平均値の差の検定:1要因:1元配置分散分析(対応なし)
sampledata <- unpaired
[1] "sampledata"
    fx1 fx2 id    y
1  Blue   A  1 46.1
2  Blue   A  2 51.7
3  Blue   A  3 51.5
4  Blue   A  4 54.1
5  Blue   A  5 49.5
6  Blue   A  6 51.1
7  Blue   A  7 52.2
8  Blue   A  8 51.1
9  Blue   A  9 53.7
10 Blue   A 10 50.2
11 Blue   A 11 51.6
12 Blue   A 12 53.8
13 Blue   A 13 51.5
14 Blue   A 14 53.3
15 Blue   B 15 52.4
16 Blue   B 16 50.1
17 Blue   B 17 47.3
18 Blue   B 18 48.9
19 Blue   B 19 49.0
20 Blue   B 20 50.3
21 Blue   B 21 47.9
22 Blue   B 22 46.0
23 Blue   B 23 49.8
24 Blue   B 24 48.8
25 Blue   B 25 49.3
26 Blue   B 26 49.5
27 Blue   B 27 47.7
28 Blue   B 28 53.0
29 Blue   B 29 55.7
30 Blue   B 30 48.0
31 Blue   B 31 48.7
32 Blue   B 32 49.0
33 Blue   B 33 48.4
34 Blue   B 34 46.9
35 Blue   B 35 49.8
36 Blue   B 36 33.3
37 Blue   B 37 38.0
38 Blue   B 38 37.4
39 Blue   B 39 30.7
40 Blue   C 40 26.1
41  Red   C 41 25.9
42  Red   C 42 32.8
43  Red   C 43 40.9
44  Red   C 44 39.8
45  Red   C 45 44.6
46  Red   C 46 39.8
47  Red   C 47 48.4
48  Red   C 48 23.1
49  Red   C 49 42.1
50  Red   C 50 33.7
51  Red   C 51 42.9
52  Red   C 52 36.5
53  Red   C 53 32.4
54  Red   C 54 17.9
55  Red   C 55 42.3
56  Red   C 56 35.6
57  Red   C 57 38.9
58  Red   C 58 31.4
59  Red   C 59 20.4
60  Red   C 60 44.4
anova(aov(data = sampledata, formula = y ~ fx2))
Analysis of Variance Table

Response: y
          Df Sum Sq Mean Sq F value    Pr(>F)    
fx2        2 2637.8 1318.88  31.946 4.944e-10 ***
Residuals 57 2353.2   41.28                      
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  • \(H_{0}:\) 要因 \(fx_{2}\) (3 群)の主効果はない(平均値間に差はない)。

平均値・代表値:パラメトリック検定:3群以上の平均値の差の検定:1要因:1元配置分散分析(対応あり)
sampledata <- paired %>% filter(fx1 == "Blue")
[1] "sampledata"
    fx1 fx2 id    y
1  Blue   A  1 48.0
2  Blue   A  2 46.9
3  Blue   A  3 50.1
4  Blue   A  4 55.1
5  Blue   A  5 46.4
6  Blue   A  6 48.5
7  Blue   A  7 43.8
8  Blue   A  8 53.6
9  Blue   A  9 53.8
10 Blue   A 10 47.6
11 Blue   B  1 52.3
12 Blue   B  2 55.8
13 Blue   B  3 52.6
14 Blue   B  4 45.2
15 Blue   B  5 42.5
16 Blue   B  6 53.6
17 Blue   B  7 50.7
18 Blue   B  8 54.6
19 Blue   B  9 64.9
20 Blue   B 10 46.5
21 Blue   C  1 51.8
22 Blue   C  2 61.3
23 Blue   C  3 52.3
24 Blue   C  4 44.3
25 Blue   C  5 41.1
26 Blue   C  6 51.0
27 Blue   C  7 48.6
28 Blue   C  8 50.9
29 Blue   C  9 44.3
30 Blue   C 10 51.8
anova(aov(data = sampledata, formula = y ~ fx2 + id))
Analysis of Variance Table

Response: y
          Df Sum Sq Mean Sq F value Pr(>F)
fx2        2  36.22  18.111  0.7331 0.4942
id         9 323.54  35.948  1.4551 0.2377
Residuals 18 444.68  24.705               
  • \(H_{0}:\) 要因 \(fx_{2}\) (3 群)の主効果はない(平均値間に差はない)。

平均値・代表値:パラメトリック検定:3群以上の平均値の差の検定:2要因:2元配置分散分析(対応なし)
sampledata <- unpaired
[1] "sampledata"
    fx1 fx2 id    y
1  Blue   A  1 46.1
2  Blue   A  2 51.7
3  Blue   A  3 51.5
4  Blue   A  4 54.1
5  Blue   A  5 49.5
6  Blue   A  6 51.1
7  Blue   A  7 52.2
8  Blue   A  8 51.1
9  Blue   A  9 53.7
10 Blue   A 10 50.2
11 Blue   A 11 51.6
12 Blue   A 12 53.8
13 Blue   A 13 51.5
14 Blue   A 14 53.3
15 Blue   B 15 52.4
16 Blue   B 16 50.1
17 Blue   B 17 47.3
18 Blue   B 18 48.9
19 Blue   B 19 49.0
20 Blue   B 20 50.3
21 Blue   B 21 47.9
22 Blue   B 22 46.0
23 Blue   B 23 49.8
24 Blue   B 24 48.8
25 Blue   B 25 49.3
26 Blue   B 26 49.5
27 Blue   B 27 47.7
28 Blue   B 28 53.0
29 Blue   B 29 55.7
30 Blue   B 30 48.0
31 Blue   B 31 48.7
32 Blue   B 32 49.0
33 Blue   B 33 48.4
34 Blue   B 34 46.9
35 Blue   B 35 49.8
36 Blue   B 36 33.3
37 Blue   B 37 38.0
38 Blue   B 38 37.4
39 Blue   B 39 30.7
40 Blue   C 40 26.1
41  Red   C 41 25.9
42  Red   C 42 32.8
43  Red   C 43 40.9
44  Red   C 44 39.8
45  Red   C 45 44.6
46  Red   C 46 39.8
47  Red   C 47 48.4
48  Red   C 48 23.1
49  Red   C 49 42.1
50  Red   C 50 33.7
51  Red   C 51 42.9
52  Red   C 52 36.5
53  Red   C 53 32.4
54  Red   C 54 17.9
55  Red   C 55 42.3
56  Red   C 56 35.6
57  Red   C 57 38.9
58  Red   C 58 31.4
59  Red   C 59 20.4
60  Red   C 60 44.4
anova(aov(data = sampledata, formula = y ~ fx1 * fx2))
Analysis of Variance Table

Response: y
          Df  Sum Sq Mean Sq F value    Pr(>F)    
fx1        1 2048.48 2048.48 50.6328 2.229e-09 ***
fx2        2  676.86  338.43  8.3651 0.0006626 ***
Residuals 56 2265.62   40.46                      
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  • \(H_{0}:\) 要因 \(fx_{1}\) (2 群)の主効果はない(平均値間に差はない)。
  • \(H_{0}:\) 要因 \(fx_{2}\) (3 群)の主効果はない(平均値間に差はない)。

平均値・代表値:パラメトリック検定:3群以上の平均値の差の検定:2要因:2元配置分散分析(1要因のみ対応あり)
sampledata <- paired
[1] "sampledata"
    fx1 fx2 id    y
1  Blue   A  1 48.0
2  Blue   A  2 46.9
3  Blue   A  3 50.1
4  Blue   A  4 55.1
5  Blue   A  5 46.4
6  Blue   A  6 48.5
7  Blue   A  7 43.8
8  Blue   A  8 53.6
9  Blue   A  9 53.8
10 Blue   A 10 47.6
11 Blue   B  1 52.3
12 Blue   B  2 55.8
13 Blue   B  3 52.6
14 Blue   B  4 45.2
15 Blue   B  5 42.5
16 Blue   B  6 53.6
17 Blue   B  7 50.7
18 Blue   B  8 54.6
19 Blue   B  9 64.9
20 Blue   B 10 46.5
21 Blue   C  1 51.8
22 Blue   C  2 61.3
23 Blue   C  3 52.3
24 Blue   C  4 44.3
25 Blue   C  5 41.1
26 Blue   C  6 51.0
27 Blue   C  7 48.6
28 Blue   C  8 50.9
29 Blue   C  9 44.3
30 Blue   C 10 51.8
31  Red   A  1 45.4
32  Red   A  2 42.0
33  Red   A  3 36.0
34  Red   A  4 52.8
35  Red   A  5 55.0
36  Red   A  6 26.0
37  Red   A  7 39.2
38  Red   A  8 35.9
39  Red   A  9 37.1
40  Red   A 10 33.3
41  Red   B  1 50.9
42  Red   B  2 45.8
43  Red   B  3 37.6
44  Red   B  4 44.3
45  Red   B  5 40.8
46  Red   B  6 40.6
47  Red   B  7 22.6
48  Red   B  8 22.0
49  Red   B  9 24.4
50  Red   B 10 23.1
51  Red   C  1 57.1
52  Red   C  2 46.9
53  Red   C  3 22.2
54  Red   C  4 52.8
55  Red   C  5 35.6
56  Red   C  6 46.6
57  Red   C  7 29.8
58  Red   C  8 32.6
59  Red   C  9 41.5
60  Red   C 10 25.3
summary(aov(data = sampledata, formula = y ~ fx1 * fx2 + Error(id:fx1 + id:fx1:fx2)))

Error: id:fx1
          Df Sum Sq Mean Sq F value   Pr(>F)    
fx1        1   2217  2216.8   16.88 0.000659 ***
Residuals 18   2364   131.3                     
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Error: id:fx1:fx2
          Df Sum Sq Mean Sq F value Pr(>F)
fx2        2   17.1    8.54   0.210  0.812
fx1:fx2    2  158.4   79.21   1.947  0.157
Residuals 36 1464.3   40.67               
  • \(H_{0}:\) 要因 \(fx_{1}\) (2 群)の主効果はない(平均値間に差はない)。
  • \(H_{0}:\) 要因 \(fx_{2}\) (3 群)の主効果はない(平均値間に差はない)。
  • \(H_{0}:\) 要因 \(fx_{1}\)\(fx_{2}\) の交互作用効果はない(\(y\)への影響はない)。

平均値・代表値:パラメトリック検定:3群以上の平均値の差の検定:2要因:2元配置分散分析(2要因とも対応あり)
sampledata <- paired
[1] "sampledata"
    fx1 fx2 id    y
1  Blue   A  1 48.0
2  Blue   A  2 46.9
3  Blue   A  3 50.1
4  Blue   A  4 55.1
5  Blue   A  5 46.4
6  Blue   A  6 48.5
7  Blue   A  7 43.8
8  Blue   A  8 53.6
9  Blue   A  9 53.8
10 Blue   A 10 47.6
11 Blue   B  1 52.3
12 Blue   B  2 55.8
13 Blue   B  3 52.6
14 Blue   B  4 45.2
15 Blue   B  5 42.5
16 Blue   B  6 53.6
17 Blue   B  7 50.7
18 Blue   B  8 54.6
19 Blue   B  9 64.9
20 Blue   B 10 46.5
21 Blue   C  1 51.8
22 Blue   C  2 61.3
23 Blue   C  3 52.3
24 Blue   C  4 44.3
25 Blue   C  5 41.1
26 Blue   C  6 51.0
27 Blue   C  7 48.6
28 Blue   C  8 50.9
29 Blue   C  9 44.3
30 Blue   C 10 51.8
31  Red   A  1 45.4
32  Red   A  2 42.0
33  Red   A  3 36.0
34  Red   A  4 52.8
35  Red   A  5 55.0
36  Red   A  6 26.0
37  Red   A  7 39.2
38  Red   A  8 35.9
39  Red   A  9 37.1
40  Red   A 10 33.3
41  Red   B  1 50.9
42  Red   B  2 45.8
43  Red   B  3 37.6
44  Red   B  4 44.3
45  Red   B  5 40.8
46  Red   B  6 40.6
47  Red   B  7 22.6
48  Red   B  8 22.0
49  Red   B  9 24.4
50  Red   B 10 23.1
51  Red   C  1 57.1
52  Red   C  2 46.9
53  Red   C  3 22.2
54  Red   C  4 52.8
55  Red   C  5 35.6
56  Red   C  6 46.6
57  Red   C  7 29.8
58  Red   C  8 32.6
59  Red   C  9 41.5
60  Red   C 10 25.3
summary(aov(data = sampledata, formula = y ~ fx1 * fx2 + Error(id + id:fx1 + id:fx2 + id:fx1:fx2)))

Error: id
          Df Sum Sq Mean Sq F value Pr(>F)
Residuals  9   1069   118.8               

Error: id:fx1
          Df Sum Sq Mean Sq F value  Pr(>F)   
fx1        1   2217  2216.8   15.41 0.00348 **
Residuals  9   1294   143.8                   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Error: id:fx2
          Df Sum Sq Mean Sq F value Pr(>F)
fx2        2   17.1    8.54   0.215  0.809
Residuals 18  716.6   39.81               

Error: id:fx1:fx2
          Df Sum Sq Mean Sq F value Pr(>F)
fx1:fx2    2  158.4   79.21   1.907  0.177
Residuals 18  747.7   41.54               
  • \(H_{0}:\) 要因 \(fx_{1}\) (2 群)の主効果はない(平均値間に差はない)。
  • \(H_{0}:\) 要因 \(fx_{2}\) (3 群)の主効果はない(平均値間に差はない)。
  • \(H_{0}:\) 要因 \(fx_{1}\)\(fx_{2}\) の交互作用効果はない(\(y\)への影響はない)。

平均値・代表値:パラメトリック検定:3群以上の平均値の差の検定:各要因水準間の比較:多重比較
sampledata <- unpaired
[1] "sampledata"
    fx1 fx2 id    y
1  Blue   A  1 46.1
2  Blue   A  2 51.7
3  Blue   A  3 51.5
4  Blue   A  4 54.1
5  Blue   A  5 49.5
6  Blue   A  6 51.1
7  Blue   A  7 52.2
8  Blue   A  8 51.1
9  Blue   A  9 53.7
10 Blue   A 10 50.2
11 Blue   A 11 51.6
12 Blue   A 12 53.8
13 Blue   A 13 51.5
14 Blue   A 14 53.3
15 Blue   B 15 52.4
16 Blue   B 16 50.1
17 Blue   B 17 47.3
18 Blue   B 18 48.9
19 Blue   B 19 49.0
20 Blue   B 20 50.3
21 Blue   B 21 47.9
22 Blue   B 22 46.0
23 Blue   B 23 49.8
24 Blue   B 24 48.8
25 Blue   B 25 49.3
26 Blue   B 26 49.5
27 Blue   B 27 47.7
28 Blue   B 28 53.0
29 Blue   B 29 55.7
30 Blue   B 30 48.0
31 Blue   B 31 48.7
32 Blue   B 32 49.0
33 Blue   B 33 48.4
34 Blue   B 34 46.9
35 Blue   B 35 49.8
36 Blue   B 36 33.3
37 Blue   B 37 38.0
38 Blue   B 38 37.4
39 Blue   B 39 30.7
40 Blue   C 40 26.1
41  Red   C 41 25.9
42  Red   C 42 32.8
43  Red   C 43 40.9
44  Red   C 44 39.8
45  Red   C 45 44.6
46  Red   C 46 39.8
47  Red   C 47 48.4
48  Red   C 48 23.1
49  Red   C 49 42.1
50  Red   C 50 33.7
51  Red   C 51 42.9
52  Red   C 52 36.5
53  Red   C 53 32.4
54  Red   C 54 17.9
55  Red   C 55 42.3
56  Red   C 56 35.6
57  Red   C 57 38.9
58  Red   C 58 31.4
59  Red   C 59 20.4
60  Red   C 60 44.4
TukeyHSD(aov(sampledata %>% pull(y) ~ sampledata %>% pull(fx2)))
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = sampledata %>% pull(y) ~ sampledata %>% pull(fx2))

$`sampledata %>% pull(fx2)`
          diff        lwr         upr     p adj
B-A  -4.492571  -9.653912   0.6687696 0.0999533
C-A -16.295238 -21.630118 -10.9603584 0.0000000
C-B -11.802667 -16.379484  -7.2258498 0.0000002
  • \(H_{0}:\) 各選択2群(\(A \sim B,A\sim C,B\sim C\))の間に平均値の差はない。

平均値・代表値:ノンパラメトリック検定:2群の代表値の差の検定:対応のない場合:ウィルコクソンの順位和検定
x1 <- unpaired %>% filter(fx1 == "Blue", fx2 %in% "A") %>% pull(y)
x2 <- unpaired %>% filter(fx1 == "Blue", fx2 %in% "B") %>% pull(y)
[1] "x1"
 [1] 46.1 51.7 51.5 54.1 49.5 51.1 52.2 51.1 53.7 50.2 51.6 53.8 51.5 53.3
[1] "x2"
 [1] 52.4 50.1 47.3 48.9 49.0 50.3 47.9 46.0 49.8 48.8 49.3 49.5 47.7 53.0 55.7 48.0 48.7 49.0 48.4 46.9 49.8 33.3 38.0 37.4 30.7
exactRankTests::wilcox.exact(x = x1, y = x2, paired = F)

    Exact Wilcoxon rank sum test

data:  x1 and x2
W = 293.5, p-value = 0.0002795
alternative hypothesis: true mu is not equal to 0
  • \(H_{0}:\) 2群間の中央値に差はない。

平均値・代表値:ノンパラメトリック検定:2群の代表値の差の検定:対応のある場合:ウィルコクソンの符号付順位検定
sampledata0 <- paired %>% filter(fx1 == "Blue", fx2 %in% c("A", "B"))
sampledata <- matrix(sampledata0 %>% pull(y), ncol = 2)
[1] "sampledata0"
    fx1 fx2 id    y
1  Blue   A  1 48.0
2  Blue   A  2 46.9
3  Blue   A  3 50.1
4  Blue   A  4 55.1
5  Blue   A  5 46.4
6  Blue   A  6 48.5
7  Blue   A  7 43.8
8  Blue   A  8 53.6
9  Blue   A  9 53.8
10 Blue   A 10 47.6
11 Blue   B  1 52.3
12 Blue   B  2 55.8
13 Blue   B  3 52.6
14 Blue   B  4 45.2
15 Blue   B  5 42.5
16 Blue   B  6 53.6
17 Blue   B  7 50.7
18 Blue   B  8 54.6
19 Blue   B  9 64.9
20 Blue   B 10 46.5


------------------------------------------

[1] "sampledata"
      [,1] [,2]
 [1,] 48.0 52.3
 [2,] 46.9 55.8
 [3,] 50.1 52.6
 [4,] 55.1 45.2
 [5,] 46.4 42.5
 [6,] 48.5 53.6
 [7,] 43.8 50.7
 [8,] 53.6 54.6
 [9,] 53.8 64.9
[10,] 47.6 46.5
exactRankTests::wilcox.exact(x = sampledata[, 1], y = sampledata[, 2], paired = T)

    Exact Wilcoxon signed rank test

data:  sampledata[, 1] and sampledata[, 2]
V = 15, p-value = 0.2324
alternative hypothesis: true mu is not equal to 0
  • \(H_{0}:\) 2群間の中央値に差はない。

平均値・代表値:ノンパラメトリック検定:3群以上の代表値の差の検定:対応のない場合:クラスカルウォーリス検定
sampledata <- unpaired %>% filter(fx1 == "Blue")
[1] "sampledata"
    fx1 fx2 id    y
1  Blue   A  1 46.1
2  Blue   A  2 51.7
3  Blue   A  3 51.5
4  Blue   A  4 54.1
5  Blue   A  5 49.5
6  Blue   A  6 51.1
7  Blue   A  7 52.2
8  Blue   A  8 51.1
9  Blue   A  9 53.7
10 Blue   A 10 50.2
11 Blue   A 11 51.6
12 Blue   A 12 53.8
13 Blue   A 13 51.5
14 Blue   A 14 53.3
15 Blue   B 15 52.4
16 Blue   B 16 50.1
17 Blue   B 17 47.3
18 Blue   B 18 48.9
19 Blue   B 19 49.0
20 Blue   B 20 50.3
21 Blue   B 21 47.9
22 Blue   B 22 46.0
23 Blue   B 23 49.8
24 Blue   B 24 48.8
25 Blue   B 25 49.3
26 Blue   B 26 49.5
27 Blue   B 27 47.7
28 Blue   B 28 53.0
29 Blue   B 29 55.7
30 Blue   B 30 48.0
31 Blue   B 31 48.7
32 Blue   B 32 49.0
33 Blue   B 33 48.4
34 Blue   B 34 46.9
35 Blue   B 35 49.8
36 Blue   B 36 33.3
37 Blue   B 37 38.0
38 Blue   B 38 37.4
39 Blue   B 39 30.7
40 Blue   C 40 26.1
kruskal.test(x = sampledata %>% pull(y), g = sampledata %>% pull(fx2))

    Kruskal-Wallis rank sum test

data:  sampledata %>% pull(y) and sampledata %>% pull(fx2)
Kruskal-Wallis chi-squared = 14.309, df = 2, p-value = 0.0007812
  • \(H_{0}:\) 要因 \(fx_{2}\) (3 群)の主効果はない(中央値間に差はない)。

平均値・代表値:ノンパラメトリック検定:3群以上の代表値の差の検定:対応のある場合:フリードマン検定
sampledata0 <- paired %>% filter(fx1 == "Blue")
sampledata <- matrix(sampledata0 %>% pull(y), ncol = 3)
[1] "sampledata0"
    fx1 fx2 id    y
1  Blue   A  1 48.0
2  Blue   A  2 46.9
3  Blue   A  3 50.1
4  Blue   A  4 55.1
5  Blue   A  5 46.4
6  Blue   A  6 48.5
7  Blue   A  7 43.8
8  Blue   A  8 53.6
9  Blue   A  9 53.8
10 Blue   A 10 47.6
11 Blue   B  1 52.3
12 Blue   B  2 55.8
13 Blue   B  3 52.6
14 Blue   B  4 45.2
15 Blue   B  5 42.5
16 Blue   B  6 53.6
17 Blue   B  7 50.7
18 Blue   B  8 54.6
19 Blue   B  9 64.9
20 Blue   B 10 46.5
21 Blue   C  1 51.8
22 Blue   C  2 61.3
23 Blue   C  3 52.3
24 Blue   C  4 44.3
25 Blue   C  5 41.1
26 Blue   C  6 51.0
27 Blue   C  7 48.6
28 Blue   C  8 50.9
29 Blue   C  9 44.3
30 Blue   C 10 51.8


------------------------------------------

[1] "sampledata"
      [,1] [,2] [,3]
 [1,] 48.0 52.3 51.8
 [2,] 46.9 55.8 61.3
 [3,] 50.1 52.6 52.3
 [4,] 55.1 45.2 44.3
 [5,] 46.4 42.5 41.1
 [6,] 48.5 53.6 51.0
 [7,] 43.8 50.7 48.6
 [8,] 53.6 54.6 50.9
 [9,] 53.8 64.9 44.3
[10,] 47.6 46.5 51.8
friedman.test(y = sampledata)

    Friedman rank sum test

data:  sampledata
Friedman chi-squared = 3.8, df = 2, p-value = 0.1496
  • \(H_{0}:\) 要因 \(fx_{2}\) (3 群)の主効果はない(中央値間に差はない)。

比率:母比率の検定:母比率の検定
x <- 15  # 例 成功回数
n <- 100  # 例 試行回数
p <- 0.3  # 比率
conf.level <- 0.95
prop.test(x, n, p = p, alternative = "two.sided", conf.level = conf.level)

    1-sample proportions test with continuity correction

data:  x out of n, null probability p
X-squared = 10.012, df = 1, p-value = 0.001555
alternative hypothesis: true p is not equal to 0.3
95 percent confidence interval:
 0.0891491 0.2385308
sample estimates:
   p 
0.15 
prop.test(x, n, p = p, alternative = "less", conf.level = conf.level)

    1-sample proportions test with continuity correction

data:  x out of n, null probability p
X-squared = 10.012, df = 1, p-value = 0.0007777
alternative hypothesis: true p is less than 0.3
95 percent confidence interval:
 0.0000000 0.2235259
sample estimates:
   p 
0.15 
prop.test(x, n, p = p, alternative = "greater", conf.level = conf.level)

    1-sample proportions test with continuity correction

data:  x out of n, null probability p
X-squared = 10.012, df = 1, p-value = 0.9992
alternative hypothesis: true p is greater than 0.3
95 percent confidence interval:
 0.09644405 1.00000000
sample estimates:
   p 
0.15 

比率:母比率の検定:母比率の検定(2項検定)
x <- 65  # 例 成功回数
n <- 100  # 例 試行回数
p <- 0.6  # 比率
conf.level <- 0.95
binom.test(x, n, p = p, alternative = "two.sided", conf.level = conf.level)

    Exact binomial test

data:  x and n
number of successes = 65, number of trials = 100, p-value = 0.3584
alternative hypothesis: true probability of success is not equal to 0.6
95 percent confidence interval:
 0.5481506 0.7427062
sample estimates:
probability of success 
                  0.65 
binom.test(x, n, p = p, alternative = "less", conf.level = conf.level)

    Exact binomial test

data:  x and n
number of successes = 65, number of trials = 100, p-value = 0.8697
alternative hypothesis: true probability of success is less than 0.6
95 percent confidence interval:
 0.0000000 0.7292455
sample estimates:
probability of success 
                  0.65 
binom.test(x, n, p = p, alternative = "greater", conf.level = conf.level)

    Exact binomial test

data:  x and n
number of successes = 65, number of trials = 100, p-value = 0.1795
alternative hypothesis: true probability of success is greater than 0.6
95 percent confidence interval:
 0.5639164 1.0000000
sample estimates:
probability of success 
                  0.65 

比率:2群の比率の差の検定:対応のない場合:フィッシャーの正確確率検定
sampledata <- matrix(c(5, 12, 14, 4), nrow = 2, byrow = T)
conf.level <- 0.95
fisher.test(x = sampledata, alternative = "two.sided", conf.level = conf.level)

    Fisher's Exact Test for Count Data

data:  sampledata
p-value = 0.00671
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
 0.01946201 0.66787840
sample estimates:
odds ratio 
 0.1280452 
fisher.test(x = sampledata, alternative = "less", conf.level = conf.level)

    Fisher's Exact Test for Count Data

data:  sampledata
p-value = 0.005169
alternative hypothesis: true odds ratio is less than 1
95 percent confidence interval:
 0.0000000 0.5402117
sample estimates:
odds ratio 
 0.1280452 
fisher.test(x = sampledata, alternative = "greater", conf.level = conf.level)

    Fisher's Exact Test for Count Data

data:  sampledata
p-value = 0.9995
alternative hypothesis: true odds ratio is greater than 1
95 percent confidence interval:
 0.02570015        Inf
sample estimates:
odds ratio 
 0.1280452 
  • \(H_{0}:\) 2要因の比率に差はない(2要因は互いに無関係)=Odds ratio(オッズ比)の信頼区間が1を跨いでいない。
  • \(オッズ比:\dfrac{a/b}{c/d}\)
  • \(相対リスク:\dfrac{a/(a+b)}{c/(c+d)}\)


|            | a person who has cancer| a person who has no cancer|
|:-----------|-----------------------:|--------------------------:|
|Drinker     |                    5(a)|                      12(b)|
|Non-drinker |                   14(c)|                       4(d)|
# 自然数以外(カウントデータ(度数)以外を入力した場合のメッセージ)
fisher.test(x = matrix(c(5.4, 12.4, 14.2, 4.1), nrow = 2, byrow = T), alternative = "two.sided", conf.level = conf.level)
Warning in fisher.test(x = matrix(c(5.4, 12.4, 14.2, 4.1), nrow = 2, byrow = T), : 'x' has been rounded to integer: Mean relative difference: 0.03047091

比率:2群の比率の差の検定:対応のある場合:マクネマー検定
sampledata <- matrix(c(9, 5, 12, 8), nrow = 2, byrow = T)
mcnemar.test(x = sampledata)

    McNemar's Chi-squared test with continuity correction

data:  sampledata
McNemar's chi-squared = 2.1176, df = 1, p-value = 0.1456


|                    | After training:Yes| After training:No|
|:-------------------|------------------:|-----------------:|
|Before training:Yes |                  9|                 5|
|Before training:No  |                 12|                 8|
  • \(H_{0}:\) 2要因の比率に差はない(2要因は互いに無関係)。

比率:3群以上の比率の差の検定:対応のない場合:フィッシャーの正確確率検定
library("RVAideMemoire")
sampledata <- matrix(round(runif(n = 4 * 4, min = 10, max = 40)), nrow = 4, byrow = T, dimnames = list(paste0("Cause", 1:4), paste0("Condition", 1:4)))
fisher.multcomp(sampledata, p.method = "fdr")

        Pairwise comparisons using Fisher's exact test for count data

data:  sampledata

              Condition1:Condition2 Condition1:Condition3 Condition1:Condition4 Condition2:Condition3 Condition2:Condition4 Condition3:Condition4
Cause1:Cause2                1.0000                0.7291                0.7291                0.6945                0.7291                0.8959
Cause1:Cause3                0.7747                0.8959                0.7291                1.0000                1.0000                1.0000
Cause1:Cause4                1.0000                1.0000                1.0000                1.0000                1.0000                1.0000
Cause2:Cause3                0.7291                0.7747                1.0000                0.6945                0.7291                0.7291
Cause2:Cause4                1.0000                0.6945                0.7291                0.6945                0.7291                0.7747
Cause3:Cause4                0.7291                0.7291                0.7291                1.0000                1.0000                1.0000

P value adjustment method: fdr
fisher.multcomp(sampledata, p.method = "none")

        Pairwise comparisons using Fisher's exact test for count data

data:  sampledata

              Condition1:Condition2 Condition1:Condition3 Condition1:Condition4 Condition2:Condition3 Condition2:Condition4 Condition3:Condition4
Cause1:Cause2                1.0000               0.16003                0.3240               0.07717                0.2583                0.5226
Cause1:Cause3                0.3676               0.50863                0.3208               0.82326                1.0000                0.8429
Cause1:Cause4                0.8116               0.80940                0.8260               1.00000                1.0000                1.0000
Cause2:Cause3                0.2314               0.38766                1.0000               0.03198                0.1924                0.3060
Cause2:Cause4                1.0000               0.06148                0.1603               0.04866                0.1911                0.4089
Cause3:Cause4                0.2518               0.26687                0.1594               1.00000                1.0000                1.0000

P value adjustment method: none
fisher.multcomp(sampledata, p.method = "holm")

        Pairwise comparisons using Fisher's exact test for count data

data:  sampledata

              Condition1:Condition2 Condition1:Condition3 Condition1:Condition4 Condition2:Condition3 Condition2:Condition4 Condition3:Condition4
Cause1:Cause2                     1                     1                     1                     1                     1                     1
Cause1:Cause3                     1                     1                     1                     1                     1                     1
Cause1:Cause4                     1                     1                     1                     1                     1                     1
Cause2:Cause3                     1                     1                     1                     1                     1                     1
Cause2:Cause4                     1                     1                     1                     1                     1                     1
Cause3:Cause4                     1                     1                     1                     1                     1                     1

P value adjustment method: holm
fisher.multcomp(sampledata, p.method = "hochberg")

        Pairwise comparisons using Fisher's exact test for count data

data:  sampledata

              Condition1:Condition2 Condition1:Condition3 Condition1:Condition4 Condition2:Condition3 Condition2:Condition4 Condition3:Condition4
Cause1:Cause2                     1                     1                     1                     1                     1                     1
Cause1:Cause3                     1                     1                     1                     1                     1                     1
Cause1:Cause4                     1                     1                     1                     1                     1                     1
Cause2:Cause3                     1                     1                     1                     1                     1                     1
Cause2:Cause4                     1                     1                     1                     1                     1                     1
Cause3:Cause4                     1                     1                     1                     1                     1                     1

P value adjustment method: hochberg


|       | Condition1| Condition2| Condition3| Condition4|
|:------|----------:|----------:|----------:|----------:|
|Cause1 |         16|         19|         19|         32|
|Cause2 |         25|         32|         14|         33|
|Cause3 |         26|         19|         22|         33|
|Cause4 |         14|         19|         21|         34|
  • \(H_{0}:\) 要因間の比率に差はない。

比率:3群以上の比率の差の検定:対応のある場合(2値型変数):コクランのQ検定
library(nonpar)
sampledata <- matrix(sapply(X = 1:40, function(x) sample(x = c(0, 1), size = 1)), nrow = 10, byrow = T, dimnames = list(paste0("Person", 1:10), paste0("Condition", 1:4)))
cochrans.q(x = sampledata, alpha = 0.05)

 Cochran's Q Test 
 
 H0: There is no difference in the effectiveness of treatments. 
 HA: There is a difference in the effectiveness of treatments. 
 
 Q = 4.44827586206897 
 
 Degrees of Freedom = 3 
 
 Significance Level = 0.05 
 The p-value is  0.216950625105275 
  
 
cochrans.q(x = sampledata, alpha = 0.99)

 Cochran's Q Test 
 
 H0: There is no difference in the effectiveness of treatments. 
 HA: There is a difference in the effectiveness of treatments. 
 
 Q = 4.44827586206897 
 
 Degrees of Freedom = 3 
 
 Significance Level = 0.99 
 The p-value is  0.216950625105275 
 There is enough evidence to conclude that the effectiveness of at least two treatments differ. 
 


|         | Condition1| Condition2| Condition3| Condition4|
|:--------|----------:|----------:|----------:|----------:|
|Person1  |          1|          0|          0|          0|
|Person2  |          0|          1|          0|          0|
|Person3  |          1|          1|          0|          0|
|Person4  |          0|          0|          0|          1|
|Person5  |          0|          0|          1|          0|
|Person6  |          0|          0|          0|          0|
|Person7  |          0|          1|          1|          1|
|Person8  |          1|          1|          0|          0|
|Person9  |          0|          1|          0|          0|
|Person10 |          0|          1|          0|          0|
  • \(H_{0}:\) 名義尺度間に比率の差はない。

分散比:2群の分散比:F検定
x1 <- rnorm(n = 10, mean = 50, sd = 5)
x1
x2 <- rnorm(n = 15, mean = 50, sd = 15)
x2
var.test(x1, x2, conf.level = 0.95, alternative = "two")
 [1] 46.09772 39.14309 49.37971 46.59222 50.02527 46.36694 49.83845 47.41577 54.87273 52.69528
 [1] 49.70391 36.95847 10.88734 20.90672 43.95356 65.15868 52.12895 40.17359 62.47197 42.57358 49.03782 29.86599 48.45481 62.82107 36.58956

    F test to compare two variances

data:  x1 and x2
F = 0.079451, num df = 9, denom df = 14, p-value = 0.000609
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
 0.02475635 0.30174941
sample estimates:
ratio of variances 
        0.07945055 
  • サンプル:等しい標準偏差の2つの正規乱数群
x1 <- rnorm(n = 20, mean = 50, sd = 15)
x1
x2 <- rnorm(n = 10, mean = 50, sd = 15)
x2
var.test(x1, x2, conf.level = 0.95, alternative = "two")
 [1] 66.00198 57.93159 38.28812 37.96049 32.70867 85.15332 68.96308 44.17479 47.96451 30.77549 39.59804 49.12921 26.45435 84.82110 39.59042 70.74223 46.99702 23.70435 33.61965 51.29038
 [1] 49.17823 73.88234 43.85561 35.48117 68.06597 50.15888 57.26604 61.20317 48.46911 45.55969

    F test to compare two variances

data:  x1 and x2
F = 2.3974, num df = 19, denom df = 9, p-value = 0.18
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
 0.6508675 6.9045363
sample estimates:
ratio of variances 
          2.397365 
  • \(H_{0}:\) 2群の母分散は等しい。

分散比:3群以上の分散比:バートレットの検定
library(lawstat)
x1 <- rnorm(n = 10, mean = 50, sd = 5)
x1
x2 <- rnorm(n = 15, mean = 50, sd = 15)
x2
x3 <- rnorm(n = 20, mean = 50, sd = 10)
x3
x4 <- rnorm(n = 13, mean = 50, sd = 25)
x4
x <- c(x1, x2, x3, x4)
fx <- factor(rep(c("x1", "x2", "x3", "x4"), c(length(x1), length(x2), length(x3), length(x4))))
bartlett.test(formula = x ~ fx)
 [1] 50.31213 54.17857 51.82893 46.51281 53.00417 54.71090 48.55800 46.39452 58.05533 46.33392
 [1] 55.685407 69.593884 64.454152 42.335403 30.273403 66.436561 38.924689 46.106573 70.815928 38.298540 50.912933 52.092739 41.425722 59.471722  8.546084
 [1] 70.64138 46.73474 47.96021 49.53206 62.07607 35.97227 58.15858 47.36998 67.10023 55.77698 43.32490 49.22866 52.14825 52.32579 38.68419 27.97874 30.16222 44.20726 62.03609 67.20145
 [1] 30.586776 76.485413 45.164934 66.734463 68.715545 53.991438 76.806352 27.172464 37.806092  9.451688 66.174188 25.362103 47.765558

    Bartlett test of homogeneity of variances

data:  x by fx
Bartlett's K-squared = 20.762, df = 3, p-value = 0.0001179
  • サンプル:等しい標準偏差の4つの正規乱数群
x1 <- rnorm(n = 10, mean = 50, sd = 10)
x1
x2 <- rnorm(n = 15, mean = 50, sd = 10)
x2
x3 <- rnorm(n = 20, mean = 50, sd = 10)
x3
x4 <- rnorm(n = 13, mean = 50, sd = 10)
x4
x <- c(x1, x2, x3, x4)
fx <- factor(rep(c("x1", "x2", "x3", "x4"), c(length(x1), length(x2), length(x3), length(x4))))
bartlett.test(formula = x ~ fx)
 [1] 40.98558 47.44534 50.76247 44.30208 41.21262 41.03958 61.87178 61.73139 40.14091 56.27096
 [1] 48.81861 48.47967 51.61566 45.89666 57.73128 60.97752 51.38669 45.67605 33.86945 57.36506 52.39620 39.81632 62.85657 75.33988 44.52733
 [1] 40.13345 43.08094 61.89695 42.17389 54.36449 52.76246 41.45347 61.08971 60.39345 50.53818 62.60910 53.34545 39.75805 55.92466 69.45084 50.44609 34.76198 39.78483 47.10781 44.31990
 [1] 41.85334 35.65538 40.30444 60.03757 59.11817 48.02029 53.48646 47.69278 68.51407 25.62719 60.51301 47.58683 41.94073

    Bartlett test of homogeneity of variances

data:  x by fx
Bartlett's K-squared = 1.0764, df = 3, p-value = 0.7828
  • \(H_{0}:\) 各群の母分散は全て等しい。

分散比:3群以上の分散比:ルービンの検定
x1 <- rnorm(n = 10, mean = 50, sd = 5)
x1
x2 <- rnorm(n = 15, mean = 50, sd = 15)
x2
x3 <- rnorm(n = 20, mean = 50, sd = 10)
x3
x4 <- rnorm(n = 13, mean = 50, sd = 25)
x4
x <- c(x1, x2, x3, x4)
fx <- factor(rep(c("x1", "x2", "x3", "x4"), c(length(x1), length(x2), length(x3), length(x4))))
levene.test(y = x, group = fx)
 [1] 54.35651 45.73554 45.27364 57.85004 45.77224 55.64347 53.03535 55.03159 52.23612 48.88372
 [1] 31.07751 53.90315 48.34566 32.47806 49.12782 54.64009 56.28903 48.51985 69.28409 23.92105 33.86356 56.02181 35.53937 51.18913 57.32180
 [1] 51.81872 66.78845 63.75667 42.06827 47.41227 46.29887 41.79144 46.14452 50.44579 46.55096 33.63878 46.91348 41.64575 52.17339 52.34836 58.52529 34.18790 40.54802 41.04518 45.07084
 [1] 90.737585 61.895153 89.959989 37.976126 38.216781 36.636645 61.731743  4.686266 -5.703668 -1.892180 52.849753 35.766801 28.964131

    Modified robust Brown-Forsythe Levene-type test based on the absolute deviations from the median

data:  x
Test Statistic = 7.9525, p-value = 0.0001751
  • サンプル:等しい標準偏差の4つの正規乱数群
x1 <- rnorm(n = 10, mean = 50, sd = 10)
x1
x2 <- rnorm(n = 15, mean = 50, sd = 10)
x2
x3 <- rnorm(n = 20, mean = 50, sd = 10)
x3
x4 <- rnorm(n = 13, mean = 50, sd = 10)
x4
x <- c(x1, x2, x3, x4)
fx <- factor(rep(c("x1", "x2", "x3", "x4"), c(length(x1), length(x2), length(x3), length(x4))))
levene.test(y = x, group = fx)
 [1] 47.52172 48.70263 63.45944 54.36066 25.81330 57.93560 32.45110 53.22808 34.19317 50.77645
 [1] 64.62481 45.44255 42.79375 48.24262 63.27150 51.51403 46.25898 53.08863 50.11443 35.70400 37.55666 37.89322 59.13541 40.42750 42.94235
 [1] 62.04157 49.67260 55.94650 49.30675 54.49721 35.37357 43.12584 67.20214 55.10023 54.12287 36.45427 47.80468 42.74274 45.01913 48.53629 54.02777 27.45120 54.20788 55.65901 42.90990
 [1] 37.14248 34.17197 48.90413 48.81314 62.60763 51.66514 55.60558 53.58457 32.14883 55.32684 43.21414 40.52612 59.99598

    Modified robust Brown-Forsythe Levene-type test based on the absolute deviations from the median

data:  x
Test Statistic = 0.26937, p-value = 0.8472
  • \(H_{0}:\) 各群の母分散は全て等しい。

平均値・代表値:ノンパラメトリック検定:2群の代表値の差の検定:対応のない場合:Brunner-Munzel検定/ブルンナー=ムンツェル検定
x1 <- rnorm(n = 10, mean = 50, sd = 10)
x1
median(x1)
x2 <- rnorm(n = 15, mean = 30, sd = 15)
x2
median(x2)
brunner.munzel.test(x = x1, y = x2, alternative = "two", alpha = 0.05)
 [1] 32.74355 54.75750 54.02932 52.57404 46.62811 51.84788 47.64260 59.44250 43.14691 55.86716
[1] 52.21096
 [1] 25.32807 24.52399 24.49532 36.68441 40.07230 43.61417 40.98378 28.84423 30.24728 18.67903 23.75187 43.40702 41.65594 47.24935 27.35462
[1] 30.24728

    Brunner-Munzel Test

data:  x1 and x2
Brunner-Munzel Test Statistic = -7.8798, df = 13.796, p-value = 1.797e-06
95 percent confidence interval:
 -0.04296243  0.18962910
sample estimates:
P(X<Y)+.5*P(X=Y) 
      0.07333333 
  • サンプル:平均値と標準偏差が等しくサンプルサイズが異なる2つの正規乱数群
x1 <- rnorm(n = 30, mean = 50, sd = 5)
x1
median(x1)
x2 <- rnorm(n = 15, mean = 50, sd = 5)
x2
median(x2)
brunner.munzel.test(x = x1, y = x2, alternative = "two", alpha = 0.05)
 [1] 48.04679 51.67182 49.76586 55.01692 45.46703 51.64532 56.50821 44.46391 47.37299 46.12783 50.33087 51.28513 41.95718 46.95612 54.75566 43.82644 55.58291 55.49664 42.44282 46.70553 61.69556 58.19738 54.19277 46.74029 41.26975 50.75475 48.44999 53.17299 47.07168 53.82910
[1] 50.04836
 [1] 45.57108 53.63077 55.32120 51.64846 46.16259 49.41116 53.91025 49.90258 51.78309 42.70488 49.88766 46.82916 44.23080 48.32110 51.98634
[1] 49.88766

    Brunner-Munzel Test

data:  x1 and x2
Brunner-Munzel Test Statistic = -0.30165, df = 39.328, p-value = 0.7645
95 percent confidence interval:
 0.2945676 0.6520990
sample estimates:
P(X<Y)+.5*P(X=Y) 
       0.4733333 
  • \(H_{0}:\) 2群間の中央値に差はない。

利用関数のヘルプ
  • ヘルプは各々のパッケージから引用しています。
t.test R Documentation

Student’s t-Test

Description

Performs one and two sample t-tests on vectors of data.

Usage

t.test(x, ...)

## Default S3 method:
t.test(x, y = NULL,
       alternative = c("two.sided", "less", "greater"),
       mu = 0, paired = FALSE, var.equal = FALSE,
       conf.level = 0.95, ...)

## S3 method for class 'formula'
t.test(formula, data, subset, na.action, ...)

Arguments

x

a (non-empty) numeric vector of data values.

y

an optional (non-empty) numeric vector of data values.

alternative

a character string specifying the alternative hypothesis, must be one of “two.sided” (default), “greater” or “less”. You can specify just the initial letter.

mu

a number indicating the true value of the mean (or difference in means if you are performing a two sample test).

paired

a logical indicating whether you want a paired t-test.

var.equal

a logical variable indicating whether to treat the two variances as being equal. If TRUE then the pooled variance is used to estimate the variance otherwise the Welch (or Satterthwaite) approximation to the degrees of freedom is used.

conf.level

confidence level of the interval.

formula

a formula of the form lhs ~ rhs where lhs is a numeric variable giving the data values and rhs a factor with two levels giving the corresponding groups.

data

an optional matrix or data frame (or similar: see model.frame) containing the variables in the formula formula. By default the variables are taken from environment(formula).

subset

an optional vector specifying a subset of observations to be used.

na.action

a function which indicates what should happen when the data contain NAs. Defaults to getOption(“na.action”).

further arguments to be passed to or from methods.

Details

The formula interface is only applicable for the 2-sample tests.

alternative = “greater” is the alternative that x has a larger mean than y.

If paired is TRUE then both x and y must be specified and they must be the same length. Missing values are silently removed (in pairs if paired is TRUE). If var.equal is TRUE then the pooled estimate of the variance is used. By default, if var.equal is FALSE then the variance is estimated separately for both groups and the Welch modification to the degrees of freedom is used.

If the input data are effectively constant (compared to the larger of the two means) an error is generated.

Value

A list with class “htest” containing the following components:

statistic

the value of the t-statistic.

parameter

the degrees of freedom for the t-statistic.

p.value

the p-value for the test.

conf.int

a confidence interval for the mean appropriate to the specified alternative hypothesis.

estimate

the estimated mean or difference in means depending on whether it was a one-sample test or a two-sample test.

null.value

the specified hypothesized value of the mean or mean difference depending on whether it was a one-sample test or a two-sample test.

alternative

a character string describing the alternative hypothesis.

method

a character string indicating what type of t-test was performed.

data.name

a character string giving the name(s) of the data.

See Also

prop.test

Examples

require(graphics)

t.test(1:10, y = c(7:20))      # P = .00001855
t.test(1:10, y = c(7:20, 200)) # P = .1245    -- NOT significant anymore

## Classical example: Student's sleep data
plot(extra ~ group, data = sleep)
## Traditional interface
with(sleep, t.test(extra[group == 1], extra[group == 2]))
## Formula interface
t.test(extra ~ group, data = sleep)

aov R Documentation

Fit an Analysis of Variance Model

Description

Fit an analysis of variance model by a call to lm for each stratum.

Usage

aov(formula, data = NULL, projections = FALSE, qr = TRUE,
    contrasts = NULL, ...)

Arguments

formula

A formula specifying the model.

data

A data frame in which the variables specified in the formula will be found. If missing, the variables are searched for in the standard way.

projections

Logical flag: should the projections be returned?

qr

Logical flag: should the QR decomposition be returned?

contrasts

A list of contrasts to be used for some of the factors in the formula. These are not used for any Error term, and supplying contrasts for factors only in the Error term will give a warning.

Arguments to be passed to lm, such as subset or na.action. See ‘Details’ about weights.

Details

This provides a wrapper to lm for fitting linear models to balanced or unbalanced experimental designs.

The main difference from lm is in the way print, summary and so on handle the fit: this is expressed in the traditional language of the analysis of variance rather than that of linear models.

If the formula contains a single Error term, this is used to specify error strata, and appropriate models are fitted within each error stratum.

The formula can specify multiple responses.

Weights can be specified by a weights argument, but should not be used with an Error term, and are incompletely supported (e.g., not by model.tables).

Value

An object of class c(“aov”, “lm”) or for multiple responses of class c(“maov”, “aov”, “mlm”, “lm”) or for multiple error strata of class c(“aovlist”, “listof”). There are print and summary methods available for these.

Note

aov is designed for balanced designs, and the results can be hard to interpret without balance: beware that missing values in the response(s) will likely lose the balance. If there are two or more error strata, the methods used are statistically inefficient without balance, and it may be better to use lme in package nlme.

Balance can be checked with the replications function.

The default ‘contrasts’ in R are not orthogonal contrasts, and aov and its helper functions will work better with such contrasts: see the examples for how to select these.

Author(s)

The design was inspired by the S function of the same name described in Chambers et al (1992).

References

Chambers, J. M., Freeny, A and Heiberger, R. M. (1992) Analysis of variance; designed experiments. Chapter 5 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.

See Also

lm, summary.aov, replications, alias, proj, model.tables, TukeyHSD

Examples

## From Venables and Ripley (2002) p.165.

## Set orthogonal contrasts.
op <- options(contrasts = c("contr.helmert", "contr.poly"))
( npk.aov <- aov(yield ~ block + N*P*K, npk) )
summary(npk.aov)
coefficients(npk.aov)

## to show the effects of re-ordering terms contrast the two fits
aov(yield ~ block + N * P + K, npk)
aov(terms(yield ~ block + N * P + K, keep.order = TRUE), npk)


## as a test, not particularly sensible statistically
npk.aovE <- aov(yield ~  N*P*K + Error(block), npk)
npk.aovE
summary(npk.aovE)
options(op)  # reset to previous

anova R Documentation

Anova Tables

Description

Compute analysis of variance (or deviance) tables for one or more fitted model objects.

Usage

anova(object, ...)

Arguments

object

an object containing the results returned by a model fitting function (e.g., lm or glm).

additional objects of the same type.

Value

This (generic) function returns an object of class anova. These objects represent analysis-of-variance and analysis-of-deviance tables. When given a single argument it produces a table which tests whether the model terms are significant.

When given a sequence of objects, anova tests the models against one another in the order specified.

The print method for anova objects prints tables in a ‘pretty’ form.

Warning

The comparison between two or more models will only be valid if they are fitted to the same dataset. This may be a problem if there are missing values and R’s default of na.action = na.omit is used.

References

Chambers, J. M. and Hastie, T. J. (1992) Statistical Models in S, Wadsworth & Brooks/Cole.

See Also

coefficients, effects, fitted.values, residuals, summary, drop1, add1.


TukeyHSD R Documentation

Compute Tukey Honest Significant Differences

Description

Create a set of confidence intervals on the differences between the means of the levels of a factor with the specified family-wise probability of coverage. The intervals are based on the Studentized range statistic, Tukey’s ‘Honest Significant Difference’ method.

Usage

TukeyHSD(x, which, ordered = FALSE, conf.level = 0.95, ...)

Arguments

x

A fitted model object, usually an aov fit.

which

A character vector listing terms in the fitted model for which the intervals should be calculated. Defaults to all the terms.

ordered

A logical value indicating if the levels of the factor should be ordered according to increasing average in the sample before taking differences. If ordered is true then the calculated differences in the means will all be positive. The significant differences will be those for which the lwr end point is positive.

conf.level

A numeric value between zero and one giving the family-wise confidence level to use.

Optional additional arguments. None are used at present.

Details

This is a generic function: the description here applies to the method for fits of class “aov”.

When comparing the means for the levels of a factor in an analysis of variance, a simple comparison using t-tests will inflate the probability of declaring a significant difference when it is not in fact present. This because the intervals are calculated with a given coverage probability for each interval but the interpretation of the coverage is usually with respect to the entire family of intervals.

John Tukey introduced intervals based on the range of the sample means rather than the individual differences. The intervals returned by this function are based on this Studentized range statistics.

The intervals constructed in this way would only apply exactly to balanced designs where there are the same number of observations made at each level of the factor. This function incorporates an adjustment for sample size that produces sensible intervals for mildly unbalanced designs.

If which specifies non-factor terms these will be dropped with a warning: if no terms are left this is an error.

Value

A list of class c(“multicomp”, “TukeyHSD”), with one component for each term requested in which. Each component is a matrix with columns diff giving the difference in the observed means, lwr giving the lower end point of the interval, upr giving the upper end point and p adj giving the p-value after adjustment for the multiple comparisons.

There are print and plot methods for class “TukeyHSD”. The plot method does not accept xlab, ylab or main arguments and creates its own values for each plot.

Author(s)

Douglas Bates

References

Miller, R. G. (1981) Simultaneous Statistical Inference. Springer.

Yandell, B. S. (1997) Practical Data Analysis for Designed Experiments. Chapman & Hall.

See Also

aov, qtukey, model.tables, glht in package multcomp.

Examples

require(graphics)

summary(fm1 <- aov(breaks ~ wool + tension, data = warpbreaks))
TukeyHSD(fm1, "tension", ordered = TRUE)
plot(TukeyHSD(fm1, "tension"))

wilcox.exact R Documentation

Wilcoxon Rank Sum and Signed Rank Tests

Description

Performs one and two sample Wilcoxon tests on vectors of data for possibly tied observations.

Usage

## Default S3 method:
wilcox.exact(x, y = NULL, alternative = c("two.sided", "less", "greater"),
             mu = 0, paired = FALSE, exact = NULL,  
             conf.int = FALSE, conf.level = 0.95, ...)
## S3 method for class 'formula'
wilcox.exact(formula, data, subset, na.action, ...)

Arguments

x

numeric vector of data values.

y

an optional numeric vector of data values.

alternative

the alternative hypothesis must be one of “two.sided” (default), “greater” or “less”. You can specify just the initial letter.

mu

a number specifying an optional location parameter.

paired

a logical indicating whether you want a paired test.

exact

a logical indicating whether an exact p-value should be computed.

conf.int

a logical indicating whether a confidence interval should be computed.

conf.level

confidence level of the interval.

formula

a formula of the form lhs ~ rhs where lhs is a numeric variable giving the data values and rhs a factor with two levels giving the corresponding groups.

data

an optional data frame containing the variables in the model formula.

subset

an optional vector specifying a subset of observations to be used.

na.action

a function which indicates what should happen when the data contain NAs. Defaults to getOption(“na.action”).

further arguments to be passed to or from methods.

Details

This version computes exact conditional (on the data) p-values and quantiles using the Shift-Algorithm by Streitberg & R&quot;ohmel for both tied and untied samples.

If only x is given, or if both x and y are given and paired is TRUE, a Wilcoxon signed rank test of the null that the median of x (in the one sample case) or of x-y (in the paired two sample case) equals mu is performed.

Otherwise, if both x and y are given and paired is FALSE, a Wilcoxon rank sum test (equivalent to the Mann-Whitney test) is carried out. In this case, the null hypothesis is that the location of the distributions of x and y differ by mu.

By default (if exact is not specified), an exact p-value is computed if the samples contain less than 50 finite values and there are no ties. Otherwise, a normal approximation is used.

Optionally (if argument conf.int is true), a nonparametric confidence interval for the median (one-sample case) or for the difference of the location parameters x-y is computed. If exact p-values are available, an exact confidence interval is obtained by the algorithm described in Bauer (1972). Otherwise, an asymptotic confidence interval is returned.

Value

A list with class “htest” containing the following components:

statistic

the value of the test statistic with a name describing it.

p.value

the p-value for the test.

pointprob

this gives the probability of observing the test statistic itself (called point-prob).

null.value

the location parameter mu.

alternative

a character string describing the alternative hypothesis.

method

the type of test applied.

data.name

a character string giving the names of the data.

conf.int

a confidence interval for the location parameter. (Only present if argument conf.int = TRUE.)

estimate

Hodges-Lehmann estimate of the location parameter. (Only present if argument conf.int = TRUE.)

References

Myles Hollander & Douglas A. Wolfe (1973), Nonparametric statistical inference. New York: John Wiley & Sons. Pages 27–33 (one-sample), 68–75 (two-sample).

David F. Bauer (1972), Constructing confidence sets using rank statistics. Journal of the American Statistical Association 67, 687–690.

Cyrus R. Mehta & Nitin R. Patel (2001), StatXact-5 for Windows. Manual, Cytel Software Cooperation, Cambridge, USA

See Also

perm.test for the one and two sample permutation test.

Examples

## One-sample test.
## Hollander & Wolfe (1973), 29f.
## Hamilton depression scale factor measurements in 9 patients with
##  mixed anxiety and depression, taken at the first (x) and second
##  (y) visit after initiation of a therapy (administration of a
##  tranquilizer).
x <- c(1.83,  0.50,  1.62,  2.48, 1.68, 1.88, 1.55, 3.06, 1.30)
y <- c(0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29)
wilcox.exact(x, y, paired = TRUE, alternative = "greater")
wilcox.exact(y - x, alternative = "less")    # The same.

## Two-sample test.
## Hollander & Wolfe (1973), 69f.
## Permeability constants of the human chorioamnion (a placental
##  membrane) at term (x) and between 12 to 26 weeks gestational
##  age (y).  The alternative of interest is greater permeability
##  of the human chorioamnion for the term pregnancy.
x <- c(0.80, 0.83, 1.89, 1.04, 1.45, 1.38, 1.91, 1.64, 0.73, 1.46)
y <- c(1.15, 0.88, 0.90, 0.74, 1.21)
wilcox.exact(x, y, alternative = "g")        # greater

## Formula interface.
data(airquality)
boxplot(Ozone ~ Month, data = airquality)
wilcox.exact(Ozone ~ Month, data = airquality,
            subset = Month %in% c(5, 8))


# Hollander & Wolfe, p. 39, results p. 40 and p. 53

x <- c(1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30)
y <- c(0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29)

wilcox.exact(y,x, paired=TRUE, conf.int=TRUE)

# Hollander & Wolfe, p. 110, results p. 111 and p. 126

x <- c(0.8, 0.83, 1.89, 1.04, 1.45, 1.38, 1.91, 1.64, 0.73, 1.46)
y <- c(1.15, 0.88, 0.90, 0.74, 1.21)

wilcox.exact(y,x, conf.int=TRUE)


kruskal.test R Documentation

Kruskal-Wallis Rank Sum Test

Description

Performs a Kruskal-Wallis rank sum test.

Usage

kruskal.test(x, ...)

## Default S3 method:
kruskal.test(x, g, ...)

## S3 method for class 'formula'
kruskal.test(formula, data, subset, na.action, ...)

Arguments

x

a numeric vector of data values, or a list of numeric data vectors. Non-numeric elements of a list will be coerced, with a warning.

g

a vector or factor object giving the group for the corresponding elements of x. Ignored with a warning if x is a list.

formula

a formula of the form response ~ group where response gives the data values and group a vector or factor of the corresponding groups.

data

an optional matrix or data frame (or similar: see model.frame) containing the variables in the formula formula. By default the variables are taken from environment(formula).

subset

an optional vector specifying a subset of observations to be used.

na.action

a function which indicates what should happen when the data contain NAs. Defaults to getOption(“na.action”).

further arguments to be passed to or from methods.

Details

kruskal.test performs a Kruskal-Wallis rank sum test of the null that the location parameters of the distribution of x are the same in each group (sample). The alternative is that they differ in at least one.

If x is a list, its elements are taken as the samples to be compared, and hence have to be numeric data vectors. In this case, g is ignored, and one can simply use kruskal.test(x) to perform the test. If the samples are not yet contained in a list, use kruskal.test(list(x, …)).

Otherwise, x must be a numeric data vector, and g must be a vector or factor object of the same length as x giving the group for the corresponding elements of x.

Value

A list with class “htest” containing the following components:

statistic

the Kruskal-Wallis rank sum statistic.

parameter

the degrees of freedom of the approximate chi-squared distribution of the test statistic.

p.value

the p-value of the test.

method

the character string “Kruskal-Wallis rank sum test”.

data.name

a character string giving the names of the data.

References

Myles Hollander and Douglas A. Wolfe (1973), Nonparametric Statistical Methods. New York: John Wiley & Sons. Pages 115–120.

See Also

The Wilcoxon rank sum test (wilcox.test) as the special case for two samples; lm together with anova for performing one-way location analysis under normality assumptions; with Student’s t test (t.test) as the special case for two samples.

wilcox_test in package coin for exact, asymptotic and Monte Carlo conditional p-values, including in the presence of ties.

Examples

## Hollander & Wolfe (1973), 116.
## Mucociliary efficiency from the rate of removal of dust in normal
##  subjects, subjects with obstructive airway disease, and subjects
##  with asbestosis.
x <- c(2.9, 3.0, 2.5, 2.6, 3.2) # normal subjects
y <- c(3.8, 2.7, 4.0, 2.4)      # with obstructive airway disease
z <- c(2.8, 3.4, 3.7, 2.2, 2.0) # with asbestosis
kruskal.test(list(x, y, z))
## Equivalently,
x <- c(x, y, z)
g <- factor(rep(1:3, c(5, 4, 5)),
            labels = c("Normal subjects",
                       "Subjects with obstructive airway disease",
                       "Subjects with asbestosis"))
kruskal.test(x, g)

## Formula interface.
require(graphics)
boxplot(Ozone ~ Month, data = airquality)
kruskal.test(Ozone ~ Month, data = airquality)

friedman.test R Documentation

Friedman Rank Sum Test

Description

Performs a Friedman rank sum test with unreplicated blocked data.

Usage

friedman.test(y, ...)

## Default S3 method:
friedman.test(y, groups, blocks, ...)

## S3 method for class 'formula'
friedman.test(formula, data, subset, na.action, ...)

Arguments

y

either a numeric vector of data values, or a data matrix.

groups

a vector giving the group for the corresponding elements of y if this is a vector; ignored if y is a matrix. If not a factor object, it is coerced to one.

blocks

a vector giving the block for the corresponding elements of y if this is a vector; ignored if y is a matrix. If not a factor object, it is coerced to one.

formula

a formula of the form a ~ b | c, where a, b and c give the data values and corresponding groups and blocks, respectively.

data

an optional matrix or data frame (or similar: see model.frame) containing the variables in the formula formula. By default the variables are taken from environment(formula).

subset

an optional vector specifying a subset of observations to be used.

na.action

a function which indicates what should happen when the data contain NAs. Defaults to getOption(“na.action”).

further arguments to be passed to or from methods.

Details

friedman.test can be used for analyzing unreplicated complete block designs (i.e., there is exactly one observation in y for each combination of levels of groups and blocks) where the normality assumption may be violated.

The null hypothesis is that apart from an effect of blocks, the location parameter of y is the same in each of the groups.

If y is a matrix, groups and blocks are obtained from the column and row indices, respectively. NA’s are not allowed in groups or blocks; if y contains NA’s, corresponding blocks are removed.

Value

A list with class “htest” containing the following components:

statistic

the value of Friedman’s chi-squared statistic.

parameter

the degrees of freedom of the approximate chi-squared distribution of the test statistic.

p.value

the p-value of the test.

method

the character string “Friedman rank sum test”.

data.name

a character string giving the names of the data.

References

Myles Hollander and Douglas A. Wolfe (1973), Nonparametric Statistical Methods. New York: John Wiley & Sons. Pages 139–146.

See Also

quade.test.

Examples

## Hollander & Wolfe (1973), p. 140ff.
## Comparison of three methods ("round out", "narrow angle", and
##  "wide angle") for rounding first base.  For each of 18 players
##  and the three method, the average time of two runs from a point on
##  the first base line 35ft from home plate to a point 15ft short of
##  second base is recorded.
RoundingTimes <-
matrix(c(5.40, 5.50, 5.55,
         5.85, 5.70, 5.75,
         5.20, 5.60, 5.50,
         5.55, 5.50, 5.40,
         5.90, 5.85, 5.70,
         5.45, 5.55, 5.60,
         5.40, 5.40, 5.35,
         5.45, 5.50, 5.35,
         5.25, 5.15, 5.00,
         5.85, 5.80, 5.70,
         5.25, 5.20, 5.10,
         5.65, 5.55, 5.45,
         5.60, 5.35, 5.45,
         5.05, 5.00, 4.95,
         5.50, 5.50, 5.40,
         5.45, 5.55, 5.50,
         5.55, 5.55, 5.35,
         5.45, 5.50, 5.55,
         5.50, 5.45, 5.25,
         5.65, 5.60, 5.40,
         5.70, 5.65, 5.55,
         6.30, 6.30, 6.25),
       nrow = 22,
       byrow = TRUE,
       dimnames = list(1 : 22,
                       c("Round Out", "Narrow Angle", "Wide Angle")))
friedman.test(RoundingTimes)
## => strong evidence against the null that the methods are equivalent
##    with respect to speed

wb <- aggregate(warpbreaks$breaks,
                by = list(w = warpbreaks$wool,
                          t = warpbreaks$tension),
                FUN = mean)
wb
friedman.test(wb$x, wb$w, wb$t)
friedman.test(x ~ w | t, data = wb)

prop.test R Documentation

Test of Equal or Given Proportions

Description

prop.test can be used for testing the null that the proportions (probabilities of success) in several groups are the same, or that they equal certain given values.

Usage

prop.test(x, n, p = NULL,
          alternative = c("two.sided", "less", "greater"),
          conf.level = 0.95, correct = TRUE)

Arguments

x

a vector of counts of successes, a one-dimensional table with two entries, or a two-dimensional table (or matrix) with 2 columns, giving the counts of successes and failures, respectively.

n

a vector of counts of trials; ignored if x is a matrix or a table.

p

a vector of probabilities of success. The length of p must be the same as the number of groups specified by x, and its elements must be greater than 0 and less than 1.

alternative

a character string specifying the alternative hypothesis, must be one of “two.sided” (default), “greater” or “less”. You can specify just the initial letter. Only used for testing the null that a single proportion equals a given value, or that two proportions are equal; ignored otherwise.

conf.level

confidence level of the returned confidence interval. Must be a single number between 0 and 1. Only used when testing the null that a single proportion equals a given value, or that two proportions are equal; ignored otherwise.

correct

a logical indicating whether Yates’ continuity correction should be applied where possible.

Details

Only groups with finite numbers of successes and failures are used. Counts of successes and failures must be nonnegative and hence not greater than the corresponding numbers of trials which must be positive. All finite counts should be integers.

If p is NULL and there is more than one group, the null tested is that the proportions in each group are the same. If there are two groups, the alternatives are that the probability of success in the first group is less than, not equal to, or greater than the probability of success in the second group, as specified by alternative. A confidence interval for the difference of proportions with confidence level as specified by conf.level and clipped to [-1,1] is returned. Continuity correction is used only if it does not exceed the difference of the sample proportions in absolute value. Otherwise, if there are more than 2 groups, the alternative is always “two.sided”, the returned confidence interval is NULL, and continuity correction is never used.

If there is only one group, then the null tested is that the underlying probability of success is p, or .5 if p is not given. The alternative is that the probability of success is less than, not equal to, or greater than p or 0.5, respectively, as specified by alternative. A confidence interval for the underlying proportion with confidence level as specified by conf.level and clipped to [0,1] is returned. Continuity correction is used only if it does not exceed the difference between sample and null proportions in absolute value. The confidence interval is computed by inverting the score test.

Finally, if p is given and there are more than 2 groups, the null tested is that the underlying probabilities of success are those given by p. The alternative is always “two.sided”, the returned confidence interval is NULL, and continuity correction is never used.

Value

A list with class “htest” containing the following components:

statistic

the value of Pearson’s chi-squared test statistic.

parameter

the degrees of freedom of the approximate chi-squared distribution of the test statistic.

p.value

the p-value of the test.

estimate

a vector with the sample proportions x/n.

conf.int

a confidence interval for the true proportion if there is one group, or for the difference in proportions if there are 2 groups and p is not given, or NULL otherwise. In the cases where it is not NULL, the returned confidence interval has an asymptotic confidence level as specified by conf.level, and is appropriate to the specified alternative hypothesis.

null.value

the value of p if specified by the null, or NULL otherwise.

alternative

a character string describing the alternative.

method

a character string indicating the method used, and whether Yates’ continuity correction was applied.

data.name

a character string giving the names of the data.

References

Wilson, E.B. (1927). Probable inference, the law of succession, and statistical inference. Journal of the American Statistical Association, 22, 209–212. doi: 10.2307/2276774.

Newcombe R.G. (1998). Two-Sided Confidence Intervals for the Single Proportion: Comparison of Seven Methods. Statistics in Medicine, 17, 857–872. doi: 10.1002/(SICI)1097-0258(19980430)17:8<857::AID-SIM777>3.0.CO;2-E.

Newcombe R.G. (1998). Interval Estimation for the Difference Between Independent Proportions: Comparison of Eleven Methods. Statistics in Medicine, 17, 873–890. doi: 10.1002/(SICI)1097-0258(19980430)17:8<873::AID-SIM779>3.0.CO;2-I.

See Also

binom.test for an exact test of a binomial hypothesis.

Examples

heads <- rbinom(1, size = 100, prob = .5)
prop.test(heads, 100)          # continuity correction TRUE by default
prop.test(heads, 100, correct = FALSE)

## Data from Fleiss (1981), p. 139.
## H0: The null hypothesis is that the four populations from which
##     the patients were drawn have the same true proportion of smokers.
## A:  The alternative is that this proportion is different in at
##     least one of the populations.

smokers  <- c( 83, 90, 129, 70 )
patients <- c( 86, 93, 136, 82 )
prop.test(smokers, patients)

binom.test R Documentation

Exact Binomial Test

Description

Performs an exact test of a simple null hypothesis about the probability of success in a Bernoulli experiment.

Usage

binom.test(x, n, p = 0.5,
           alternative = c("two.sided", "less", "greater"),
           conf.level = 0.95)

Arguments

x

number of successes, or a vector of length 2 giving the numbers of successes and failures, respectively.

n

number of trials; ignored if x has length 2.

p

hypothesized probability of success.

alternative

indicates the alternative hypothesis and must be one of “two.sided”, “greater” or “less”. You can specify just the initial letter.

conf.level

confidence level for the returned confidence interval.

Details

Confidence intervals are obtained by a procedure first given in Clopper and Pearson (1934). This guarantees that the confidence level is at least conf.level, but in general does not give the shortest-length confidence intervals.

Value

A list with class “htest” containing the following components:

statistic

the number of successes.

parameter

the number of trials.

p.value

the p-value of the test.

conf.int

a confidence interval for the probability of success.

estimate

the estimated probability of success.

null.value

the probability of success under the null, p.

alternative

a character string describing the alternative hypothesis.

method

the character string “Exact binomial test”.

data.name

a character string giving the names of the data.

References

Clopper, C. J. & Pearson, E. S. (1934). The use of confidence or fiducial limits illustrated in the case of the binomial. Biometrika, 26, 404–413. doi: 10.2307/2331986.

William J. Conover (1971), Practical nonparametric statistics. New York: John Wiley & Sons. Pages 97–104.

Myles Hollander & Douglas A. Wolfe (1973), Nonparametric Statistical Methods. New York: John Wiley & Sons. Pages 15–22.

See Also

prop.test for a general (approximate) test for equal or given proportions.

Examples

## Conover (1971), p. 97f.
## Under (the assumption of) simple Mendelian inheritance, a cross
##  between plants of two particular genotypes produces progeny 1/4 of
##  which are "dwarf" and 3/4 of which are "giant", respectively.
##  In an experiment to determine if this assumption is reasonable, a
##  cross results in progeny having 243 dwarf and 682 giant plants.
##  If "giant" is taken as success, the null hypothesis is that p =
##  3/4 and the alternative that p != 3/4.
binom.test(c(682, 243), p = 3/4)
binom.test(682, 682 + 243, p = 3/4)   # The same.
## => Data are in agreement with the null hypothesis.

fisher.test R Documentation

Fisher’s Exact Test for Count Data

Description

Performs Fisher’s exact test for testing the null of independence of rows and columns in a contingency table with fixed marginals.

Usage

fisher.test(x, y = NULL, workspace = 200000, hybrid = FALSE,
            hybridPars = c(expect = 5, percent = 80, Emin = 1),
            control = list(), or = 1, alternative = "two.sided",
            conf.int = TRUE, conf.level = 0.95,
            simulate.p.value = FALSE, B = 2000)

Arguments

x

either a two-dimensional contingency table in matrix form, or a factor object.

y

a factor object; ignored if x is a matrix.

workspace

an integer specifying the size of the workspace used in the network algorithm. In units of 4 bytes. Only used for non-simulated p-values larger than 2 by 2 tables. Since R version 3.5.0, this also increases the internal stack size which allows larger problems to be solved, however sometimes needing hours. In such cases, simulate.p.values=TRUE may be more reasonable.

hybrid

a logical. Only used for larger than 2 by 2 tables, in which cases it indicates whether the exact probabilities (default) or a hybrid approximation thereof should be computed.

hybridPars

a numeric vector of length 3, by default describing “Cochran’s conditions” for the validity of the chisquare approximation, see ‘Details’.

control

a list with named components for low level algorithm control. At present the only one used is “mult”, a positive integer ≥ 2 with default 30 used only for larger than 2 by 2 tables. This says how many times as much space should be allocated to paths as to keys: see file ‘fexact.c’ in the sources of this package.

or

the hypothesized odds ratio. Only used in the 2 by 2 case.

alternative

indicates the alternative hypothesis and must be one of “two.sided”, “greater” or “less”. You can specify just the initial letter. Only used in the 2 by 2 case.

conf.int

logical indicating if a confidence interval for the odds ratio in a 2 by 2 table should be computed (and returned).

conf.level

confidence level for the returned confidence interval. Only used in the 2 by 2 case and if conf.int = TRUE.

simulate.p.value

a logical indicating whether to compute p-values by Monte Carlo simulation, in larger than 2 by 2 tables.

B

an integer specifying the number of replicates used in the Monte Carlo test.

Details

If x is a matrix, it is taken as a two-dimensional contingency table, and hence its entries should be nonnegative integers. Otherwise, both x and y must be vectors of the same length. Incomplete cases are removed, the vectors are coerced into factor objects, and the contingency table is computed from these.

For 2 by 2 cases, p-values are obtained directly using the (central or non-central) hypergeometric distribution. Otherwise, computations are based on a C version of the FORTRAN subroutine FEXACT which implements the network developed by Mehta and Patel (1983, 1986) and improved by Clarkson, Fan and Joe (1993). The FORTRAN code can be obtained from http://www.netlib.org/toms/643. Note this fails (with an error message) when the entries of the table are too large. (It transposes the table if necessary so it has no more rows than columns. One constraint is that the product of the row marginals be less than 2^31 - 1.)

For 2 by 2 tables, the null of conditional independence is equivalent to the hypothesis that the odds ratio equals one. ‘Exact’ inference can be based on observing that in general, given all marginal totals fixed, the first element of the contingency table has a non-central hypergeometric distribution with non-centrality parameter given by the odds ratio (Fisher, 1935). The alternative for a one-sided test is based on the odds ratio, so alternative = “greater” is a test of the odds ratio being bigger than or.

Two-sided tests are based on the probabilities of the tables, and take as ‘more extreme’ all tables with probabilities less than or equal to that of the observed table, the p-value being the sum of such probabilities.

For larger than 2 by 2 tables and hybrid = TRUE, asymptotic chi-squared probabilities are only used if the ‘Cochran conditions’ (or modified version thereof) specified by hybridPars = c(expect = 5, percent = 80, Emin = 1) are satisfied, that is if no cell has expected counts less than 1 (= Emin) and more than 80% (= percent) of the cells have expected counts at least 5 (= expect), otherwise the exact calculation is used. A corresponding if() decision is made for all sub-tables considered.

Accidentally, R has used 180 instead of 80 as percent, i.e., hybridPars[2] in R versions between 3.0.0 and 3.4.1 (inclusive), i.e., the 2nd of the hybridPars (all of which used to be hard-coded previous to R 3.5.0). Consequently, in these versions of R, hybrid=TRUE never made a difference.

In the r x c case with r > 2 or c > 2, internal tables can get too large for the exact test in which case an error is signalled. Apart from increasing workspace sufficiently, which then may lead to very long running times, using simulate.p.value = TRUE may then often be sufficient and hence advisable.

Simulation is done conditional on the row and column marginals, and works only if the marginals are strictly positive. (A C translation of the algorithm of Patefield (1981) is used.)

Value

A list with class “htest” containing the following components:

p.value

the p-value of the test.

conf.int

a confidence interval for the odds ratio. Only present in the 2 by 2 case and if argument conf.int = TRUE.

estimate

an estimate of the odds ratio. Note that the conditional Maximum Likelihood Estimate (MLE) rather than the unconditional MLE (the sample odds ratio) is used. Only present in the 2 by 2 case.

null.value

the odds ratio under the null, or. Only present in the 2 by 2 case.

alternative

a character string describing the alternative hypothesis.

method

the character string “Fisher’s Exact Test for Count Data”.

data.name

a character string giving the names of the data.

References

Agresti, A. (1990). Categorical data analysis. New York: Wiley. Pages 59–66.

Agresti, A. (2002). Categorical data analysis. Second edition. New York: Wiley. Pages 91–101.

Fisher, R. A. (1935). The logic of inductive inference. Journal of the Royal Statistical Society Series A, 98, 39–54. doi: 10.2307/2342435.

Fisher, R. A. (1962). Confidence limits for a cross-product ratio. Australian Journal of Statistics, 4, 41. doi: 10.1111/j.1467-842X.1962.tb00285.x.

Fisher, R. A. (1970). Statistical Methods for Research Workers. Oliver & Boyd.

Mehta, Cyrus R. and Patel, Nitin R. (1983). A network algorithm for performing Fisher’s exact test in r x c contingency tables. Journal of the American Statistical Association, 78, 427–434. doi: 10.1080/01621459.1983.10477989.

Mehta, C. R. and Patel, N. R. (1986). Algorithm 643: FEXACT, a FORTRAN subroutine for Fisher’s exact test on unordered r x c contingency tables. ACM Transactions on Mathematical Software, 12, 154–161. doi: 10.1145/6497.214326.

Clarkson, D. B., Fan, Y. and Joe, H. (1993) A Remark on Algorithm 643: FEXACT: An Algorithm for Performing Fisher’s Exact Test in r x c Contingency Tables. ACM Transactions on Mathematical Software, 19, 484–488. doi: 10.1145/168173.168412.

Patefield, W. M. (1981). Algorithm AS 159: An efficient method of generating r x c tables with given row and column totals. Applied Statistics, 30, 91–97. doi: 10.2307/2346669.

See Also

chisq.test

fisher.exact in package exact2x2 for alternative interpretations of two-sided tests and confidence intervals for 2 by 2 tables.

Examples

## Agresti (1990, p. 61f; 2002, p. 91) Fisher's Tea Drinker
## A British woman claimed to be able to distinguish whether milk or
##  tea was added to the cup first.  To test, she was given 8 cups of
##  tea, in four of which milk was added first.  The null hypothesis
##  is that there is no association between the true order of pouring
##  and the woman's guess, the alternative that there is a positive
##  association (that the odds ratio is greater than 1).
TeaTasting <-
matrix(c(3, 1, 1, 3),
       nrow = 2,
       dimnames = list(Guess = c("Milk", "Tea"),
                       Truth = c("Milk", "Tea")))
fisher.test(TeaTasting, alternative = "greater")
## => p = 0.2429, association could not be established

## Fisher (1962, 1970), Criminal convictions of like-sex twins
Convictions <- matrix(c(2, 10, 15, 3), nrow = 2,
                  dimnames =
           list(c("Dizygotic", "Monozygotic"),
            c("Convicted", "Not convicted")))
Convictions
fisher.test(Convictions, alternative = "less")
fisher.test(Convictions, conf.int = FALSE)
fisher.test(Convictions, conf.level = 0.95)$conf.int
fisher.test(Convictions, conf.level = 0.99)$conf.int

## A r x c table  Agresti (2002, p. 57) Job Satisfaction
Job <- matrix(c(1,2,1,0, 3,3,6,1, 10,10,14,9, 6,7,12,11), 4, 4,
           dimnames = list(income = c("< 15k", "15-25k", "25-40k", "> 40k"),
                     satisfaction = c("VeryD", "LittleD", "ModerateS", "VeryS")))
fisher.test(Job) # 0.7827
fisher.test(Job, simulate.p.value = TRUE, B = 1e5) # also close to 0.78

## 6th example in Mehta & Patel's JASA paper
MP6 <- rbind(
        c(1,2,2,1,1,0,1),
        c(2,0,0,2,3,0,0),
        c(0,1,1,1,2,7,3),
        c(1,1,2,0,0,0,1),
        c(0,1,1,1,1,0,0))
fisher.test(MP6)
# Exactly the same p-value, as Cochran's conditions are never met:
fisher.test(MP6, hybrid=TRUE)

mcnemar.test R Documentation

McNemar’s Chi-squared Test for Count Data

Description

Performs McNemar’s chi-squared test for symmetry of rows and columns in a two-dimensional contingency table.

Usage

mcnemar.test(x, y = NULL, correct = TRUE)

Arguments

x

either a two-dimensional contingency table in matrix form, or a factor object.

y

a factor object; ignored if x is a matrix.

correct

a logical indicating whether to apply continuity correction when computing the test statistic.

Details

The null is that the probabilities of being classified into cells [i,j] and [j,i] are the same.

If x is a matrix, it is taken as a two-dimensional contingency table, and hence its entries should be nonnegative integers. Otherwise, both x and y must be vectors or factors of the same length. Incomplete cases are removed, vectors are coerced into factors, and the contingency table is computed from these.

Continuity correction is only used in the 2-by-2 case if correct is TRUE.

Value

A list with class “htest” containing the following components:

statistic

the value of McNemar’s statistic.

parameter

the degrees of freedom of the approximate chi-squared distribution of the test statistic.

p.value

the p-value of the test.

method

a character string indicating the type of test performed, and whether continuity correction was used.

data.name

a character string giving the name(s) of the data.

References

Alan Agresti (1990). Categorical data analysis. New York: Wiley. Pages 350–354.

Examples

## Agresti (1990), p. 350.
## Presidential Approval Ratings.
##  Approval of the President's performance in office in two surveys,
##  one month apart, for a random sample of 1600 voting-age Americans.
Performance <-
matrix(c(794, 86, 150, 570),
       nrow = 2,
       dimnames = list("1st Survey" = c("Approve", "Disapprove"),
                       "2nd Survey" = c("Approve", "Disapprove")))
Performance
mcnemar.test(Performance)
## => significant change (in fact, drop) in approval ratings

fisher.multcomp R Documentation

Pairwise comparisons using Fisher’s exact test

Description

Performs pairwise comparisons after a comparison of proportions or after a test for independence of 2 categorical variables, by using a Fisher’s exact test.

Usage

fisher.multcomp(tab.cont, p.method = "fdr")

Arguments

tab.cont

contingency table.

p.method

method for p-values correction. See help of p.adjust.

Details

Since chi-squared and G tests are approximate tests, exact tests are preferable when the number of individuals is small (200 is a reasonable minimum).

Value

method

name of the test.

data.name

a character string giving the name(s) of the data.

p.adjust.method

method for p-values correction.

p.value

table of results of pairwise comparisons.

Author(s)

Maxime Hervテゥ <mx.herve@gmail.com>;

See Also

chisq.test, prop.test, fisher.test

Examples

# 2-column contingency table: comparison of proportions
tab.cont1 <- matrix(c(17,23,12,24,20,10),ncol=2,dimnames=list(c("Control",
  "Treatment1","Treatment2"),c("Alive","Dead")),byrow=TRUE)
fisher.test(tab.cont1)
fisher.multcomp(tab.cont1)

# 3-column contingency table: independence test
tab.cont2 <- as.table(matrix(c(25,10,12,6,15,14,9,16,9),ncol=3,dimnames=list(c("fair",
  "dark","russet"),c("blue","brown","green"))))
fisher.test(tab.cont2)
fisher.multcomp(tab.cont2)

cochrans.q R Documentation

Cochran’s Q Test

Description

This function will perform the Cochran’s Q Test to test for identical treatment effects in a two-way randomized block design with k treatments.

Usage

  cochrans.q(x, alpha=NULL)

Arguments

x

A b x k matrix, where b is the number of blocking factors and k is the number of treatment factors.

alpha

The Significance level, defaults to 0.05.

Value

Q

This is the Cochran’s Q Test Statistic.

Degrees of Freedom

The number of degrees of freedom used in calculating the p-value.

Significance Level

Returns the alpha value.

P-value

Returns the p-value from the Cochran’s Q Test.

Author(s)

D. Lukke Sweet

References

https://www.r-bloggers.com/cochran-q-test-for-k-related-samples-in-r/

http://rcompanion.org/handbook/H_07.html

Examples

  ## Run Cochran's Q Test on a matrix.
  cochrans.q(matrix(c(1,1,1,1,1,1,
              1,1,0,1,1,1,
              0,0,0,1,0,0,
              0,1,0,0,1,1), 6, 4))

  ## Cochran's Q Test works for any size matrix.
  cochrans.q(matrix(c(0,1,0,0,1,0,0,0,1,0,0,0,0,0,
                0,1,1,1,1,1,1,1,0,1,1,1,1,1,
                0,1,0,0,0,0,0,0,1,0,0,0,0,0,
                0,1,1,0,0,1,1,0,0,0,0,1,0,1), 14, 4), alpha=0.01)


var.test R Documentation

F Test to Compare Two Variances

Description

Performs an F test to compare the variances of two samples from normal populations.

Usage

var.test(x, ...)

## Default S3 method:
var.test(x, y, ratio = 1,
         alternative = c("two.sided", "less", "greater"),
         conf.level = 0.95, ...)

## S3 method for class 'formula'
var.test(formula, data, subset, na.action, ...)

Arguments

x, y

numeric vectors of data values, or fitted linear model objects (inheriting from class “lm”).

ratio

the hypothesized ratio of the population variances of x and y.

alternative

a character string specifying the alternative hypothesis, must be one of “two.sided” (default), “greater” or “less”. You can specify just the initial letter.

conf.level

confidence level for the returned confidence interval.

formula

a formula of the form lhs ~ rhs where lhs is a numeric variable giving the data values and rhs a factor with two levels giving the corresponding groups.

data

an optional matrix or data frame (or similar: see model.frame) containing the variables in the formula formula. By default the variables are taken from environment(formula).

subset

an optional vector specifying a subset of observations to be used.

na.action

a function which indicates what should happen when the data contain NAs. Defaults to getOption(“na.action”).

further arguments to be passed to or from methods.

Details

The null hypothesis is that the ratio of the variances of the populations from which x and y were drawn, or in the data to which the linear models x and y were fitted, is equal to ratio.

Value

A list with class “htest” containing the following components:

statistic

the value of the F test statistic.

parameter

the degrees of the freedom of the F distribution of the test statistic.

p.value

the p-value of the test.

conf.int

a confidence interval for the ratio of the population variances.

estimate

the ratio of the sample variances of x and y.

null.value

the ratio of population variances under the null.

alternative

a character string describing the alternative hypothesis.

method

the character string “F test to compare two variances”.

data.name

a character string giving the names of the data.

See Also

bartlett.test for testing homogeneity of variances in more than two samples from normal distributions; ansari.test and mood.test for two rank based (nonparametric) two-sample tests for difference in scale.

Examples

x <- rnorm(50, mean = 0, sd = 2)
y <- rnorm(30, mean = 1, sd = 1)
var.test(x, y)                  # Do x and y have the same variance?
var.test(lm(x ~ 1), lm(y ~ 1))  # The same.

bartlett.test R Documentation

Bartlett Test of Homogeneity of Variances

Description

Performs Bartlett’s test of the null that the variances in each of the groups (samples) are the same.

Usage

bartlett.test(x, ...)

## Default S3 method:
bartlett.test(x, g, ...)

## S3 method for class 'formula'
bartlett.test(formula, data, subset, na.action, ...)

Arguments

x

a numeric vector of data values, or a list of numeric data vectors representing the respective samples, or fitted linear model objects (inheriting from class “lm”).

g

a vector or factor object giving the group for the corresponding elements of x. Ignored if x is a list.

formula

a formula of the form lhs ~ rhs where lhs gives the data values and rhs the corresponding groups.

data

an optional matrix or data frame (or similar: see model.frame) containing the variables in the formula formula. By default the variables are taken from environment(formula).

subset

an optional vector specifying a subset of observations to be used.

na.action

a function which indicates what should happen when the data contain NAs. Defaults to getOption(“na.action”).

further arguments to be passed to or from methods.

Details

If x is a list, its elements are taken as the samples or fitted linear models to be compared for homogeneity of variances. In this case, the elements must either all be numeric data vectors or fitted linear model objects, g is ignored, and one can simply use bartlett.test(x) to perform the test. If the samples are not yet contained in a list, use bartlett.test(list(x, …)).

Otherwise, x must be a numeric data vector, and g must be a vector or factor object of the same length as x giving the group for the corresponding elements of x.

Value

A list of class “htest” containing the following components:

statistic

Bartlett’s K-squared test statistic.

parameter

the degrees of freedom of the approximate chi-squared distribution of the test statistic.

p.value

the p-value of the test.

method

the character string “Bartlett test of homogeneity of variances”.

data.name

a character string giving the names of the data.

References

Bartlett, M. S. (1937). Properties of sufficiency and statistical tests. Proceedings of the Royal Society of London Series A 160, 268–282. doi: 10.1098/rspa.1937.0109.

See Also

var.test for the special case of comparing variances in two samples from normal distributions; fligner.test for a rank-based (nonparametric) k-sample test for homogeneity of variances; ansari.test and mood.test for two rank based two-sample tests for difference in scale.

Examples

require(graphics)

plot(count ~ spray, data = InsectSprays)
bartlett.test(InsectSprays$count, InsectSprays$spray)
bartlett.test(count ~ spray, data = InsectSprays)

brunner.munzel.test R Documentation

Brunner–Munzel Test for Stochastic Equality

Description

The Brunner–Munzel test for stochastic equality of two samples, which is also known as the Generalized Wilcoxon Test. NAs from the data are omitted.

Usage

brunner.munzel.test(x, y, alternative = c("two.sided", "greater",
  "less"), alpha = 0.05)

Arguments

x

the numeric vector of data values from the sample 1.

y

the numeric vector of data values from the sample 2.

alternative

a character string specifying the alternative hypothesis, must be one of “two.sided” (default), “greater” or “less”. User can specify just the initial letter.

alpha

significance level, default is 0.05 for 95% confidence interval.

Details

There exist discrepancies with Brunner and Munzel (2000) because there is a typo in the paper. The corrected version is in Neubert and Brunner (2007) (e.g., compare the estimates for the case study on pain scores). The current function follows Neubert and Brunner (2007).

Value

A list of class “htest” with the following components:

statistic

the Brunner–Munzel test statistic.

parameter

the degrees of freedom.

conf.int

the confidence interval.

p.value

the p-value of the test.

data.name

a character string giving the name of the data.

estimate

an estimate of the effect size, i.e., P(X < Y) + 0.5 P(X =Y ).

Author(s)

Wallace Hui, Yulia R. Gel, Joseph L. Gastwirth, Weiwen Miao. This function was updated with the help of Dr. Ian Fellows.

References

Brunner E, Munzel U (2000). “The nonparametric Behrens–Fisher problem: asymptotic theory and a small-sample approximation.” Biometrical Journal, 42(1), 17–25.

Neubert K, Brunner E (2007). “A studentized permutation test for the non-parametric Behrens–Fisher problem.” Computational Statistics & Data Analysis, 51(10), 5192–5204. doi: 10.1016/j.csda.2006.05.024.

See Also

wilcox.test, pwilcox

Examples

## Pain score on the third day after surgery for 14 patients under
## the treatment Y and 11 patients under the treatment N
## (see Brunner and Munzel, 2000; Neubert and Brunner, 2007).

Y <- c(1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 4, 1, 1)
N <- c(3, 3, 4, 3, 1, 2, 3, 1, 1, 5, 4)

brunner.munzel.test(Y, N)

##       Brunner-Munzel Test
## data: Y and N
## Brunner-Munzel Test Statistic = 3.1375,  df = 17.683, p-value = 0.005786
## 95 percent confidence interval:
##  0.5952169 0.9827052
## sample estimates:
## P(X<Y)+.5*P(X=Y)
##        0.788961


levene.test R Documentation

Levene’s Test of Equality of Variances

Description

Tests equality of the k population variances.

Usage

levene.test(y, group, location = c("median", "mean", "trim.mean"),
  trim.alpha = 0.25, bootstrap = FALSE, num.bootstrap = 1000,
  kruskal.test = FALSE, correction.method = c("none",
  "correction.factor", "zero.removal", "zero.correction"))

Arguments

y

a numeric vector of data values.

group

factor of the data.

location

the default option is “median” corresponding to the robust Brown–Forsythe Levene-type procedure (Brown and Forsythe 1974); “mean” corresponds to the classical Levene’s procedure (Levene 1960), and “trim.mean” corresponds to the robust Levene-type procedure using the group trimmed means.

trim.alpha

the fraction (0 to 0.5) of observations to be trimmed from each end of x before the mean is computed.

bootstrap

a logical value identifying whether to implement bootstrap. The default is FALSE, i.e., no bootstrap; if set to TRUE, the bootstrap method described in Lim and Loh (1996) for Levene’s test is applied.

num.bootstrap

number of bootstrap samples to be drawn when the bootstrap argument is set to TRUE. The default value is 1000.

kruskal.test

logical value indentifying whether to use the Kruskal–Wallis statistic. The default option is FALSE, i.e., the usual ANOVA statistic is used.

correction.method

procedures to make the test more robust; the default option is “none”; “correction.factor” applies the correction factor described by O’Brien (1978) and Keyes and Levy (1997); “zero.removal” performs the structural zero removal method by Hines and Hines (2000); “zero.correction” performs a combination of the O’Brien’s correction factor and the Hines–Hines structural zero removal method (Noguchi and Gel 2010). Note that the options “zero.removal” and “zero.correction” are only applicable when the location is set to “median”, otherwise, “none” is applied.

Details

The test statistic is based on the classical Levene’s procedure (using the group means), the modified Brown–Forsythe Levene-type procedure (using the group medians), or the modified Levene-type procedure (using the group trimmed means). More robust versions of the test using the correction factor or structural zero removal method are also available. Two options for calculating critical values, namely, approximated and bootstrapped, are available. By default, NAs are omitted from the data.

Value

A list of class “htest” with the following components:

statistic

the value of the test statistic.

p.value

the p-value of the test.

method

type of test performed.

data.name

a character string giving the name of the data.

non.bootstrap.p.value

the p-value of the test without bootstrap method; i.e. the p-value using the approximated critical value.

Note

Instead of the ANOVA statistic suggested by Levene, the Kruskal–Wallis ANOVA may also be applied using this function (see the parameter kruskal.test).

Modified from a response posted by Brian Ripley to the R-help e-mail list.

Author(s)

Kimihiro Noguchi, W. Wallace Hui, Yulia R. Gel, Joseph L. Gastwirth, Weiwen Miao

References

Brown MB, Forsythe AB (1974). “Robust tests for the equality of variances.” Journal of the American Statistical Association, 69(346), 364–367. doi: 10.1080/01621459.1974.10482955.

Hines WGS, Hines RJO (2000). “Increased power with modified forms of the Levene (Med) test for heterogeneity of variance.” Biometrics, 56(2), 451–454. doi: 10.1111/j.0006-341X.2000.00451.x.

Keyes TK, Levy MS (1997). “Analysis of Levene’s test under design imbalance.” Journal of Educational and Behavioral Statistics, 22(2), 227–236. doi: 10.3102/10769986022002227.

Levene H (1960). “Robust Tests for Equality of Variances.” In Olkin I, others (eds.), Contributions to Probability and Statistics: Essays in Honor of Harold Hotelling. Stanford University Press, Palo Alto, CA.

Lim T, Loh W (1996). “A comparison of tests of equality of variances.” Computational Statistics & Data Analysis, 22(3), 287–301. doi: 10.1016/0167-9473(95)00054-2.

Noguchi K, Gel YR (2010). “Combination of Levene-type tests and a finite-intersection method for testing equality of variances against ordered alternatives.” Journal of Nonparametric Statistics, 22(7), 897–913. doi: 10.1080/10485251003698505.

O’Brien RG (1978). “Robust techniques for testing heterogeneity of variance effects in factorial designs.” Psychometrika, 43(3), 327–342. doi: 10.1007/BF02293643.

See Also

neuhauser.hothorn.test, lnested.test, ltrend.test, mma.test, robust.mmm.test

Examples

data(pot)
levene.test(pot[,"obs"], pot[,"type"], 
            location = "median", correction.method = "zero.correction")
            
## Bootstrap version of the test. The calculation may take up a few minutes 
## depending on the number of bootstrap sampling.
levene.test(pot[,"obs"], pot[,"type"], 
            location = "median", correction.method = "zero.correction", 
            bootstrap = TRUE, num.bootstrap = 500)