分散分析(ANOVA)

Pythonで学ぶ仮説検定

James Chapman

Curriculum Manager, DataCamp

仕事満足度:5区分

stack_overflow['job_sat'].value_counts()
Very satisfied           879
Slightly satisfied       680
Slightly dissatisfied    342
Neither                  201
Very dissatisfied        159
Name: job_sat, dtype: int64
Pythonで学ぶ仮説検定

複数分布の可視化

仕事満足度の水準で、年間報酬の平均は異なりますか?

import seaborn as sns
import matplotlib.pyplot as plt
sns.boxplot(x="converted_comp", 
            y="job_sat", 
            data=stack_overflow)
plt.show()

5区分ごとの報酬分布の箱ひげ図。「Very satisfied」は他よりやや高そうだが、判定は難しい。

Pythonで学ぶ仮説検定

分散分析(ANOVA)

  • グループ間の差を検定する手法
alpha = 0.2


pingouin.anova(data=stack_overflow, dv="converted_comp", between="job_sat")
    Source  ddof1  ddof2         F     p-unc       np2
0  job_sat      4   2256  4.480485  0.001315  0.007882
  • 0.001315 は $\alpha$ 未満
  • 少なくとも2区分で報酬に有意差あり
Pythonで学ぶ仮説検定

ペアごとの検定

  • $\mu_{\text{very dissatisfied}} \neq \mu_{\text{slightly dissatisfied}}$
  • $\mu_{\text{very dissatisfied}} \neq \mu_{\text{neither}}$
  • $\mu_{\text{very dissatisfied}} \neq \mu_{\text{slightly satisfied}}$
  • $\mu_{\text{very dissatisfied}} \neq \mu_{\text{very satisfied}}$
  • $\mu_{\text{slightly dissatisfied}} \neq \mu_{\text{neither}}$
  • $\mu_{\text{slightly dissatisfied}} \neq \mu_{\text{slightly satisfied}}$
  • $\mu_{\text{slightly dissatisfied}} \neq \mu_{\text{very satisfied}}$
  • $\mu_{\text{neither}} \neq \mu_{\text{slightly satisfied}}$
  • $\mu_{\text{neither}} \neq \mu_{\text{very satisfied}}$
  • $\mu_{\text{slightly satisfied}} \neq \mu_{\text{very satisfied}}$

 

有意水準を $\alpha = 0.2$ とする。

Pythonで学ぶ仮説検定

pairwise_tests()

pingouin.pairwise_tests(data=stack_overflow, 
                        dv="converted_comp", 
                        between="job_sat", 
                        padjust="none")
  Contrast                   A                      B  Paired  Parametric  ...          dof  alternative     p-unc     BF10    hedges
0  job_sat  Slightly satisfied         Very satisfied   False        True  ...  1478.622799    two-sided  0.000064  158.564 -0.192931
1  job_sat  Slightly satisfied                Neither   False        True  ...   258.204546    two-sided  0.484088    0.114 -0.068513
2  job_sat  Slightly satisfied      Very dissatisfied   False        True  ...   187.153329    two-sided  0.215179    0.208 -0.145624
3  job_sat  Slightly satisfied  Slightly dissatisfied   False        True  ...   569.926329    two-sided  0.969491    0.074 -0.002719
4  job_sat      Very satisfied                Neither   False        True  ...   328.326639    two-sided  0.097286    0.337  0.120115
5  job_sat      Very satisfied      Very dissatisfied   False        True  ...   221.666205    two-sided  0.455627    0.126  0.063479
6  job_sat      Very satisfied  Slightly dissatisfied   False        True  ...   821.303063    two-sided  0.002166     7.43  0.173247
7  job_sat             Neither      Very dissatisfied   False        True  ...   321.165726    two-sided  0.585481    0.135 -0.058537
8  job_sat             Neither  Slightly dissatisfied   False        True  ...   367.730081    two-sided  0.547406    0.118  0.055707
9  job_sat   Very dissatisfied  Slightly dissatisfied   False        True  ...   247.570187    two-sided  0.259590    0.197  0.119131

[10 rows x 11 columns]
Pythonで学ぶ仮説検定

グループ数が増えると…

グループ数に対するペア数の散布図。グループ数が増えると、ペア数は二次的に増加する。

少なくとも1つ有意となる確率とグループ数の散布図。グループ数が増えると、少なくとも1つ有意となる確率が上がる。

Pythonで学ぶ仮説検定

ボンフェローニ補正

pingouin.pairwise_tests(data=stack_overflow, 
                        dv="converted_comp", 
                        between="job_sat", 
                        padjust="bonf")
  Contrast                   A                      B   ...     p-unc    p-corr p-adjust     BF10    hedges
0  job_sat  Slightly satisfied         Very satisfied   ...  0.000064  0.000638     bonf  158.564 -0.192931
1  job_sat  Slightly satisfied                Neither   ...  0.484088  1.000000     bonf    0.114 -0.068513
2  job_sat  Slightly satisfied      Very dissatisfied   ...  0.215179  1.000000     bonf    0.208 -0.145624
3  job_sat  Slightly satisfied  Slightly dissatisfied   ...  0.969491  1.000000     bonf    0.074 -0.002719
4  job_sat      Very satisfied                Neither   ...  0.097286  0.972864     bonf    0.337  0.120115
5  job_sat      Very satisfied      Very dissatisfied   ...  0.455627  1.000000     bonf    0.126  0.063479
6  job_sat      Very satisfied  Slightly dissatisfied   ...  0.002166  0.021659     bonf     7.43  0.173247
7  job_sat             Neither      Very dissatisfied   ...  0.585481  1.000000     bonf    0.135 -0.058537
8  job_sat             Neither  Slightly dissatisfied   ...  0.547406  1.000000     bonf    0.118  0.055707
9  job_sat   Very dissatisfied  Slightly dissatisfied   ...  0.259590  1.000000     bonf    0.197  0.119131

[10 rows x 11 columns]
Pythonで学ぶ仮説検定

ほかの方法

padjust : string

p値の検定・補正方法。

  • 'none': 補正なし[既定]
  • 'bonf': 1段階のボンフェローニ補正
  • 'sidak': 1段階のシダック補正
  • 'holm': 段階的(ステップダウン)ボンフェローニ
  • 'fdr_bh': ベンジャミニ–ホッホベルクのFDR補正
  • 'fdr_by': ベンジャミニ–イェクチエリのFDR補正
Pythonで学ぶ仮説検定

¡Vamos a practicar!

Pythonで学ぶ仮説検定

Preparing Video For Download...