Skip to content

Commit cfd26e4

Browse files
double-freeYe Yuan
and
Ye Yuan
authored
Ye.yuan/ols (#1)
* add ols doc * finish f-statistic * complete table 2 --------- Co-authored-by: Ye Yuan <[email protected]>
1 parent 50ababc commit cfd26e4

File tree

4 files changed

+174
-0
lines changed

4 files changed

+174
-0
lines changed
+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
# Interpret OLS Regression Result
2+
3+
OLS (ordinary least square) 是一种基础的线性回归方法。本文的 “OLS result” 特指使用 `statsmodels.api.OLS` 得到的结果。
4+
5+
```py
6+
import statsmodels.api as sm
7+
8+
data = sm.datasets.ccard.load_pandas()
9+
X, y = data.exog.copy(), data.endog.copy()
10+
X = sm.add_constant(X)
11+
res = sm.OLS(y, X).fit()
12+
res.summary()
13+
```
14+
15+
运行以上代码可以得到一个表格:
16+
17+
![Inclusion-Exclusion Principle](images/OLS_summary.jpeg)
18+
19+
表格里是评估模型有效性的指标,但是解读起来需要一定的统计基础。本文将逐个解读其含义以及背后的统计学基础知识。
20+
21+
该表格分为了三个子表格,分别表达:
22+
23+
1. 评估模型显著性
24+
2. 评估单个模型参数显著性
25+
3. 评估模型偏差
26+
27+
我们将逐个表格讲解。
28+
29+
## Table 1: Model Overview
30+
31+
跳过简单的字段。
32+
33+
### Df (Degree of freedom)
34+
35+
自由度是一个基础概念,在回归问题中,如果模型自由度=样本数,那么该回归问题退化到一个解方程的问题。因此我们一般希望有较高的 Df Residuals。自由度满足:
36+
37+
> No. Observations = Df Residuals + Df Model
38+
39+
注意,当我们添加常数项,即:
40+
41+
```py
42+
X = sm.add_constant(X)
43+
```
44+
45+
因为常数项同样需要估计(也可以看作模型的一部分),该公式变为:
46+
47+
> No. Observations = Df Residuals + Df Model + 1
48+
49+
### R-squared
50+
51+
R-squared 描述该模型对样本输入的拟合度。在本例中,其值为 0.244,即 24.4% 的变化可以被该模型解释。
52+
它定义为:
53+
54+
55+
$$ R^2 = 1 - \frac{\sum_{i=1}^n (y_i - \hat{y})^2}{\sum_{i=1}^n (y_i - \bar{y})^2} $$
56+
57+
即,1 减去(模型误差平方和/以均值为预测值的误差平方和)。
58+
59+
```py
60+
1.0 - res.ssr / res.centered_tss
61+
```
62+
可以通过这个代码检查,算出来 R-squared 是 `0.24357791682194052`,与表格相同。
63+
64+
直观来看,如果模型越准确,则 R-squared 值越接近 1.0。如果直接取样本输出的平均值为预测值,则 R-squared 为 0.0。如果模型甚至不如直接取样本的平均值准确,则 R-squared 可以为负值。
65+
66+
似乎,R-squared 越大,拟合效果越好。但是我们忽略了一个重要的因素,即上面提到的“自由度”。
67+
只要我们往模型中增加参数,必然可以增加 R-squared,直到模型自由度与样本数相同,最终 R-squared = 1.0。但是这样得到的并不是一个有效的模型,而是存在明显的过拟合。
68+
69+
因此,我们需要引入 Adjusted R-squared。
70+
71+
#### Adj. R-squared
72+
73+
Adjusted R-squared 只会在新加入的模型变量有助于提高模型拟合度时增加,加入一个与结果无关的变量会降低它的数值。
74+
75+
它定义为:
76+
77+
$$\begin{aligned}
78+
\bar{R}^2 &= 1 - \frac{SS_{res} / df_{res}}{SS_{tot} / df_{tot}} \\
79+
&= 1 - \frac{\sum_{i=1}^n (y_i - \hat{y})^2 / (n - p - 1)}{\sum_{i=1}^n (y_i - \bar{y})^2 / (n - 1)} \\
80+
&= 1 - (1 - R^2)\frac{n-1}{n - p - 1}
81+
\end{aligned}$$
82+
83+
其中 p 是模型参数数量。
84+
85+
```py
86+
1 - (res.ssr / (res.nobs - res.df_model - 1)) / (res.centered_tss / (res.nobs - 1))
87+
```
88+
可以通过这个代码检查,算出来 Adj. R-squared 是 `0.1984183894680266`,与表格相同。
89+
90+
根据公式,有两个推论:
91+
92+
1. Adj. R-squared 可以为负
93+
2. Adj. R-squared <= R-squared
94+
95+
### F-statistic
96+
97+
F-statistic 衡量模型整体的显著性(后面要讲到的 t-statistic 是衡量单个模型参数的显著性)。
98+
99+
我们的原假设是:__模型所有参数为 0__。即,该模型是无效的,无论因子如何变化,都对结果不产生影响。
100+
101+
F-distribution 定义为两个 variance 的比值的分布:
102+
103+
$$ X = \frac{S_1 / df_1}{S_2 / df_2} $$
104+
105+
其中 $S_1$ 服从自由度为 $df_1$ 的 chi-squared 分布,$S_2$ 服从自由度为 $df_2$ 的 chi-squared 分布。
106+
107+
![F distribution](images/F_distribution.png)
108+
109+
F-value 越大,说明模型相比噪声越显著。即:
110+
111+
$$ \text{F-value} = \frac{\text{variance of y explained by model}}{\text{variance of y explained by error}} $$
112+
113+
```py
114+
(res.ess / res.df_model) / (res.ssr / res.df_resid)
115+
```
116+
可以通过这个代码检查,算出来 F-value 是 `5.393721570932906`,与表格相同。
117+
118+
#### Prob(F-statistic)
119+
120+
F-distribution 由两个自由度的值确定,代入 F-statistic 可以得出其 p-value,就是这里的 Prob(F-statistic)。这个 p-value 的含义是原假设(__模型所有参数为 0__ )成立情况下,出现当前结果的概率。它的 __值越小,说明这个模型越显著__
121+
122+
### AIC and BIC
123+
124+
> It stands for Akaike’s Information Criteria and is used for model selection. It penalizes the errors mode in case a new variable is added to the regression equation. It is calculated as number of parameters minus the likelihood of the overall model. A lower AIC implies a better model. Whereas, BIC stands for Bayesian information criteria and is a variant of AIC where penalties are made more severe.
125+
126+
127+
## Table 2: Model Parameter
128+
129+
这个表格主要是逐个参数的分析。
130+
131+
### Coef & std err
132+
133+
`Coef` 一列即模型参数的估计值。对于 OLS 这样的线性模型,这个值表明其对应变量 X_i 的每个单位贡献多少的 y。
134+
135+
`std err`表示该参数估计的不确定性。
136+
137+
### t
138+
139+
`t` 列表示 t-statistic 的值。这里的 t 是指“学生分布”。引入它是为了在不知道 __总体均值__,也不知道 __总体方差__ 的情况下,检查 __两个总体是否有显著不同__
140+
141+
学生分布是用于描述 __样本均值__ 的分布。假设总体 x 符合正态分布 $N(\mu, \sigma^2)$ (均值方差都是未知)。我们采 n 个样本:
142+
143+
$$\begin{aligned}
144+
\bar{x} &= \frac{x_1 + x_2 + \cdots + x_n}{n} \\
145+
s^2 = \frac{1}{n-1} \sum_{i=1}^n (x_i - \bar{x})^2
146+
\end{aligned}$$
147+
148+
其中 $\bar{x}$ 是对 $\mu$ 的无偏估计,$s^2$ 是对 $\sigma^2$ 的无偏估计。我们 __将样本均值标准化__,它应该符合 __自由度为 n-1__ 的 t 分布:
149+
150+
$$ t = \frac{\bar{x} - \mu_0}{\sqrt{s^2 / n}} \sim t_{n-1} $$
151+
152+
注意,我们并不知道真实的 $\mu$,此处的 $\mu_0$ 正是我们的 __原假设__:该总体的均值为 $\mu_0$。这个计算出来的 t-value 代入 t 分布中就能知道 p-value:
153+
154+
![Student's t distribution](images/Student_t.png)
155+
156+
回到我们的表格中,因为我们检验的 __原假设是参数无效__,因此 $\mu_0 = 0$。`coef` 即参数的均值,`std err` 是 coef 的标准差,即 __均值的标准差__,他们直接相除就是 t-statistic:
157+
158+
```py
159+
res.params / res.bse
160+
```
161+
162+
执行这个命令即可以算出表格中的 t-values。
163+
164+
#### P > |t|
165+
166+
这是上面算出来的 t-values 对应的 p-values。将以上 t-values 代入自由度为 n-1 的 t 分布上即可获得 p-values。与 `Prob (F-statistic)` 类似,p-value 越小说明对应的参数越显著。
167+
168+
## Reference
169+
170+
1. [Interpreting OLS results](https://desktop.arcgis.com/en/arcmap/latest/tools/spatial-statistics-toolbox/interpreting-ols-results.htm)
171+
172+
2. [Regression analysis basics](https://desktop.arcgis.com/en/arcmap/latest/tools/spatial-statistics-toolbox/regression-analysis-basics.htm)
173+
174+
3. [Understanding t-test for linear regression](https://stats.stackexchange.com/questions/344006/understanding-t-test-for-linear-regression)
Loading
Loading
Loading

0 commit comments

Comments
 (0)