解释模型推断

Python 中的广义线性模型

Ita Cirovic Donev

Data Science Consultant

β 系数的估计

  • 极大似然估计(MLE)
  • 估计系数 $\hat\beta$
    • 对数似然取最大值

似然函数

Python 中的广义线性模型

β 系数的估计

  • 迭代加权最小二乘(IRLS)

拟合模型的汇总输出

Python 中的广义线性模型

显著性检验

拟合模型的摘要输出,突出显示系数估计相关统计量。

Python 中的广义线性模型

标准误(SE)

  • 峰更平缓
    $\rightarrow$ 最大值位置更难确定
    $\rightarrow$ SE 更大

当标准误更大时的似然可视化。

  • 峰更尖锐
    $\rightarrow$ 最大值位置更清晰
    $\rightarrow$ SE 更小

当标准误更小时的似然可视化。

Python 中的广义线性模型

标准误的计算

# Extract variance-covariance matrix
print(model_GLM.cov_params())
           Intercept    weight
Intercept   0.774762 -0.325087
weight     -0.325087  0.141903
# Compute standard error for weight
std_error = np.sqrt(0.141903)
0.3767

方差-协方差矩阵

方差-协方差矩阵示意图

Python 中的广义线性模型

显著性检验

  • z 统计量 $$ \color{#2485F2}{z=\hat\beta/SE} $$

  • $\color{#2485F2}{z}$ 大 $\Rightarrow$ 系数 $\ne0$ $\Rightarrow$ 变量显著

  • 经验法则:阈值为 2

示例:马蹄蟹模型
y ~ weight

$z = 1.8151/0.377 = 4.819$

Python 中的广义线性模型

β 的置信区间

  • 估计的不确定性
  • β 的 95% 置信区间

$$ [\color{#5A5AF3}{下限},\color{#D8498E}{上限}] $$

$$ [\color{#5A5AF3}{\hat\beta - 1.96 \times SE},\color{#D8498E}{\hat\beta+1.96 \times SE}] $$

Python 中的广义线性模型

计算置信区间

示例:马蹄蟹模型

                 coef    std err   
<hr />-------------------------------
Intercept     -3.6947      0.880  
weight         1.8151      0.377  

Python 中的广义线性模型

提取置信区间

print(model_GLM.conf_int())
                  0         1
Intercept -5.419897 -1.969555
weight     1.076826  2.553463
Python 中的广义线性模型

提取置信区间

print(model_GLM.conf_int())
              lower         1
Intercept -5.419897 -1.969555
weight     1.076826  2.553463
Python 中的广义线性模型

提取置信区间

print(model_GLM.conf_int())
                  0     upper
Intercept -5.419897 -1.969555
weight     1.076826  2.553463
Python 中的广义线性模型

胜算比的置信区间

  1. 提取 $\beta$ 的置信区间

  2. 对端点取指数

print(np.exp(model_GLM.conf_int()))
                  0          1
Intercept  0.004428   0.139519
weight     2.935348  12.851533
Python 中的广义线性模型

Passons à la pratique !

Python 中的广义线性模型

Preparing Video For Download...