用 GAM 學非線性轉換

R 中的監督式學習:回歸

Nina Zumel and John Mount

Win-Vector, LLC

Generalized Additive Models(GAM)

$$ y \sim b0 + s1(x1) + s2(x2) + .... $$

R 中的監督式學習:回歸

學習非線性關係

R 中的監督式學習:回歸

mgcv 套件中的 gam()

gam(formula, family, data)

family:

  • gaussian(預設):「一般」迴歸
  • binomial:機率
  • poisson/quasipoisson:計數

較適合大型資料集

R 中的監督式學習:回歸

s() 函式

anx ~ s(hassles)
  • s() 表示該變數以非線性建模
  • s() 用於連續變數
    • 唯一值多於約 10 個
R 中的監督式學習:回歸

再看 hassles 資料

R 中的監督式學習:回歸

再看 hassles 資料

模型 RMSE(交叉驗證) $R^2$(訓練)
線性($hassles$) 7.69 0.53
二次($hassles^2$) 6.89 0.63
三次($hassles^3$) 6.70 0.65
R 中的監督式學習:回歸

hassles 資料的 GAM

model <- gam(
  anx ~ s(hassles), 
  data = hassleframe, 
  family = gaussian
)

summary(model)
...

R-sq.(adj) =  0.619   Deviance explained = 64.1%
GCV = 49.132  Scale est. = 45.153    n = 40
R 中的監督式學習:回歸

檢視轉換

plot(model)

$y$ 值:predict(model, type = "terms")

R 中的監督式學習:回歸

用模型做預測

predict(model, newdata = hassleframe, type = "response")

R 中的監督式學習:回歸

比較樣本外表現

若已知正確轉換最佳;未知時,GAM 很實用。

模型 RMSE(交叉驗證) $R^2$(訓練)
線性($hassles$) 7.69 0.53
二次($hassles^2$) 6.89 0.63
三次($hassles^3$) 6.70 0.65
GAM 7.06 0.64
  • 資料集小 $\rightarrow$ GAM 較雜訊
R 中的監督式學習:回歸

一起來練習吧!

R 中的監督式學習:回歸

Preparing Video For Download...