glm 関数と t 検定

t 検定は 2 つの実験群の間の平均に有意差があるかどうを検定する方法である。R では t.test 関数を利用することで検定を行うことができる。一方で、t 検定は一般化線形モデルの問題に帰着することができる。そのため、t 検定を一般化線形モデルの問題として捉えて、glm 関数で検定することもできる。このページでは、t 検定を一般化線形モデルの問題として解く例を示す。

等分散のときの t 検定

サンプルデータとして 2 つの実験群を用意し、1 群の平均を β1、2 群の平均を β2 とする。群の情報を(1, 0) または (0, 1) を利用して表す。また、実験データを従属変数として与える。数式で表すと次のようになる。

\[ \mathbf{y} = \mathbf{X}\mathbf{\beta} + \mathbf{e} \] \[ \begin{pmatrix} y_{11} \\ y_{12} \\ \vdots \\ y_{21} \\ y_{22} \\ \vdots \end{pmatrix} = \begin{pmatrix} 1 & 0 \\ 1 & 0 \\ \vdots & \vdots \\ 0 & 1 \\ 0 & 1 \\ \vdots & \vdots \end{pmatrix} \begin{pmatrix} \beta_{1} \\ \beta_{2} \end{pmatrix} \]

モデル式に合うようにサンプルデータを生成し、一般化線形モデルを構築する。

y1 <- rnorm(10, 7, 2)
y2 <- rnorm(10, 12, 2)
y <- c(y1, y2)

x1 <- factor(c(rep(1, 10), rep(0, 10)))
x2 <- factor(c(rep(0, 10), rep(1, 10)))

m <- glm(y ~ x1 + x2, family = gaussian())
summary(m)
## Call:
## glm(formula = y ~ x1 + x2, family = gaussian())
## Deviance Residuals:
##     Min       1Q   Median       3Q      Max
## -3.9771  -1.0643  -0.2124   1.2976   8.4149
## Coefficients: (1 not defined because of singularities)
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  12.7710     0.8977  14.227 3.11e-11 ***
## x11          -5.4394     1.2695  -4.285 0.000446 ***
## x21               NA         NA      NA       NA
## ---
## Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
## (Dispersion parameter for gaussian family taken to be 8.058125)
## 
##     Null deviance: 292.98  on 19  degrees of freedom
## Residual deviance: 145.05  on 18  degrees of freedom
## AIC: 102.38
## Number of Fisher Scoring iterations: 2

glm 関数の解析結果をみると、 x11 の項の t value は -4.285 であり、Pr は 0.000446 である。同様なデータを利用して、t 検定を行うと結果は以下のようになる。t 検定の結果も glm と同様に t は -4.2847、p-value は 0.000446 であることがわかる。

t.test(y1, y2, var.equal = T)
## 	Two Sample t-test
## 
## data:  y1 and y2
## t = -4.2847, df = 18, p-value = 0.000446
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -8.106492 -2.772260
## sample estimates:
## mean of x mean of y
##  7.331672 12.771048

分散が異なるときの t 検定

分散が異なるときの t 検定も一般化線形モデルで解析できる。2 つの分散が同じであるとき、一般化線形モデルには 2 つの群の平均をパラメーターとして組み込むことで十分であった。2 つの群の分散が異なるとき、一般化線形モデルには 2 つの平均に加え、2 つの分散もパラメーターとして組み込むことで対応できる。

\[ \begin{pmatrix} y_{11} \\ y_{12} \\ \vdots \\ y_{21} \\ y_{22} \\ \vdots \end{pmatrix} = \begin{pmatrix} 1 & 1 & 0 & 0 \\ 1 & 1& 0 & 0 \\ \vdots & \vdots & \vdots & \vdots \\ 0 & 0 & 1 & 1 \\ 0 & 0 & 1 &1 \\ \vdots & \vdots & \vdots & \vdots \end{pmatrix} \begin{pmatrix} \beta_{1\mu} \\ \beta_{1\sigma^{2}} \\ \beta_{2\mu} \\ \beta_{2\sigma^{2}} \end{pmatrix} \]
y1 <- rnorm(10, 7, 3)
y2 <- rnorm(10, 9, 5)
y <- c(y1, y2)

f1 <- factor(c(rep(1, 10), rep(0, 10)))
s1 <- factor(c(rep(1, 10), rep(0, 10)))
f2 <- factor(c(rep(0, 10), rep(1, 10)))
s2 <- factor(c(rep(0, 10), rep(1, 10)))

m <- glm(y ~ f1 + s1 + f2 + s2, family = gaussian())
summary(m)
## Call:
## glm(formula = y ~ f1 + s1 + f2 + s2, family = gaussian())
## Deviance Residuals:
##     Min       1Q   Median       3Q      Max
## -4.0298  -1.3452  -0.2259   0.6227   7.8657
## Coefficients: (3 not defined because of singularities)
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)   6.6821     0.8101   8.249 1.58e-07 ***
## f11          -1.2198     1.1456  -1.065    0.301
## s11               NA         NA      NA       NA
## f21               NA         NA      NA       NA
## s21               NA         NA      NA       NA
## ---
## Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
## (Dispersion parameter for gaussian family taken to be 6.562184)
##     Null deviance: 125.56  on 19  degrees of freedom
## Residual deviance: 118.12  on 18  degrees of freedom
## AIC: 98.277
## Number of Fisher Scoring iterations: 2


t.test(y1, y2, var.equal = F)
## 	Welch Two Sample t-test
## 
## data:  y1 and y2
## t = -1.0647, df = 14.082, p-value = 0.3049
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -3.675538  1.235970
## sample estimates:
## mean of x mean of y
##  5.462298  6.682082