類別與交互項

Generalized Linear Models in Python

Ita Cirovic Donev

Data Science Consultant

類別變數

  • 簡單二元變數
    • Yes, No
  • 名目變數
    • 顏色:red, green, blue
  • 次序變數
    • 教育程度:Education1, Education2,...,Education4
Generalized Linear Models in Python

共變數分析

  • 解釋變數

    • $x_1$:類別(二元)
    • $x_2$:連續
  • 邏輯斯模型 $$ \text{logit}(y=1|X)=\beta_0 + \beta_1x_1 + \beta_2x_2 $$

Generalized Linear Models in Python

共變數分析

  • 解釋變數
    • $x_1$:類別(二元)
    • $x_2$:連續
  • 邏輯斯模型 $$ \text{logit}(y=1|\color{red}{X})=\beta_0 + \beta_1\color{red}{x_1} + \beta_2\color{red}{x_2} $$
Generalized Linear Models in Python

共變數分析

  • 解釋變數
    • $x_1$:類別(二元)
    • $x_2$:連續
  • 邏輯斯模型 $$ \text{logit}(y=1|X)=\beta_0 + \beta_1\color{red}{x_1} + \beta_2x_2 $$

  • 若 $x_1=0$,則 $$ \text{logit}(y=1|\color{red}{x_1=0},x_2)=\beta_0 + \color{red}{0} + \beta_2x_2 $$

Generalized Linear Models in Python

共變數分析

  • 解釋變數
    • $x_1$:類別(二元)
    • $x_2$:連續
  • 邏輯斯模型 $$ \text{logit}(y=1|X)=\beta_0 + \beta_1\color{red}{x_1} + \beta_2x_2 $$

  • 若 $x_1=0$,則 $$ \text{logit}(y=1|x_1=0,x_2)=\beta_0 + 0 + \beta_2x_2 $$

  • 若 $x_1 = 1$,則 $$ \text{logit}(y=1|\color{red}{x_1=1},x_2)=\beta_0 + \color{red}{\beta_1} + \beta_2x_2 $$ $$ \text{logit}(y=1|\color{red}{x_1=1},x_2)=(\beta_0 + \color{red}{\beta_1}) + \beta_2x_2 $$

Generalized Linear Models in Python

假設

無交互時為平行線的視覺化。

Generalized Linear Models in Python

假設

無交互模型中截距差異的強調。

Generalized Linear Models in Python

假設

無交互模型

非平行線導致含交互項的模型。

Generalized Linear Models in Python

交互作用

  • 斜率不相等 $\rightarrow$ 存在交互作用
  • $x_1$ 對 $y$ 的影響取決於 $x_2$ 的水準,反之亦然
  • 允許交互的邏輯斯模型 $$ \text{logit}(y=1|X)=\beta_0 + \beta_1x_1 + \beta_2x_2 + \color{red}{\beta_3x_1x_2} $$
Generalized Linear Models in Python

交互作用

  • 斜率不相等 $\rightarrow$ 存在交互作用
  • $x_1$ 對 $y$ 的影響取決於 $x_2$ 的水準,反之亦然
  • 允許交互的邏輯斯模型 $$ \text{logit}(y=1|X)=\beta_0 + \beta_1x_1 + \beta_2x_2 + \beta_3x_1x_2 $$

  • 若 $x_1=0$,則 $$ \text{logit}(y=1|\color{red}{x_1=0},x_2)=\beta_0 + \color{red}{0} + \beta_2x_2 + \color{red}{0} $$

Generalized Linear Models in Python

交互作用

  • 斜率不相等 $\rightarrow$ 存在交互作用
  • $x_1$ 對 $y$ 的影響取決於 $x_2$ 的水準,反之亦然
  • 允許交互的邏輯斯模型 $$ \text{logit}(y=1|X)=\beta_0 + \beta_1x_1 + \beta_2x_2 + \beta_3x_1x_2 $$

  • 若 $x_1=0$,則 $$ \text{logit}(y=1|x_1=0,x_2)=\beta_0 + \beta_2x_2 $$

  • 若 $x_1 = 1$,則 $$ \text{logit}(y=1|\color{red}{x_1=1},x_2)=\beta_0 + \color{red}{\beta_1} + \beta_2x_2 + \color{red}{\beta_3}x_2 $$ $$ \text{logit}(y=1|\color{red}{x_1=1},x_2)=(\beta_0 + \color{red}{\beta_1}) + (\beta_2 + \color{red}{\beta_3})x_2 $$

Generalized Linear Models in Python

交互作用

  • 斜率不相等 $\rightarrow$ 存在交互作用
  • $x_1$ 對 $y$ 的影響取決於 $x_2$ 的水準,反之亦然
  • 允許交互的邏輯斯模型 $$ \text{logit}(y=1|X)=\beta_0 + \beta_1x_1 + \beta_2x_2 + \beta_3x_1x_2 $$

  • 若 $x_1=0$,則 $$ \text{logit}(y=1|x_1=0,x_2)=\color{red}{\beta_0} + \color{red}{\beta_2}x_2 $$

  • 若 $x_1 = 1$,則 $$ \text{logit}(y=1|x_1=1,x_2)=\beta_0 + \beta_1 + \beta_2x_2 + \beta_3x_2 $$ $$ \text{logit}(y=1|x_1=1,x_2)=\color{red}{(\beta_0 + \beta_1)} + \color{red}{(\beta_2 + \beta_3)}x_2 $$

Generalized Linear Models in Python

視覺化交互作用

含交互項模型的示意(非平行線)。

交互作用可讓:

  • $x_1$ 的截距與斜率不同
  • $\beta_1$:兩條截距之差
  • $\beta_3$:兩條斜率之差
Generalized Linear Models in Python

交互類型

  • 二元 $\times$ 二元
  • 二元 $\times$ 類別
  • 二元 $\times$ 連續
  • 連續 $\times$ 類別
  • 連續 $\times$ 連續
  • 類別 $\times$ 類別
  • 超過 2 個變數的交互
Generalized Linear Models in Python

一起來練習吧!

Generalized Linear Models in Python

Preparing Video For Download...