· 

Pythonで線形回帰モデルの計算 (2)

(著)山たー

 

よくよく調べたらPythonでも回帰分析をしてくれるライブラリがあった。勉強不足。

ソースコード

from sklearn.datasets import load_boston
import statsmodels.api as sm

#Boston Housing Dataをインポート
boston = load_boston()
X=boston.data
y=boston.target
n=X.shape[0] #サンプル数
p=X.shape[1] #説明変数の数
mod = sm.OLS(y, sm.add_constant(X))
res = mod.fit()
print(res.summary())

結果

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      y   R-squared:                       0.741
Model:                            OLS   Adj. R-squared:                  0.734
Method:                 Least Squares   F-statistic:                     108.1
Date:                Fri, 20 Apr 2018   Prob (F-statistic):          6.95e-135
Time:                        14:41:14   Log-Likelihood:                -1498.8
No. Observations:                 506   AIC:                             3026.
Df Residuals:                     492   BIC:                             3085.
Df Model:                          13                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const         36.4911      5.104      7.149      0.000      26.462      46.520
x1            -0.1072      0.033     -3.276      0.001      -0.171      -0.043
x2             0.0464      0.014      3.380      0.001       0.019       0.073
x3             0.0209      0.061      0.339      0.735      -0.100       0.142
x4             2.6886      0.862      3.120      0.002       0.996       4.381
x5           -17.7958      3.821     -4.658      0.000     -25.302     -10.289
x6             3.8048      0.418      9.102      0.000       2.983       4.626
x7             0.0008      0.013      0.057      0.955      -0.025       0.027
x8            -1.4758      0.199     -7.398      0.000      -1.868      -1.084
x9             0.3057      0.066      4.608      0.000       0.175       0.436
x10           -0.0123      0.004     -3.278      0.001      -0.020      -0.005
x11           -0.9535      0.131     -7.287      0.000      -1.211      -0.696
x12            0.0094      0.003      3.500      0.001       0.004       0.015
x13           -0.5255      0.051    -10.366      0.000      -0.625      -0.426
==============================================================================
Omnibus:                      178.029   Durbin-Watson:                   1.078
Prob(Omnibus):                  0.000   Jarque-Bera (JB):              782.015
Skew:                           1.521   Prob(JB):                    1.54e-170
Kurtosis:                       8.276   Cond. No.                     1.51e+04
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 1.51e+04. This might indicate that there are
strong multicollinearity or other numerical problems.