选择合适的统计检验

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 实验设计

Passons à la pratique !

Python 实验设计

Preparing Video For Download...