第I部

第5章 統計的仮設検定

In [1]:
options(repr.plot.width= 4, repr.plot.height = 4)

5.3 標準正規分布を用いた検定

  • 検定統計量は $Z = \frac{\bar{X} - \mu}{\sigma / \sqrt{n}}$

例題

  • 心理学テストの母集団分布は $\bar{X} \approx N \left( \mu, \frac{\sigma^2}{n}\right) $
  • 標準化すると, $Z = \frac{\bar{X} - \mu}{\sigma / \sqrt{n}} \approx N(0, 1)$
In [2]:
library(readr)
In [3]:
d <- read_csv("teaching_methods.csv") # UTF-8
d
ID名前性別数学統計心理学テスト統計テスト1統計テスト2指導法
11大村嫌い好き13610C
22本多嫌い好き141013B
33川崎好き好き768B
44多村好き好き121015A
55松中嫌い嫌い1058B
66小久保嫌い嫌い636C
77柴原嫌い嫌い859A
88井手嫌い嫌い15910D
99田上嫌い嫌い437D
1010松田好き嫌い1433D
1111高谷好き好き91118A
1212杉内嫌い好き6614A
1313和田好き好き101118A
1414新垣嫌い嫌い12911C
1515大隣嫌い好き5712B
1616水田好き嫌い1255D
1717斉藤嫌い嫌い887C
1818柳瀬嫌い嫌い8712C
1919佐藤嫌い嫌い1277B
2020馬原嫌い嫌い1597D
In [4]:
names(d) <- c("id", "name", "gender", "math", "stat", "psy_test", "stat_test1", "stat_test2", "teach_method")
d
idnamegendermathstatpsy_teststat_test1stat_test2teach_method
11大村嫌い好き13610C
22本多嫌い好き141013B
33川崎好き好き768B
44多村好き好き121015A
55松中嫌い嫌い1058B
66小久保嫌い嫌い636C
77柴原嫌い嫌い859A
88井手嫌い嫌い15910D
99田上嫌い嫌い437D
1010松田好き嫌い1433D
1111高谷好き好き91118A
1212杉内嫌い好き6614A
1313和田好き好き101118A
1414新垣嫌い嫌い12911C
1515大隣嫌い好き5712B
1616水田好き嫌い1255D
1717斉藤嫌い嫌い887C
1818柳瀬嫌い嫌い8712C
1919佐藤嫌い嫌い1277B
2020馬原嫌い嫌い1597D
In [5]:
psy_test <- d$psy_test

(4) 統計検定量の計算

In [6]:
z <- (mean(psy_test) - 12) / (sqrt(10 / length(psy_test)))
z
-2.82842712474619

棄却域を計算する
上側確率は,

In [7]:
qnorm(1 - 0.05 / 2)
1.95996398454005

下側確率は,

In [8]:
qnorm(0.05 / 2)
-1.95996398454005

棄却域は,
$Z \lt -1.95996$, $Z \gt 1.95996$

In [9]:
library(ggplot2)
In [10]:
ggplot(data.frame(x = c(-3, 3)), aes(x)) +
    stat_function(fun = dnorm, args = list(mean = 0, sd = 1)) + 
    scale_x_continuous(breaks = c(-3:3, 1)) + 
    geom_vline(xintercept = c(qnorm(0.025), qnorm(0.975)))

$p$値を計算する.
検定統計量z が左側の棄却域に入る確率は,

In [11]:
pnorm(z)
0.00233886749052363

両側検定なので,

In [12]:
2 * pnorm(-z, lower.tail = FALSE)
0.00467773498104727

$p$値が有意水準0.05より小さいので,帰無仮説は棄却される.
(標本平均は母平均と等しいとは言えない)

5.4 $t$分布 を用いた検定

$t = \frac{\bar{X} - \mu}{\hat{\sigma} / \sqrt{n}}$

In [13]:
ggplot(data.frame(x = c(-5, 5)), aes(x)) +
    stat_function(fun = dt, args = list(df = 1)) + 
    stat_function(fun = dt, args = list(df = 2), col = "green") + 
    stat_function(fun = dt, args = list(df = 4), col = "red") + 
    stat_function(fun = dt, args = list(df = 8), col = "blue") + 
    scale_x_continuous(breaks = c(-4:4, 1))

母集団分布は, $N(12, \sigma^2)$
検定統計量$t$は,

In [14]:
t <- (mean(psy_test) - 12) / sqrt(var(psy_test) / length(psy_test))
t
-2.61664801737774

棄却域は,

In [15]:
lower <- qt(0.05 / 2, df = length(psy_test) - 1)
upper <- qt(0.05 / 2, df = length(psy_test) - 1, lower.tail = FALSE)
cat("t < ", lower, ", t > ", upper)
t <  -2.093024 , t >  2.093024
In [16]:
ggplot(data.frame(x = c(-3, 3)), aes(x)) +
    stat_function(fun = dt, args = list(df = 19)) + 
    scale_x_continuous(breaks = c(-3:3, 1)) + 
    geom_vline(xintercept = c(qt(0.025, df = 19), qt(0.975, df = 19)))

帰無仮説は棄却された

$p$ 値は,

In [17]:
2 * pt(t, df = length(psy_test) - 1)
0.0169709202695634

t.test() で計算

In [18]:
t.test(psy_test, mu = 12)
	One Sample t-test

data:  psy_test
t = -2.6166, df = 19, p-value = 0.01697
alternative hypothesis: true mean is not equal to 12
95 percent confidence interval:
  8.400225 11.599775
sample estimates:
mean of x 
       10 

5.5 相関係数の検定

H0: 母集団において相間が0である($\rho = 0$)

$t = \frac{r\sqrt{n - 2}}{\sqrt{1 - r^2}}$

In [19]:
stat_test1 <- d$stat_test1
stat_test2 <- d$stat_test2

検定統計量は,

In [20]:
r <- cor(stat_test1, stat_test2)
n <- length(stat_test1)
t <- (r * sqrt(n - 2)) / sqrt(1 - r^2)
t
4.80570675334372

棄却域は,

In [21]:
lower <- qt(0.05 / 2, df = n-2)
upper <- qt(0.05 / 2, df = n - 2, lower.tail = FALSE)
cat("t < ", lower, ", t > ", upper)
t <  -2.100922 , t >  2.100922
In [22]:
ggplot(data.frame(x = c(-3, 3)), aes(x)) +
    stat_function(fun = dt, args = list(df = n - 2)) + 
    scale_x_continuous(breaks = c(-3:3, 1)) + 
    geom_vline(xintercept = c(qt(0.025, df = n - 2), qt(0.975, df = n - 2)))

帰無仮説は棄却された

$p$ 値は,

In [23]:
2 * pt(-t, df = n - 2)
0.000141622881554482

cor.test() で計算

In [24]:
cor.test(stat_test1, stat_test2)
	Pearson's product-moment correlation

data:  stat_test1 and stat_test2
t = 4.8057, df = 18, p-value = 0.0001416
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 0.4596086 0.8952048
sample estimates:
     cor 
0.749659 

5.6 カイ二乗検定

In [25]:
math <- d$math
stat <- d$stat
table(math, stat)
      stat
math   嫌い 好き
  嫌い   10    4
  好き    2    4

検定統計量

$$\chi^2 = \sum_{i = 1}^{k} \frac{(O_i - E_i)^2}{E_i}$$

カイ二乗分布

In [26]:
ggplot(data.frame(x = c(0, 20)), aes(x)) + 
    stat_function(fun = dchisq, args = list(df = 1)) + 
    stat_function(fun = dchisq, args = list(df = 2), col = "green") + 
    stat_function(fun = dchisq, args = list(df = 4), col = "red") + 
    stat_function(fun = dchisq, args = list(df = 8), col = "blue")

観測度数

In [27]:
library(pipeR)
In [28]:
m.o <- table(math, stat)
m.o
      stat
math   嫌い 好き
  嫌い   10    4
  好き    2    4
In [29]:
m.o %>>% rowSums
嫌い
14
好き
6
In [30]:
m.o %>>% colSums
嫌い
12
好き
8
In [31]:
m.o %>>% sum
20

期待度数

In [32]:
m.o.c <- colSums(m.o)
m.e <- rbind(m.o.c, m.o.c) * rowSums(m.o) / sum(m.o)
m.e
嫌い好き
m.o.c8.45.6
m.o.c3.62.4
In [33]:
chi2 <- sum((m.o - m.e)^2 / m.e)
chi2
2.53968253968254

棄却域は,

In [34]:
n <- 2
qchisq(0.05, df = (n - 1) * (n - 1), lower.tail = FALSE)
3.84145882069413
In [35]:
ggplot(data.frame(x = c(0, 6)), aes(x)) +
    stat_function(fun = dchisq, args = list(df = (n - 1) * (n - 1))) + 
    scale_x_continuous(breaks = c(0:6, 1)) + 
    geom_vline(xintercept = c(qchisq(0.95, df = (n - 1) * (n - 1))))

帰無仮説は棄却されなかった.

$p$ 値を計算すると,

In [36]:
pchisq(chi2, df = (n - 1) * (n - 1), lower.tail = FALSE)
0.111017105512186

chisq.test() で計算すると,

In [37]:
chisq.test(m.o, correct = FALSE)
Warning message:
In chisq.test(m.o, correct = FALSE): Chi-squared approximation may be incorrect
	Pearson's Chi-squared test

data:  m.o
X-squared = 2.5397, df = 1, p-value = 0.111

5.7 サンプルサイズ

サンプルサイズが大きくなると,検定結果が有意になりやすい

In [38]:
reg_a <- matrix(c(16, 12, 4, 8), 2, 2)
rownames(reg_a) <- c("文系", "理系")
colnames(reg_a) <- c("履修した", "履修してない")
reg_a
履修した履修してない
文系16 4
理系12 8
In [39]:
chisq.test(reg_a, correct = FALSE)
	Pearson's Chi-squared test

data:  reg_a
X-squared = 1.9048, df = 1, p-value = 0.1675
In [40]:
reg_b <- reg_a * 10
reg_b
履修した履修してない
文系160 40
理系120 80
In [41]:
chisq.test(reg_b, correct = FALSE)
	Pearson's Chi-squared test

data:  reg_b
X-squared = 19.048, df = 1, p-value = 1.275e-05

練習問題

(1)

In [42]:
height <- c(165,150,170,168,159,170,167,178,155,159,161,162,166,171,155,160,168,172,155,167)
t.test(height, mu = 170)
	One Sample t-test

data:  height
t = -3.8503, df = 19, p-value = 0.001079
alternative hypothesis: true mean is not equal to 170
95 percent confidence interval:
 160.584 167.216
sample estimates:
mean of x 
    163.9 

帰無仮説が棄却されたので,無作為標本とは言えない

(2)

In [43]:
d.study <- data.frame(time = c(1, 3, 10, 12, 6, 3, 8, 4, 1, 5), score = c(20, 40, 100, 80, 50, 50, 70, 50, 10, 60))
d.study
timescore
1120
2340
310100
41280
5650
6350
7870
8450
9110
10560
In [44]:
cor.test(d.study$time, d.study$score)
	Pearson's product-moment correlation

data:  d.study$time and d.study$score
t = 6.1802, df = 8, p-value = 0.0002651
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 0.6542283 0.9786369
sample estimates:
      cor 
0.9092974 

帰無仮説が棄却されたので,無相関ではない

(3)

スピアマンの順位相関係数

In [45]:
cor(d.study$time, d.study$score, method = "spearman")
0.938289481375165
In [46]:
cor.test(d.study$time, d.study$score, method = "spearman")
Warning message:
In cor.test.default(d.study$time, d.study$score, method = "spearman"): Cannot compute exact p-value with ties
	Spearman's rank correlation rho

data:  d.study$time and d.study$score
S = 10.182, p-value = 5.887e-05
alternative hypothesis: true rho is not equal to 0
sample estimates:
      rho 
0.9382895 

ケンドールの順位相関係数

In [47]:
cor(d.study$time, d.study$score, method = "kendall")
0.847117449603021
In [48]:
cor.test(d.study$time, d.study$score, method = "kendall")
Warning message:
In cor.test.default(d.study$time, d.study$score, method = "kendall"): Cannot compute exact p-value with ties
	Kendall's rank correlation tau

data:  d.study$time and d.study$score
z = 3.2937, p-value = 0.0009889
alternative hypothesis: true tau is not equal to 0
sample estimates:
      tau 
0.8471174 

(4)

In [49]:
library(readxl)
In [50]:
d.food <- read_excel("Chap03_food.xlsx", sheet = 1)
head(d.food)
WesternOrJapaneseSweetOrHot
1洋食甘党
2和食辛党
3和食甘党
4洋食甘党
5和食辛党
6洋食辛党
In [51]:
m.o <- table(d.food)
m.o
                 SweetOrHot
WesternOrJapanese 甘党 辛党
             洋食    6    4
             和食    3    7
In [52]:
chisq.test(m.o, correct = FALSE)
Warning message:
In chisq.test(m.o, correct = FALSE): Chi-squared approximation may be incorrect
	Pearson's Chi-squared test

data:  m.o
X-squared = 1.8182, df = 1, p-value = 0.1775

帰無仮説を棄却できない

(5)

(5-1)

In [53]:
jap <- c(60, 40, 30, 70, 55)
soc <- c(80, 25, 35, 70, 50)
In [54]:
cor.test(jap, soc)
	Pearson's product-moment correlation

data:  jap and soc
t = 2.6952, df = 3, p-value = 0.07408
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 -0.1590624  0.9892731
sample estimates:
     cor 
0.841263 

(5-2)

In [55]:
jap <- rep(jap, 2)
soc <- rep(soc, 2)
In [56]:
cor.test(jap, soc)
	Pearson's product-moment correlation

data:  jap and soc
t = 4.4013, df = 8, p-value = 0.002283
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 0.4499858 0.9615658
sample estimates:
     cor 
0.841263 

サンプルサイズが2倍になると,相関係数は変わらないが,$p$値が小さくなった

In [57]:
devtools::session_info()
Session info -------------------------------------------------------------------
Packages -----------------------------------------------------------------------
Session info -------------------------------------------------------------------
Packages -----------------------------------------------------------------------
 setting  value                       
 version  R version 3.2.3 (2015-12-10)
 system   x86_64, mingw32             
 ui       RTerm                       
 language en                          
 collate  Japanese_Japan.932          
 tz       Asia/Tokyo                  
 date     2016-05-18                  

 package    * version date       source                            
 base64enc    0.1-3   2015-07-28 CRAN (R 3.2.2)                    
 colorspace   1.2-6   2015-03-11 CRAN (R 3.2.1)                    
 devtools     1.10.0  2016-01-23 CRAN (R 3.2.3)                    
 digest       0.6.9   2016-01-08 CRAN (R 3.2.3)                    
 evaluate     0.8     2015-09-18 CRAN (R 3.2.2)                    
 ggplot2    * 2.0.0   2015-12-18 CRAN (R 3.2.3)                    
 gtable       0.1.2   2012-12-05 CRAN (R 3.2.1)                    
 IRdisplay    0.3     2015-04-27 local                             
 IRkernel     0.6     2016-02-08 Github (IRkernel/IRkernel@40dc791)
 jsonlite     0.9.19  2015-11-28 CRAN (R 3.2.2)                    
 labeling     0.3     2014-08-23 CRAN (R 3.2.1)                    
 magrittr     1.5     2014-11-22 CRAN (R 3.2.1)                    
 memoise      1.0.0   2016-01-29 CRAN (R 3.2.3)                    
 munsell      0.4.2   2013-07-11 CRAN (R 3.2.1)                    
 pbdZMQ       0.2-1   2016-01-21 CRAN (R 3.2.3)                    
 pipeR      * 0.6.0.6 2015-07-08 CRAN (R 3.2.1)                    
 plyr         1.8.3   2015-06-12 CRAN (R 3.2.1)                    
 R6           2.1.2   2016-01-26 CRAN (R 3.2.3)                    
 Rcpp         0.12.3  2016-01-10 CRAN (R 3.2.3)                    
 readr      * 0.2.2   2015-10-22 CRAN (R 3.2.2)                    
 readxl     * 0.1.0   2015-04-14 CRAN (R 3.2.1)                    
 repr         0.4     2015-11-16 local                             
 scales       0.3.0   2015-08-25 CRAN (R 3.2.2)                    
 stringi      1.0-1   2015-10-22 CRAN (R 3.2.3)                    
 stringr      1.0.0   2015-04-30 CRAN (R 3.2.3)                    
 uuid         0.1-2   2015-07-28 CRAN (R 3.2.2)