<jupyter_start><jupyter_text>Vector Autoregressive (VAR)<jupyter_code>import statsmodels.api as sm
from statsmodels.tsa.api import VAR, DynamicVAR
mdata = sm.datasets.macrodata.load_pandas().data
dates = mdata[['year', 'quarter']].astype(int).astype(str)
quarterly = dates["year"] + "Q" + dates["quarter"]
from statsmodels.tsa.base.datetools import dates_from_str
quarterly = dates_from_str(quarterly)
mdata = mdata[['realgdp','realcons','realinv']]
mdata.index = pd.DatetimeIndex(quarterly)
data = np.log(mdata).diff().dropna()
train_df = pd.DataFrame(data={'fln_res': fln_train.residual.tolist(), 'joi_res': joi_train.residual.tolist(), 'sbr_res': sbr_train.residual.tolist()}, index=fln_train.index)
test_df = pd.DataFrame(data={'fln_res': fln_test.residual.tolist(), 'joi_res': joi_test.residual.tolist(), 'sbr_res': sbr_test.residual.tolist()}, index=fln_test.index)
model = VAR(train_df)
order = 2
results = model.fit(order)
results.summary()
results = model.fit(maxlags=24, ic='aic')
results.k_ar
order = results.k_ar
forecast = []
for i in range(len(test_df)-order) :
    forecast.append(results.forecast(test_df.values[i:i+order],1))
fcst = [item[0][0] for item in forecast]
obs = fln_test[order:].glo_avg.tolist()
fcst =  [sum(x) for x in zip(fcst, fln_test[order:].clean.tolist())]
_nrmse = normalized_rmse(obs, fcst)
print("nRMSE: ", _nrmse, "\n")<jupyter_output>nRMSE:  44.0832659227
{'fraction_non_alphanumeric': 0.17191977077363896, 'fraction_numerical': 0.012893982808022923, 'mean_word_length': 7.365269461077844, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '195482', 'license': 'MIT', 'n_tokens_mistral': 583, 'n_tokens_neox': 559, 'n_words': 89, 'path': 'notebooks/180328 - SONDA Forecasting.ipynb', 'repo_name': 'cseveriano/spatio-temporal-forecasting', 'size': '1396'}