0

I can't extract the durbin-watson as a value on it's own from the statsmodel.api, or find anywhere any documentation to help (i found alot of documentation on it's parent library, but i couldn't decode any of it).

The value is being calculated and can be seen by doing the following model summary (i've been following the guidance here: https://www.statology.org/durbin-watson-test-python/)

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())

however i just can't pull out that one figure. Any ideas?

Also - it looks like DW works on a confidence interval. Can you 'set' the model to work on say 95% confidence? I essentially want to perform the test multiple times and if the DW figure is in the 95% CI, return a yes or no to continue the program.

Thanks

1 Answer 1

2

You need to use the durbin_watson function directly.

from statsmodels.formula.api import ols
from statsmodels.stats.stattools import durbin_watson
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.standard_normal((500,4)))
df.columns = ["rating", "points", "assists", "rebounds"]


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

dw = durbin_watson(res.resid)
print(f"Durbin-Watson: {dw}")

which produces

Durbin-Watson: 1.9818102986170278

Critical values for DW statistics are not available in statsmodels. The Ljung-Box test for serial correlation is a more general approach that has critical values available.

from statsmodels.stats.diagnostic import acorr_ljungbox
lb = acorr_ljungbox(res.resid)
print(lb)

which gives

     lb_stat  lb_pvalue
1   0.003400   0.953500
2   0.774305   0.678988
3   1.412020   0.702720
4   1.890551   0.755881
5   2.176684   0.824197
6   2.397583   0.879749
7   3.186928   0.867188
8   3.639602   0.888089
9   3.793818   0.924451
10  5.639786   0.844565

The left column is the test statistic for no serial correlation and the right is the p-value.

Sign up to request clarification or add additional context in comments.

1 Comment

exactly what I need - thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.