-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_VAR_model.py
160 lines (76 loc) · 2.32 KB
/
template_VAR_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# coding: utf-8
# In[ ]:
# A Multivariate Time Series Guide to Forecasting and Modeling
# In[68]:
#import required packages
import pandas as pd
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
from math import sqrt
from sklearn.metrics import mean_squared_error
import warnings
warnings.filterwarnings("ignore")
# In[58]:
#read the data
df = pd.read_csv("AirQualityUCI2.csv")
#check the dtypes
df.dtypes
# In[59]:
df.head()
# In[60]:
# convert index to have datetime
df['Date_Time'] = df.Date + ' ' + df.Time
df.head()
# In[61]:
df['Date_Time'] = pd.to_datetime(df.Date_Time , format = '%m/%d/%Y %H:%M:%S')
# In[62]:
data = df.drop(['Date', 'Time', 'Date_Time'], axis=1)
data.index = df.Date_Time
# In[63]:
data.head()
# In[64]:
data.columns
# In[65]:
data = data.drop(['Unnamed: 15', 'Unnamed: 16'], axis = 1)
# In[66]:
data = data.dropna()
# In[80]:
# plot the data
data.plot(subplots = True, figsize = (12, 16))
# In[67]:
#checking stationarity
from statsmodels.tsa.vector_ar.vecm import coint_johansen
#since the test works for only 12 variables, I have randomly dropped
#in the next iteration, I would drop another and check the eigenvalues
johan_test_temp = data.drop([ 'CO(GT)'], axis=1)
coint_johansen(johan_test_temp,-1,1).eig
# In[69]:
#creating the train and validation set
train = data[:int(0.8*(len(data)))]
valid = data[int(0.8*(len(data))):]
#fit the model
from statsmodels.tsa.vector_ar.var_model import VAR
model = VAR(endog=train)
model_fit = model.fit()
# In[70]:
train.head()
# In[71]:
# make prediction on validation
prediction = model_fit.forecast(model_fit.y, steps=len(valid))
# In[72]:
#converting predictions to dataframe
pred = pd.DataFrame(index=range(0,len(prediction)),columns=[cols])
for j in range(0,13):
for i in range(0, len(prediction)):
pred.iloc[i][j] = prediction[i][j]
#check rmse
for i in cols:
print('rmse value for', i, 'is : ', sqrt(mean_squared_error(pred[i], valid[i])))
# In[73]:
#make final predictions
model = VAR(endog=data)
model_fit = model.fit()
yhat = model_fit.forecast(model_fit.y, steps=1)
print(yhat)
# In[45]:
# https://www.analyticsvidhya.com/blog/2018/09/multivariate-time-series-guide-forecasting-modeling-python-codes/?utm_source=DataCamp.com&utm_medium=Community&utm_campaign=News