如何在 Python 中拟合 GLM?

Python 中的广义线性模型

Ita Cirovic Donev

Data Science Consultant

statsmodels

  • 导入 statsmodels

    import statsmodels.api as sm
    
  • 公式接口支持

    import statsmodels.formula.api as smf
    
  • 直接使用 glm()

    from statsmodels.formula.api import glm
    
Python 中的广义线性模型

模型拟合流程

  1. 描述模型 $\rightarrow \texttt{\color{#007AFF}{glm()}}$
  2. 拟合模型 $\rightarrow \texttt{\color{#007AFF}{.fit()}}$
  3. 总结模型 $\rightarrow \texttt{\color{#007AFF}{.summary()}}$
  4. 生成预测 $\rightarrow \texttt{\color{#007AFF}{.predict()}}$
Python 中的广义线性模型

描述模型

基于 FORMULA

from statsmodels.formula.api import glm
model = glm(formula, data, family)

基于 ARRAY

import statsmodels.api as sm
X = sm.add_constant(X)
model = sm.glm(y, X, family)
Python 中的广义线性模型

formula 参数

$$\texttt{\color{#00A388}{response}} \sim \texttt{\color{#FF6138}{explanatory variable(s)}}$$ $$\texttt{\color{#00A388}{output}} \sim \texttt{\color{#FF6138}{input(s)}}$$

formula = 'y ~ x1 + x2'
  • $\texttt{\color{#FF6138}{C(x1)}}$:将 x1 视为分类变量
  • $\texttt{\color{#FF6138}{-1}}$:去除截距
  • $\texttt{\color{#FF6138}{x1:x2}}$:x1x2 的交互项
  • $\texttt{\color{#FF6138}{x1*x2}}$:交互项及各自主效应
  • $\texttt{\color{#FF6138}{np.log(x1)}}$:对变量应用向量化函数
Python 中的广义线性模型

family 参数

family = sm.families.____()

分布族函数:

  • $\texttt{\color{#007AFF}{Gaussian}(link = sm.families.links.\color{deeppink}{identity()})}$ $\rightarrow$ 默认分布族
  • $\texttt{\color{#007AFF}{Binomial}(link = sm.families.links.\color{deeppink}{logit()})}$
    • $\texttt{\color{deeppink}{probit()}}$, $\texttt{\color{deeppink}{cauchy()}}$, $\texttt{\color{deeppink}{log()}}$, 和 $\texttt{\color{deeppink}{cloglog()}}$
  • $\texttt{\color{#007AFF}{Poisson}(link = sm.families.links.\color{deeppink}{log()})}$
    • $\texttt{\color{deeppink}{identity()}}$ 和 $\texttt{\color{deeppink}{sqrt()}}$

更多分布族参见 statsmodels 网站

Python 中的广义线性模型

模型总结

print(model_GLM.summary())
Python 中的广义线性模型
                 Generalized Linear Model Regression Results                  
=============================================================================
Dep. Variable:                      y  No. Observations:                  173
Model:                            GLM  Df Residuals:                      171
Model Family:                Binomial  Df Model:                            1
Link Function:                  logit  Scale:                          1.0000
Method:                          IRLS  Log-Likelihood:                -97.226
Date:                Mon, 21 Jan 2019  Deviance:                       194.45
Time:                        11:30:01  Pearson chi2:                     165.
No. Iterations:                     4  Covariance Type:             nonrobust
=============================================================================
                 coef    std err         z      P>|z|      [0.025      0.975]
-----------------------------------------------------------------------------
Intercept    -12.3508      2.629    -4.698      0.000     -17.503      -7.199
width          0.4972      0.102     4.887      0.000       0.298       0.697
=============================================================================
Python 中的广义线性模型

回归系数

$\texttt{\color{#007AFF}{.params}}$ 输出回归系数

model_GLM.params
Intercept   -12.350818
width         0.497231
dtype: float64

$\texttt{\color{#007AFF}{.conf\_int(alpha=0.05, cols=None)}}$ 输出置信区间

model_GLM.conf_int()
                   0         1
Intercept -17.503010 -7.198625
width       0.297833  0.696629
Python 中的广义线性模型

预测

  • 在测试数据中包含所有模型变量
  • $\texttt{\color{#007AFF}{.predict(\color{#FF931B}{test\_data})}}$ 计算预测值
model_GLM.predict(test_data)
0    0.029309
1    0.470299
2    0.834983
3    0.972363
4    0.987941
Python 中的广义线性模型

Passons à la pratique !

Python 中的广义线性模型

Preparing Video For Download...