%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())
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())
res.plot_recursive_coefficient(range(mod.k_exog), alpha=None, figsize=(10,6));
fig = res.plot_cusum(figsize=(10,6));