Ch.2 「データの分析」の章末問題の解答例 〜基礎統計学Ⅱ 人文・社会科学の統計学〜

当記事は「基礎統計学Ⅱ 人文・社会科学の統計学(東京大学出版会)」の読解サポートにあたってChapter.$2$の「データの分析」の章末問題の解説について行います。
基本的には書籍の購入者向けの解説なので、まだ入手されていない方は下記より入手をご検討ください。また、解説はあくまでサイト運営者が独自に作成したものであり、書籍の公式ページではないことにご注意ください。

・解答まとめ
https://www.hello-statisticians.com/answer_textbook_stat_basic_1-3#green

章末の演習問題について

問題2.1の解答例

下記を実行することで、幹葉表示を作成できる。

import numpy as np

x = np.array([5.9, 6.7, 7.1, 5.9, 7.5, 7.9, 7.4, 6.5, 6.7, 6.7, 4.5, 4.9, 5.2, 4.5, 7.3, 6.6, 7.2, 7.8, 7.3, 6.4, 6.1, 5.2, 7.2, 6.5, 6.4, 5.5, 6.4, 6.3, 8.1, 8.2, 7.5, 6.7, 7.8, 8.3, 7.5, 7.6, 8.9, 6.4, 7.9, 7.4, 7.5, 7.9, 7.4, 8.6, 4.9])

stem = np.unique(x//1)
leaf = x%1

leaves = dict()
for i in range(stem.shape[0]):
    leaves[int(stem[i])] = list()

for i in range(x.shape[0]):
    leaves[int(x[i]//1)].append(int(np.floor((x[i]%1)*10)))

for i in range(stem.shape[0]):
    leaves[int(stem[i])] = np.sort(leaves[int(stem[i])])
    l = ""
    for j in range(len(leaves[int(stem[i])])):
        l += str(leaves[int(stem[i])][j])
    print("stem: {}, leaves: {}".format(int(stem[i]),l))

・実行結果

stem: 4, leaves: 5599
stem: 5, leaves: 22599
stem: 6, leaves: 0244445557777
stem: 7, leaves: 022224445555577999
stem: 8, leaves: 01359

問題2.2の解答例

下記を実行することで、ジニ係数を計算することができる。

import numpy as np

y_1 = np.array([0., 0.07, 0.21, 0.39, 0.61, 1.00])
y_2 = np.array([0., 0.11, 0.26, 0.45, 0.68, 1.00])

gini_1 = 2*(0.5-np.sum((y_1[:-1]+y_1[1:])*0.2/(2.)))
gini_2 = 2*(0.5-np.sum((y_2[:-1]+y_2[1:])*0.2/(2.)))

print("gini_1: {:.3f}".format(gini_1))
print("gini_2: {:.3f}".format(gini_2))

・実行結果

> print("gini_1: {:.3f}".format(gini_1))
gini_1: 0.288
> print("gini_2: {:.3f}".format(gini_2))
gini_2: 0.200

問題2.3の解答例

問題2.4の解答例

下記を実行することで$t$統計量や相関係数$r_{xD}$を計算することができる。

import numpy as np

x = np.array([176., 173., 175., 170., 173., 170., 165., 164., 165., 160.])
D = np.array([1., 1., 1., 1., 1., 0., 0., 0., 0., 0.])

s = np.sqrt((np.sum((x[:5]-np.mean(x[:5]))**2)+np.sum((x[5:]-np.mean(x[5:]))**2))/(10.-2.))
t = np.sqrt(5.*5./10.) * (np.mean(x[:5])-np.mean(x[5:])) / s

r = t/np.sqrt(t**2+10.-2.)

print("t-statistic: {:.3f}".format(t))
print("r_xD: {:.4f}".format(r))

・実行結果

t-statistic: 4.533
r_xD: 0.8484

問題2.5の解答例

下記を実行することでそれぞれの値の計算を行うことができる。

import numpy as np

a, b, c, d = 2415., 274., 2002., 620.

phi_coef = (a*d-b*c)/np.sqrt((a+b)*(a+c)*(b+d)*(c+d))
omega = (a*d)/(b*c)
Q = (omega-1)/(omega+1)

print("Phi coefficient: {:.2f}".format(phi_coef))
print("Sample odds ratio: {:.2f}".format(omega))
print("Yule's coefficient: {:.2f}".format(Q))

・実行結果

> print("Phi coefficient: {:.2f}".format(phi_coef))
Phi coefficient: 0.18
> print("Sample odds ratio: {:.2f}".format(omega))
Sample odds ratio: 2.73
> print("Yule's coefficient: {:.2f}".format(Q))
Yule's coefficient: 0.46

$\phi$係数の値が巻末の解答と異なるが、適合度検定に用いる$\chi^2$の値が$171.7…$であることよりも上記が正しいことが確認できるので、巻末の解答は誤植であると考えられる。

問題2.6の解答例

まとめ

「Ch.2 「データの分析」の章末問題の解答例 〜基礎統計学Ⅱ 人文・社会科学の統計学〜」への2件のフィードバック

コメントは受け付けていません。