%matplotlib inline
from __future__ import print_function
from statsmodels.compat import lzip
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm
from statsmodels.formula.api import ols
beef = pd.read_csv('beef.csv')
beef.head(10)
beef.isnull().sum()
beef_model = ols("Quantity ~ Price", data=beef).fit()
print(beef_model.summary())
OLS Regression Results
==============================================================================
Dep. Variable: Quantity R-squared: 0.901
Model: OLS Adj. R-squared: 0.900
Method: Least Squares F-statistic: 811.2
Date: Fri, 31 Aug 2018 Prob (F-statistic): 1.69e-46
Time: 23:00:04 Log-Likelihood: -77.493
No. Observations: 91 AIC: 159.0
Df Residuals: 89 BIC: 164.0
Df Model: 1
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
Intercept 30.0515 0.413 72.701 0.000 29.230 30.873
Price -0.0465 0.002 -28.482 0.000 -0.050 -0.043
==============================================================================
Omnibus: 3.453 Durbin-Watson: 1.533
Prob(Omnibus): 0.178 Jarque-Bera (JB): 2.460
Skew: 0.237 Prob(JB): 0.292
Kurtosis: 2.349 Cond. No. 1.74e+03
==============================================================================
Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 1.74e+03. This might indicate that there are
strong multicollinearity or other numerical problems.
fig = plt.figure(figsize=(12,8))
fig = sm.graphics.plot_partregress_grid(beef_model, fig=fig)
fig = plt.figure(figsize=(12, 8))
fig = sm.graphics.plot_ccpr_grid(beef_model, fig=fig)
fig = plt.figure(figsize=(12,8))
fig = sm.graphics.plot_regress_exog(beef_model, 'Price', fig=fig)
beef['Year'] = pd.to_datetime(beef['Year'], format="%Y")
beef.head()
from pandas.tseries.offsets import *
beef['Date'] = beef.apply(lambda x:(x['Year'] + BQuarterBegin(x['Quarter'])), axis=1)
beef.drop(['Year', 'Quarter'], axis=1, inplace=True)
beef.set_index('Date', inplace=True)
beef.head(10)
endog = beef['Quantity']
# To the regressors in the dataset, we add a column of ones for an intercept
exog = sm.add_constant(beef['Price'])
mod = sm.RecursiveLS(endog, exog)
res = mod.fit()
print(res.summary())
Statespace Model Results
==============================================================================
Dep. Variable: Quantity No. Observations: 91
Model: RecursiveLS Log Likelihood -84.326
Date: Fri, 31 Aug 2018 AIC 172.653
Time: 23:38:41 BIC 177.675
Sample: 03-01-1977 HQIC 174.679
- 09-01-1999
Covariance Type: nonrobust
==============================================================================
coef std err z P>|z| [0.025 0.975]
------------------------------------------------------------------------------
const 30.0515 0.413 72.700 0.000 29.241 30.862
Price -0.0465 0.002 -28.482 0.000 -0.050 -0.043
===================================================================================
Ljung-Box (Q): 422.30 Jarque-Bera (JB): 1.37
Prob(Q): 0.00 Prob(JB): 0.50
Heteroskedasticity (H): 1.35 Skew: 0.10
Prob(H) (two-sided): 0.41 Kurtosis: 2.43
===================================================================================
Warnings:
[1] Parameters and covariance matrix estimates are RLS estimates conditional on the entire sample.
res.plot_recursive_coefficient(range(mod.k_exog), alpha=None, figsize=(10,6));
fig = res.plot_cusum(figsize=(10,6));