作者:chen_h
微信号 & QQ:862251340
微信公众号:coderpai
很多时候,线性回归与机器学习关联紧密 —— 这是今年来备受关注的热门话题。因此,在本教程中,我将向你展示如何使用 statsmodels 在 Python 中执行线性回归。我将用一个关于股市的而简单例子来证明这个概念。目录如下:
首先,我将分享一些关于线性回归的背景知识。
线性回归用作预测模型,假设因变量(我们师徒预测/估计的变量)与自变量 s (预测中使用的输入变量 s)之间存在线性关系。
例如,你可以使用线性回归来根据以下宏观经济学作为输入变量预测股票市场的价格:
在简单线性回归下,只使用一个独立输入变量来预测因变量。它具有如下结构:
Y = C + M ∗ X Y = C + M*X Y=C+M∗X
实际上,因变量和多个自变量之间可能存在关系。对于这些类型的模型(假设线性),我们可以使用具有以下结构的多元线性回归:
Y = C + M 1 ∗ X 1 + M 2 ∗ X 2 + . . . Y = C+M_{1}*X_{1}+M_{2}*X_{2}+... Y=C+M1∗X1+M2∗X2+...
为了便于说明,我们假设你有一个虚拟的经济数据,其中包含以下参数:
Year | Month | Interest_Rate | Unemployment_Rate | Stock_Index_Price |
---|---|---|---|---|
2017 | 12 | 2.75 | 5.3 | 1464 |
2017 | 11 | 2.5 | 5.3 | 1394 |
2017 | 10 | 2.5 | 5.3 | 1357 |
2017 | 9 | 2.5 | 5.3 | 1293 |
2017 | 8 | 2.5 | 5.4 | 1256 |
2017 | 7 | 2.5 | 5.6 | 1254 |
2017 | 6 | 2.5 | 5.5 | 1234 |
2017 | 5 | 2.25 | 5.5 | 1195 |
2017 | 4 | 2.25 | 5.5 | 1159 |
2017 | 3 | 2.25 | 5.6 | 1167 |
2017 | 2 | 2 | 5.7 | 1130 |
2017 | 1 | 2 | 5.9 | 1075 |
2016 | 12 | 2 | 6 | 1047 |
2016 | 11 | 1.75 | 5.9 | 965 |
2016 | 10 | 1.75 | 5.8 | 943 |
2016 | 9 | 1.75 | 6.1 | 958 |
2016 | 8 | 1.75 | 6.2 | 971 |
2016 | 7 | 1.75 | 6.1 | 949 |
2016 | 6 | 1.75 | 6.1 | 884 |
2016 | 5 | 1.75 | 6.1 | 866 |
2016 | 4 | 1.75 | 5.9 | 876 |
2016 | 3 | 1.75 | 6.2 | 822 |
2016 | 2 | 1.75 | 6.2 | 704 |
2016 | 1 | 1.75 | 6.1 | 719 |
这里的目标是根据两个宏观经济学变量预测 / 估计股票指数价格:利率和失业率。
我们将使用 pandas DataFrame 在 Python 中捕获上述数据。在深入研究 Python 代码之前,请确保已经安装 statsmodels 和 pandas 软件包。你可以使用 pip 方法来安装这些包。
以下 Python 代码包含多元线性回归的例子,其中输入变量为:
这两个变量用于预测 Stock_Index_Price 的因变量。
或者,你可以通过在代码中仅保留一个输入变量来应用简单线性回归。例如,如果你只想使用利率作为输入变量,只需使用以下 X = df[‘Interest_Rate’] 的语法,而不是使用 X = df[[‘Interest_Rate’, ‘Unemployment_Rate’]]
from pandas import DataFrame
import statsmodels.api as sm
Stock_Market = {'Year': [2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2016,2016,2016,2016,2016,2016,2016,2016,2016,2016,2016,2016],
'Month': [12, 11,10,9,8,7,6,5,4,3,2,1,12,11,10,9,8,7,6,5,4,3,2,1],
'Interest_Rate': [2.75,2.5,2.5,2.5,2.5,2.5,2.5,2.25,2.25,2.25,2,2,2,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75,1.75],
'Unemployment_Rate': [5.3,5.3,5.3,5.3,5.4,5.6,5.5,5.5,5.5,5.6,5.7,5.9,6,5.9,5.8,6.1,6.2,6.1,6.1,6.1,5.9,6.2,6.2,6.1],
'Stock_Index_Price': [1464,1394,1357,1293,1256,1254,1234,1195,1159,1167,1130,1075,1047,965,943,958,971,949,884,866,876,822,704,719]
}
df = DataFrame(Stock_Market,columns=['Year','Month','Interest_Rate','Unemployment_Rate','Stock_Index_Price'])
X = df[['Interest_Rate','Unemployment_Rate']] # here we have 2 variables for multiple regression. If you just want to use one variable for simple linear regression, then use X = df['Interest_Rate'] for example.Alternatively, you may add additional variables within the brackets
Y = df['Stock_Index_Price']
X = sm.add_constant(X) # adding a constant
model = sm.OLS(Y, X).fit()
predictions = model.predict(X)
print_model = model.summary()
print(print_model)
这是输出结果:
OLS Regression Results
==============================================================================
Dep. Variable: Stock_Index_Price R-squared: 0.898
Model: OLS Adj. R-squared: 0.888
Method: Least Squares F-statistic: 92.07
Date: Thu, 08 Nov 2018 Prob (F-statistic): 4.04e-11
Time: 19:44:31 Log-Likelihood: -134.61
No. Observations: 24 AIC: 275.2
Df Residuals: 21 BIC: 278.8
Df Model: 2
Covariance Type: nonrobust
=====================================================================================
coef std err t P>|t| [0.025 0.975]
-------------------------------------------------------------------------------------
const 1798.4040 899.248 2.000 0.059 -71.685 3668.493
Interest_Rate 345.5401 111.367 3.103 0.005 113.940 577.140
Unemployment_Rate -250.1466 117.950 -2.121 0.046 -495.437 -4.856
==============================================================================
Omnibus: 2.691 Durbin-Watson: 0.530
Prob(Omnibus): 0.260 Jarque-Bera (JB): 1.551
Skew: -0.612 Prob(JB): 0.461
Kurtosis: 3.226 Cond. No. 394.
==============================================================================
Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
我强调了结果中的几个重要组成部分:
回想一下,我们的多元线性回归的等式为:
Y = C + M 1 ∗ X 1 + M 2 ∗ X 2 + . . . Y = C+M_{1}*X_{1}+M_{2}*X_{2}+... Y=C+M1∗X1+M2∗X2+...
所以对于我们的例子,它看起来像这样:
Stock_Index_Price = (const coef) + (Interest_Rate coef)\*X1 + (Unemployment_Rate coef)\*X2
然后,我们将系数插入到我们的方程,我们可以得到:
Stock_Index_Price = (1798.4040) + (345.5401)\*X1 + (-250.1466)\*X2
假设你想要预测股票指数价格,你刚刚收集了 2018 年第一个月的以下数据:
当你插入这些数字时,你会得到:
Stock_Index_Price = (1798.4040) + (345.5401)*X1 + (-250.1466)*X2
Stock_Index_Price = (1798.4040) + (345.5401)*(2.75) + (-250.1466)*(5.3) = 1422.86
因此,2018年1月 Stock_Index_Price 的预测 / 估计值为 1422.86。
最终可以将预测值与实际值进行比较,以检查准确度。例如,如果该月的实际股票指数价格为 1435,则预测将在 1435 - 1422.86 = 12.14 之间。
来源:dataofish