常態資料

Python 的實驗設計

James Chapman

Curriculum Manager, DataCamp

常態分配

 

  • 熟悉的「鐘形曲線」
  • 與 z 分數相關

$$ {z} = \frac{x-\mu}{\sigma}$$

  • 平均數 = 0,標準差 = 1
    • 「此點離平均數有幾個標準差?」
    • 「得到此分數的機率是多少?」

 

典型鐘形曲線的圖,白色背景上有藍色曲線。

Python 的實驗設計

常態資料與統計檢定

 

  • 參數式檢定需要常態
  • 非參數式檢定:不假設常態資料

 

典型鐘形曲線的圖,白色背景上有藍色曲線。

Python 的實驗設計

常態、Z 與 α

 

  • 與顯著水準($\alpha$)的關聯很關鍵
  • 比較 p 值與 $\alpha$
  • 第 I 類錯誤的機率

 

常態分配兩端尾部各有一小塊區域以黑色標示

Python 的實驗設計

視覺化常態資料

 

sns.displot(data=salaries,
            x='salary',
            kind="kde")
plt.show()

 

一個較高且較窄的鐘形分配,但仍呈現典型鐘形曲線

Python 的實驗設計

QQ 圖

QQ 圖:將資料與特定分配比較

from statsmodels.graphics.gofplots import qqplot
from scipy.stats.distributions import norm
qqplot(salaries['salary'], 
       line='s', 
       dist=norm)
plt.show()
  • 理想:點緊貼直線
  • 不佳:兩端外張

 

QQ 圖中所有點大多緊貼中間 45 度直線

QQ 圖中間的點貼近 45 度線,但兩端的點向內彎成弧線

Python 的實驗設計

常態性檢定

 

  • Shapiro–Wilk(適合較小資料集)
  • D'Agostino $K^2$(使用 峰度偏度
  • Anderson–Darling(回傳一組數值)

 

$H_0$ = 「資料來自常態分配」

Python 的實驗設計

Shapiro–Wilk 檢定

 

from scipy.stats import shapiro
alpha = 0.05

stat, p = shapiro(salaries['salary']) print(f"p: {round(p,4)} test stat: {round(stat,4)}")
p: 0.8293 test stat: 0.9956
  • p > alpha
    • 未能拒絕 $H_0$ → 可能為常態
Python 的實驗設計

Anderson–Darling 檢定

from scipy.stats import anderson
result = anderson(x=salaries['salary'], dist="norm")
print(round(result.statistic,4))
print(result.significance_level)
print(result.critical_values)
0.2748
[15.  10.   5.   2.5  1. ]
[0.572 0.651 0.781 0.911 1.084]
  • 0.2748 < [0.572 0.651 0.781 0.911 1.084]
    • 未能拒絕 $H_0$ → 可能為常態
Python 的實驗設計

一起來練習吧!

Python 的實驗設計

Preparing Video For Download...