雙樣本 t 檢定

使用 Python 分析問卷資料

EbunOluwa Andrew

Data Scientist

比較「親和性」

握手的人

group_a.agreeableness.mean()
4.011701199563795
group_b.agreeableness.mean()
4.03669574700109
使用 Python 分析問卷資料

定義雙樣本 t 檢定

  • 檢驗兩個獨立組別的平均數是否有顯著差異
  • 判斷差異是否僅由隨機造成

標有 A 與 B 的兩個瓶子

使用 Python 分析問卷資料

雙樣本 t 檢定的假設

  • 獨立
  • 常態分布
    • Shapiro-Wilk 檢定
    • stats.shapiro()
    • p-value > 0.05 -> 視為常態
  • 變異數相等
    • Levene 檢定
    • stats.levene()
    • p-value > 0.05 -> 視為變異數相等

木製數字積木

使用 Python 分析問卷資料

問卷結果

group_a

| userid | agreeableness |
|--------|---------------|
|    895 |          4.78 |
|    a06 |          3.40 |
|    e94 |          3.66 |
|    ee6 |          5.41 |
|    521 |          4.58 |
|    f4c |          3.24 |
...

1 = 不親和

group_b

| userid | agreeableness |
|--------|---------------|
|    b7e | 4.43          |
|    030 | 2.92          |
|    f91 | 4.01          |
|    36f | 2.20          |
|    875 | 3.83          |
|    750 | 4.95          |
...

7 = 親和

使用 Python 分析問卷資料

獨立組別

兩個群組

使用 Python 分析問卷資料

常態分布的組別

from scipy.stats import shapiro
import scipy.stats as stats

norm_A = stats.shapiro(
  group_a.agreeableness)

ShapiroResult(
statistic=0.997467577457428,
pvalue=0.16834689676761627)
from scipy.stats import shapiro
import scipy.stats as stats

norm_B = stats.shapiro(
  group_b.agreeableness)

ShapiroResult(
statistic=0.9987381100654602,
pvalue=0.7757995128631592)
使用 Python 分析問卷資料

變異數相等

import scipy.stats as stats

var_test = stats.levene(group_a.agreeableness, group_b.agreeableness)
LeveneResult(statistic=0.40492634057696597, pvalue=0.5246354858484796)
使用 Python 分析問卷資料

檢查完畢的假設

  • 獨立組別
    • 個體不重疊
  • 常態分布的組別
  • 變異數相等
    • 兩組變異數無顯著差異

Apple Pencil-Photo by Dose Media on Unsplash

使用 Python 分析問卷資料

使用 statsmodels 做雙樣本 t 檢定

from scipy import stats

stats.ttest_ind(group_a.agreeableness, group_b.agreeableness)
使用 Python 分析問卷資料

使用 statsmodels 做雙樣本 t 檢定

Ttest_indResult(statistic=0.7746406648066304, pvalue=0.4386519848366188)
使用 Python 分析問卷資料

進一步分析

group_a_mean = 4.011701199563795
group_b_mean = 4.03669574700109

加州房屋

使用 Python 分析問卷資料

一起來練習吧!

使用 Python 分析問卷資料

Preparing Video For Download...