# 第1章
- 本章ですこし触れられてる連関の尺度のいくつかは`DescTools` パッケージの`Assocs` で求めることができる.
- 「シリーズ編者による内容紹介」の精神的健康と親の社会経済的地位(SES)に関するミッドタウン・マンハッタンデータ(the Midtown Manhattan data)を用いて分析する.
```{r}
# 元データの入力(xページ)
Freq <- c ( 64 , 94 , 58 , 46 ,
57 , 94 , 54 , 40 ,
57 , 105 , 65 , 60 ,
72 , 141 , 77 , 94 ,
36 , 97 , 54 , 78 ,
21 , 71 , 54 , 71 )
# データを表形式に変換
tab <- matrix (
Freq,
nrow = 6 ,
ncol = 4 ,
byrow = TRUE ,
dimnames = list (
SES = LETTERS[1 : 6 ],
MHS = c ("well" ,
"mild" ,
"moderate" ,
"impaired" )
)
)
tab
```
`DescTools` パッケージを用いる.
```{r}
library (DescTools)
DescTools:: Desc (tab)
DescTools:: Assocs (tab)
```
次のように個別に尺度を求めることもできる.
詳しくは`?DescTools` の`Statistics:` を確認してほしい.
```{r}
DescTools:: CramerV (tab, conf.level = 0.95 )
DescTools:: Lambda (tab, conf.level = 0.95 )
DescTools:: GoodmanKruskalTau (tab, conf.level = 0.95 )
DescTools:: KendallTauB (tab, conf.level = 0.95 )
DescTools:: StuartTauC (tab, conf.level = 0.95 )
DescTools:: GoodmanKruskalGamma (tab, conf.level = 0.95 )
DescTools:: SomersDelta (tab, conf.level = 0.95 )
DescTools:: UncertCoef (tab, conf.level = 0.95 )
```
## クラメールのV
クラメールの$V$も連関の強さをみる指標であり,次のように定義される.
\begin{equation}
V = \sqrt{\frac{\chi^2/n}{{\rm min}(I-1,J-1)}}
\end{equation}
${\rm min}(I-1,J-1)$は括弧内の最小の値であり,${\rm min}(I,J)-1$と考えても良い.
値の取りうる範囲は$$0 \leq V \leq 1$$となり,値が大きいほど2つの変数の連関は強いといえる.
ではクラメールのVを自分で求めてみよう.`chisq.test(tab)$statistic` でカイ2乗値を取り出すことができるが,
名前がついているので`unname` 関数で取り除く.`as.numeric` としても取り除くことができる.
```{r}
tab
X2 <- chisq.test (tab)$ statistic |> unname () # あるいはas.numeric()
V <- sqrt (X2/ sum (tab)/ (min (dim (tab)) - 1 ))
V
```
`tab` の2行目を10倍,1列目を10倍したデータを考える.これを`tab10` とする.
```{r}
tab10 <- tab
tab10[,1 ] <- tab10[,1 ]* 10
tab10[2 ,] <- tab10[2 ,]* 10
tab
tab10
```
この2つの表について先程の連関係数の値を確認すると,すべての係数について表によって値が異なる.
```{r}
DescTools:: CramerV (tab)
DescTools:: CramerV (tab10)
DescTools:: GoodmanKruskalTau (tab)
DescTools:: GoodmanKruskalTau (tab10)
DescTools:: KendallTauB (tab)
DescTools:: KendallTauB (tab10)
DescTools:: StuartTauC (tab)
DescTools:: StuartTauC (tab10)
DescTools:: GoodmanKruskalGamma (tab)
DescTools:: GoodmanKruskalGamma (tab10)
DescTools:: SomersDelta (tab)
DescTools:: SomersDelta (tab10)
DescTools:: UncertCoef (tab)
DescTools:: UncertCoef (tab10)
```
オッズ比に基づく尺度では表によって値は異ならない.
@bouchet-valat2022 や @zhou2015 を参照.
```{r}
# 2つの表をマージしたデータも作成
tab_merge <- dplyr:: bind_rows (data.frame (as.table (tab)),
data.frame (as.table (tab10)),
.id = "Tab" ) |>
xtabs (Freq ~ SES + MHS + Tab, data = _)
tab_merge
# intrinsic association coefficient
logmult:: iac (tab, weighting = "none" )
logmult:: iac (tab10, weighting = "none" )
logmult:: iac (tab_merge, weighting = "none" )
# Altham index
logmult:: iac (tab, weighting = "none" ) * sqrt (nrow (tab) * ncol (tab)) * 2
logmult:: iac (tab10, weighting = "none" ) * sqrt (nrow (tab) * ncol (tab)) * 2
logmult:: iac (tab_merge, weighting = "none" ) * sqrt (nrow (tab) * ncol (tab)) * 2
# Shrinkage Estimation
logmult:: iac (tab_merge,
weighting = "none" ,
shrink= TRUE ) * sqrt (nrow (tab_merge) * ncol (tab_merge)) * 2 * 2
# Mean absolute odds ratio
logmult:: maor (tab, weighting = "uniform" )
logmult:: maor (tab10, weighting = "uniform" )
logmult:: maor (tab_merge, weighting = "uniform" )
```
ただし周辺重み付けをすれば値は異なる場合もある.
```{r}
# intrinsic association coefficient
logmult:: iac (tab, weighting = "marginal" )
logmult:: iac (tab10, weighting = "marginal" )
# Mean absolute odds ratio
logmult:: maor (tab, weighting = "marginal" )
logmult:: maor (tab10, weighting = "marginal" )
```
## 練習問題 {-}
`occupationalStatus` はRの組み込みデータセットで、イギリス男性の父親(origin)と息子(destination)の職業地位(8段階)をクロス表にしたものである。
- 出典: @goodman1979a Table 3.
```{r}
occupationalStatus
```
このデータを用いて、本章で紹介した連関の尺度(クラメールのV、Kendallの$\tau_b$、Goodman-Kruskalの$\gamma$、iac、maorなど)を計算し、結果を比較せよ。
## 参考文献 {-}