係數詮釋

Generalized Linear Models in Python

Ita Cirovic Donev

Data Science Consultant

模型係數

已配適模型的統計摘要,突出顯示 coef 欄。

Generalized Linear Models in Python

係數 beta

  • $\beta > 0 \rightarrow$ 曲線遞增

砷含量與是否更換的羅吉斯模型配適。

  • $\beta < 0 \rightarrow$ 曲線遞減

distance100 與是否更換的羅吉斯模型配適。

Generalized Linear Models in Python

線性 vs 羅吉斯

LINEAR MODEL

glm('y ~ weight', 
    data = crab, 
    family = sm.families.Gaussian())

$\mu = -0.14 + \color{#B21AB4}{0.32}*weight$

當 weight 每增加 1 單位時

  • $\text{\color{#B21AB4}{估計機率}}$ 增加 0.32

LOGIT MODEL

glm('y ~ weight', 
    data = crab, 
    family = sm.families.Binomial())

$log(odds) = -3.69 + \color{#228FF5}{1.8}*weight$

當 weight 每增加 1 單位時

  • $\text{\color{#228FF5}{log(odds)}}$ 增加 1.8
Generalized Linear Models in Python

對數勝算的詮釋

  • 羅吉斯模型 $$ log(\frac{\mu}{1-\mu}) = \beta_0 + \beta_1x_1 $$

  • $x$ 增加 1 單位 $$ log(\frac{\mu}{1-\mu}) = \beta_0 + \beta_1\color{blue}{(x_1+1)} $$

Generalized Linear Models in Python

對數勝算的詮釋

  • 羅吉斯模型 $$ log(\frac{\mu}{1-\mu}) = \beta_0 + \beta_1x_1 $$

  • $x$ 增加 1 單位 $$ log(\frac{\mu}{1-\mu}) = \beta_0 + \beta_1\color{blue}{(x_1+1)} = \beta_0 + \color{blue}{\beta_1x_1+\beta_1} $$

  • 取指數 $$ (\frac{\mu}{1-\mu}) = \color{red}{\exp(\beta_0 + \beta_1x_1)}\color{blue}{\exp(\beta_1)} $$

結論 $\rightarrow$ $\color{red}{\text{勝算}}$ 會被 $\color{blue}{\exp(\beta_1)}$ 乘上

Generalized Linear Models in Python

對數勝算的詮釋

  • 螃蟹模型 y ~ weight $$ log(\frac{\mu}{1-\mu}) = -3.6947 + \color{blue}{1.815}*weight $$

  • weight 增加 1 單位時,衛星蟹的勝算會乘上 $\color{blue}{\exp(1.815) = 6.14}$

Generalized Linear Models in Python

對數勝算的詮釋

  • 螃蟹模型 y ~ weight $$ log(\frac{\mu}{1-\mu}) = \color{blue}{-3.6947} + 1.8151*weight $$

  • weight 增加 1 單位時,衛星蟹的勝算會乘上 $\exp(1.8151) = 6.14$

  • 截距係數 $\color{blue}{-3.6947}$ 表示基準對數勝算
    • 當 $weight = 0$ 時,勝算為 $\color{blue}{\exp(-3.6947)=0.0248}$。
Generalized Linear Models in Python

機率 vs 羅吉斯配適

讀書時數與通過/未通過測驗的散佈圖上之羅吉斯配適。

Generalized Linear Models in Python

機率 vs 羅吉斯配適

在羅吉斯配適與解釋變數值下,機率的小幅變化。

Generalized Linear Models in Python

機率 vs 羅吉斯配適

在羅吉斯配適與解釋變數值下,機率的大幅變化。

  • 斜率 $\rightarrow \beta \times \mu(1-\mu)$
Generalized Linear Models in Python

機率 vs 羅吉斯配適

在羅吉斯配適與解釋變數值下,機率的最大變化。

  • 斜率 $\rightarrow \beta \times \mu(1-\mu)$
Generalized Linear Models in Python

計算估計機率的變化量

# 選擇 x(weight),並取出模型係數
x = 1.5
intercept, slope = model_GLM.params
# 計算估計機率
est_prob = np.exp(intercept + slope * x)/(1 + np.exp(intercept + slope * x))
0.2744
# 在給定 x 下,計算估計機率的增量變化
ic_prob = slope * est_prob * (1 - est_prob)
0.3614
Generalized Linear Models in Python

每個 x 的機率變化率

$logit = -3.6947 + 1.8151*weight$

Generalized Linear Models in Python

一起來練習吧!

Generalized Linear Models in Python

Preparing Video For Download...