pythonによるDW test 例

サンプルデータ作成

In [1]:
import numpy as np
import pandas as pd

#create dataset
df = pd.DataFrame({'rating': [90, 85, 82, 88, 94, 90, 76, 75, 87, 86],
                   'points': [25, 20, 14, 16, 27, 20, 12, 15, 14, 19],
                   'assists': [5, 7, 7, 8, 5, 7, 6, 9, 9, 5],
                   'rebounds': [11, 8, 10, 6, 6, 9, 6, 10, 10, 7]})
In [2]:
df
Out[2]:
rating points assists rebounds
0 90 25 5 11
1 85 20 7 8
2 82 14 7 10
3 88 16 8 6
4 94 27 5 6
5 90 20 7 9
6 76 12 6 6
7 75 15 9 10
8 87 14 9 10
9 86 19 5 7

線型回帰モデル作成

In [3]:
from statsmodels.formula.api import ols

#fit multiple linear regression model
model = ols('rating ~ points + assists + rebounds', data=df).fit()

#view model summary
print(model.summary())
                            OLS Regression Results                            
==============================================================================
Dep. Variable:                 rating   R-squared:                       0.623
Model:                            OLS   Adj. R-squared:                  0.434
Method:                 Least Squares   F-statistic:                     3.299
Date:                Wed, 03 Nov 2021   Prob (F-statistic):             0.0995
Time:                        12:28:54   Log-Likelihood:                -26.862
No. Observations:                  10   AIC:                             61.72
Df Residuals:                       6   BIC:                             62.93
Df Model:                           3                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
Intercept     62.4716     14.588      4.282      0.005      26.776      98.168
points         1.1193      0.411      2.724      0.034       0.114       2.125
assists        0.8834      1.381      0.640      0.546      -2.495       4.262
rebounds      -0.4278      0.851     -0.503      0.633      -2.510       1.655
==============================================================================
Omnibus:                        2.711   Durbin-Watson:                   2.392
Prob(Omnibus):                  0.258   Jarque-Bera (JB):                0.945
Skew:                          -0.751   Prob(JB):                        0.624
Kurtosis:                       3.115   Cond. No.                         217.
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
/home/yoshi-1/.local/lib/python3.8/site-packages/scipy/stats/stats.py:1541: UserWarning: kurtosistest only valid for n>=20 ... continuing anyway, n=10
  warnings.warn("kurtosistest only valid for n>=20 ... continuing "

Durbin-Watson検定の実施

  • ※上のsummaryですでに計算済みだが、一応記載
In [4]:
from statsmodels.stats.stattools import durbin_watson

#perform Durbin-Watson test
durbin_watson(model.resid)
Out[4]:
2.3920546872335353
In [ ]: