Take US GDP and inflation data as an example:
1. Dataset
Downloading the data We need to download the US GDP and inflation data from the FRED database and store them in a CSV file. You can search and download the required data on the FRED website (https://fred.stlouisfed.org/). Here, and name them 'gdp.csv' and 'inflation.csv'.
The website is:
https://fred.stlouisfed.org/
- Type 'GDP' in the search bar and press Enter, and select 'Gross Domestic Product' from the results list.
- Type 'CPIAUCSL' in the search bar and press Enter, and select 'Consumer Price Index for All Urban Consumers: All Items in US City Average' from the results list
.
Note that the data is saved in csv format:
2. Merge datasets
Need to use pandas to read these CSV files and merge them. Here is sample code:
import pandas as pd
# 读取 GDP 和通货膨胀数据
gdp = pd.read_csv('./data/gdp.csv', index_col = 'DATE', parse_dates = True)
inflation = pd.read_csv('./data/inflation.csv', index_col = 'DATE', parse_dates = True)
# 合并数据
data = pd.concat([gdp, inflation], axis=1, join='inner')
data.columns = ['GDP', 'Inflation']
Here, we read GDP and inflation data using the pd.read_csv() method.
We store them in the gdp and inflation variables and use the pd.concat() method to combine them into one data frame.
We also use the join='inner' parameter to ensure only data from the same time period is included, and use the data.columns property to give the columns new names.
3. Model usage
Next, the VAR model object needs to be created using the VAR() method. Here is sample code:
from statsmodels.tsa.api import VAR
model = VAR(data)
Here, we use the VAR() method of the Statsmodels library to create a VAR model object. We pass the merged data to the VAR() method.
Then, we need to fit the VAR model using the fit() method. Here is sample code:
results = model.fit(maxlags=2, ic='aic')
results.summary()
Here, we fit a VAR model using the fit() method and specify a maximum lag order of 2. We also use the ic='aic' parameter to select the AIC criterion for model selection. Finally, we output the model parameters using the summary() method.
4. Forecast
Finally, we can make forecasts using the forecast() method and visualize the forecast results using the plot() method. Here is sample code:
import matplotlib.pyplot as plt
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
lag_order = results.k_ar
forecast_input = data.values[-lag_order:]
forecast = results.forecast(forecast_input, steps=12*3)
# 将预测结果可视化
forecast_index = pd.date_range(data.index[-1], periods=12*3, freq='M')
forecast_df = pd.DataFrame(forecast, index=forecast_index, columns=['GDP', 'Inflation'])
data.plot(figsize=(12, 6), legend=True)
forecast_df.plot(figsize=(12, 6), legend=True)
plt.xlabel('Time')
plt.ylabel('percent')
plt.show()
Here, we forecast GDP and inflation for the next three years (36 months) using the forecast() method. Then, we visualize the actual data and the predicted results using the plot() method.