重回帰分析

『Rによる計量経済学』第7章「重回帰分析」をPythonで実行する。
テキスト付属データセット(「k0701.csv」等)については出版社サイトよりダウンロードしてください。

例題7-1

「k0701.csv」を重回帰分析。


In [1]:
%matplotlib inline

In [2]:
# -*- coding:utf-8 -*-
from __future__ import print_function
import numpy as np
import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')

In [3]:
# データ読み込み
data = pd.read_csv('example/k0701.csv')

In [4]:
# 説明変数設定
X = data[['X', 'Z']]
X = sm.add_constant(X)
X


Out[4]:
const X Z
0 1 2 7
1 1 2 4
2 1 4 4
3 1 6 5
4 1 7 3
5 1 9 3

In [5]:
# 被説明変数設定
Y = data['Y']
Y


Out[5]:
0    1
1    3
2    4
3    5
4    7
5    8
Name: Y, dtype: int64

In [6]:
# OLSの実行(Ordinary Least Squares: 最小二乗法)
model = sm.OLS(Y,X)
results = model.fit()
print(results.summary())


                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      Y   R-squared:                       0.997
Model:                            OLS   Adj. R-squared:                  0.994
Method:                 Least Squares   F-statistic:                     427.4
Date:                Sun, 19 Jul 2015   Prob (F-statistic):           0.000207
Time:                        04:06:06   Log-Likelihood:                 3.3092
No. Observations:                   6   AIC:                           -0.6183
Df Residuals:                       3   BIC:                            -1.243
Df Model:                           2                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
const          4.1528      0.504      8.235      0.004         2.548     5.758
X              0.6528      0.041     15.781      0.001         0.521     0.785
Z             -0.6347      0.078     -8.167      0.004        -0.882    -0.387
==============================================================================
Omnibus:                          nan   Durbin-Watson:                   2.662
Prob(Omnibus):                    nan   Jarque-Bera (JB):                0.492
Skew:                          -0.356   Prob(JB):                        0.782
Kurtosis:                       1.792   Cond. No.                         43.6
==============================================================================

Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.