因子設計:原理與應用

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...