析因设计:原则与应用

Python 实验设计

James Chapman

Curriculum Manager, DataCamp

理解析因设计

 

  • 在一次实验中研究多个自变量/因素
  • 测试因素水平的所有组合
  • 发现主效应与因素间的_交互_

一个展示光照条件和肥料类型如何影响植物生长的实验。

1 Image Generated with DALL·E 3
Python 实验设计

析因设计数据示例

  • 因素1(Light_Condition)— 两个水平:Full SunlightPartial Shade
  • 因素2(Fertilizer_Type)— 两个水平:SyntheticOrganic
  • 数值型响应/因变量/结果变量:Growth_cm
plant_growth_data.head()
   Plant_ID  Light_Condition  Fertilizer_Type  Growth_cm
0         1    Full Sunlight        Synthetic  16.489735
1         2    Partial Shade          Organic  18.361689
2         3    Full Sunlight        Synthetic  18.039459
3         4    Full Sunlight          Organic  12.682425
4         5    Full Sunlight          Organic  21.480601
Python 实验设计

组织数据以可视化交互作用

plant_growth = pd.pivot_table(plant_growth_data, 
                              values='Growth_cm',
                              index='Light_Condition',
                              columns='Fertilizer_Type', 
                              aggfunc='mean')

plant_growth
Light_Condition     Organic   Synthetic
Full Sunlight       20.602       19.869
Partial Shade       20.246       20.326
Python 实验设计

用热力图可视化交互作用

import seaborn as sns
import matplotlib.pyplot as plt
sns.heatmap(plant_growth, 
            annot=True, 
            cmap='coolwarm',
            fmt='g')
plt.show()

植物生长热力图

Python 实验设计

解读交互作用

Light_Condition     Organic   Synthetic
Full Sunlight       20.602       19.869
Partial Shade       20.246       20.326
  • 交互作用:一个因素的效应随另一因素水平而变
  • 交互显著 → 因素并非独立起作用
Python 实验设计

析因设计 vs. 随机区组设计

  • 多个处理及其交互
  • 拆解复杂多变量效应与交互
  • 可能需要更多受试者

一个展示光照条件和肥料类型如何影响植物生长的实验。

  • 在随机化设计中将相似受试者分组
  • 控制组内方差
  • 每个区组内测试所有处理

两个区组,包含随机分配颜色的立方体。

1 Images Generated with DALL·E 3
Python 实验设计

让我们来练习!

Python 实验设计

Preparing Video For Download...