選對統計檢定

Python 的實驗設計

James Chapman

Curriculum Manager, DataCamp

如何選擇合適的檢定

 

  • 資料集特徵
    • 資料型別
    • 分配 → 許多檢定假設為常態!
    • 變數數量
  • 假設

結果:結論更準確、可靠!

  • t 檢定、ANOVA、卡方檢定

工具與書籍的圖書館

Python 的實驗設計

資料集:運動表現

  • 訓練課程與飲食對運動表現的影響
athletic_perf.sample(n=5)
 Athlete_ID  Training_Program     Diet_Type  Initial_Fitness  Performance_Inc 
        167         Endurance   Plant-Based             High         9.113040               
        289         Endurance          Keto              Low        11.039744               
        164         Endurance   Plant-Based           Medium        11.614835              
         30          Strength          Keto           Medium         7.384686               
        186              HIIT  High-Protein              Low         6.776078
Python 的實驗設計

獨立樣本 t 檢定

  • 比較「兩組」的平均數
  • 假設:常態分配、變異數相等
from scipy.stats import ttest_ind
group1 = athletic_perf[athletic_perf['Training_Program'] == 'HIIT']['Performance_Inc']
group2 = athletic_perf[athletic_perf['Training_Program'] == 'Endurance']['Performance_Inc']

t_stat, p_val = ttest_ind(group1, group2) print(f"T-statistic: {t_stat}, P-value: {p_val}")
T-statistic: 0.20671020082911742, P-value: 0.8364563849070663

p_val > $\alpha$ → 證據不足,無平均數差異

Python 的實驗設計

單因子 ANOVA

  • 比較「多組(>2)」的平均數
  • 假設:各組變異數相等
from scipy.stats import f_oneway
program_types = ['HIIT', 'Endurance', 'Strength']
groups = [athletic_perf_data[athletic_perf_data['Training_Program'] == program]
['Performance_Increase'] for program in program_types]

f_stat, p_val = f_oneway(*groups) print(f"F-statistic: {f_stat}, P-value: {p_val}")
F-statistic: 1.5270022393256704, P-value: 0.2188859009050602

p_val > $\alpha$ → 證據不足,無平均數差異

Python 的實驗設計

卡方關聯檢定

  • 檢驗「類別變數」之間的關聯
  • 不需分配假設
from scipy.stats import chi2_contingency
import pandas as pd
contingency_table = pd.crosstab(athletic_perf['Training_Program'],
                                athletic_perf['Diet_Type'])
Diet_Type         High-Protein  Keto  Plant-Based
Training_Program                                 
Endurance                   33    28           33
HIIT                        27    32           40
Strength                    38    29           40
Python 的實驗設計

卡方關聯檢定

chi2_stat, p_val, dof, expected = chi2_contingency(contingency_table)
print(f"Chi2-statistic: {chi2_stat}, P-value: {p_val}")
Chi2-statistic: 2.154450885821988, P-value: 0.7073764021451127

p_val > $\alpha$ → 證據不足,無關聯

Python 的實驗設計

一起來練習吧!

Python 的實驗設計

Preparing Video For Download...