あなたは「ピーマンが好きか」というアンケートを子どもたちにとりました。
また、それとは独立に、「なすが好きか」というアンケートも子どもたちにとりました。
どちらのアンケートも 5 つの選択肢から 1 つ選択する方式で、結果は以下のようになりました (2つのアンケート結果に対応はないものとします)。
ピーマン | なす | |
---|---|---|
好き | 6票 | 8票 |
どちらかといえば好き | 1票 | 7票 |
どちらでもない | 5票 | 2票 |
どちらかといえば嫌い | 6票 | 1票 |
嫌い | 2票 | 2票 |
ただ、この表からでは結局子どもたちのピーマン嗜好性となす嗜好性に差があるのかよくわかりませんでした。
カイ2乗検定
そこであなたはさしあたり、「2つの野菜間で『好き率』『どちらかといえば好き率』…『嫌い率』に差はない」という母比率の差のカイ2乗検定をしてみました。結果は有意水準5%で棄却されましたが、期待度数が小さいセルがあるのでフィッシャーの正確確率検定のほうがよかったことに気付きました。ただそもそも、この検定では今回の選択肢間に順序があることを考慮していないことに気付きました。
import pandas as pd
import scipy.stats
df = pd.DataFrame({
'GreenPepper': [6, 1, 5, 6, 2],
'Eggplant': [8, 7, 2, 1, 2],
}, index=[
'Like', 'SomewhatLike', 'Neutral', 'SomewhatDislike', 'Dislike'])
print('----- Chi-squared test -----')
chi2, p, dof, expected = scipy.stats.chi2_contingency(df.T)
print(f'{dof=}')
print(f'{expected=}')
print(f'{chi2=}')
print(f'{p=}')
----- Chi-squared test -----
dof=4
expected=array([[7. , 4. , 3.5, 3.5, 2. ],
[7. , 4. , 3.5, 3.5, 2. ]])
chi2=9.642857142857142
p=0.046893186991072186
マン・ホイットニーのU検定
そこであなたは、選択肢間の順序を考慮するために、選択肢に点数を割り当て、ピーマンのスコアたちとなすのスコアたちにマン・ホイットニーのU検定をすることにしました。
つまり、ピーマンとなすのアンケートに回答した計 40 人の子どもたちに、つけた点数が小さい順に1列に並んでもらい、なす組の子どもたちに前にピーマン組が何人いるか答えてもらい、ピーマン組の方が前に偏っていないかを調べました。
結果は有意水準5%では棄却されませんでした。
import pandas as pd
import scipy.stats
df = pd.DataFrame({
'GreenPepper': [6, 1, 5, 6, 2],
'Eggplant': [8, 7, 2, 1, 2],
}, index=[
'Like', 'SomewhatLike', 'Neutral', 'SomewhatDislike', 'Dislike'])
print('----- Mann-Whitney U test -----')
df['score'] = [2, 1, 0, -1, -2] # 選択肢に点数 (スコア) を割り当て
def to_scores(df, colname):
scores = []
for index, row in df.iterrows():
scores += [row['score']] * row[colname]
return scores
scores_gp = to_scores(df, 'GreenPepper')
scores_ep = to_scores(df, 'Eggplant')
print(f'{scores_gp=}')
print(f'{scores_ep=}')
stat, p = scipy.stats.mannwhitneyu(scores_gp, scores_ep)
print(f'{stat=}')
print(f'{p=}')
----- Mann-Whitney U test -----
scores_gp=[2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -2, -2]
scores_ep=[2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, -1, -2, -2]
stat=141.5
p=0.10524670367291183