#####NOW CASTING###################
GDP: https://www.imf.org/external/pubs/ft/wp/2016/wp1656.pdf
CPI: https://www.ecb.europa.eu/pub/pdf/scpwps/ecbwp1324.pdf
R: https://journal.r-project.org/archive/2019/RJ-2019-020/RJ-2019-020.pdf
#####Traditional Statistical Model ########
https://www.r-econometrics.com/timeseries/bvar/
http://journaljemt.com/index.php/JEMT/article/view/11709
http://dl.ueb.edu.vn/bitstream/1247/4911/1/8.%20BVAR-Article.pdf
https://www.kthohr.com/bmr/BMR.pdf
---
title: "Vector Autoregression Model"
author: "Phu"
date: "9/10/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
#Call essential libraries
library(lubridate)
library(data.table)
library(dplyr)
library(ggplot2)
library(patchwork)
library(car)
library(stringr)
library(vars)
```
## Vector Autoregression (VAR)
rm(list = ls())
Vector Autoregression (VAR) is a forecasting algorithm that can be used when two or more time series influence each other. That is, the relationship between the time series involved is bi-directional.
Các bước xây dựng model:\
1.Analyze the time series characteristics\
2.Test for causation amongst the time series\
3.Test for stationarity\
4.Transform the series to make it stationary, if needed\
5.Find optimal order (p)\
6.Prepare training and test datasets\
7.Train the model\
8.Roll back the transformations, if any\
9.Evaluate the model using test set\
10.Forecast to future\
```{r cars}
#Đọc dữ liệu
eco_data <- fread("G:/GDP_FORECAST/Vietnam/data_model.csv")
#Kiểm tra dữ liệu
eco_data %>% head()
#Tạo biến thời gian
dates <- seq(as.Date("2000-01-01"), as.Date("2020-06-01"), "3 months")
eco_data <- cbind(eco_data, dates)
#Vẽ biểu đồ
ggplot(eco_data, aes(x=dates, y = Interest)) +
geom_line() +
labs(x = "Ngay", y = "Tong tieu dung",
title = "Tong tieu dung hang ngay")
par(mfrow=c(1,1))
p1 <- ggplot(eco_data, aes(x=dates, y = Interest_rate)) +
geom_line() +
labs(x = "Time", y = " % Interest_rate",
title = "Central Bank Rate")
p2 <- ggplot(eco_data, aes(x=dates, y = World_oil_price)) +
geom_line() +
labs(x = "Time", y = "Log World_oil_price (Crude WTI)",
title = "World Oil Price")
p3 <- ggplot(Spending_df, aes(x=dates, y = SMS)) +
geom_line() +
labs(x = "Ngay", y = "Tong tieu dung SMS",
title = "Tong tieu dung SMS hang ngay")
p4 <- ggplot(Spending_df, aes(x=dates, y = THOAI)) +
geom_line() +
labs(x = "Ngay", y = "Tong tieu dung THOAI",
title = "Tong tieu dung THOAI hang ngay")
p1 + p2
#Kiểm định tính dừng
###Augmented Dicker Fuller###
ur.df(eco_data$Exchange_rate, type = "none") %>%
summary()
#Lag1
ur.df(diff(eco_data$, differences = 1), type = "none") %>%
summary()
###Tất cả dừng tại sai phân bậc 1
eco_gdp_dif <- diff(eco_data$GDP, differences = 1)
eco_CPI_dif<- diff(eco_data$CPI, differences = 1)
eco_M2_dif <- diff(eco_data$M2, differences = 1)
eco_Exchange_rate_dif <- diff(eco_data$Exchange_rate, differences = 1)
eco_Interest_rate_dif <- diff(eco_data$Interest_rate, differences = 1)
eco_World_oil_price_dif <- diff(eco_data$World_oil_price, differences = 1)
dif_Spending <- data.frame(Spending_df$DATA, Spending_df$VAS, Spending_df$SMS, Spending_df$THOAI)
dif_Spending %>% head()
dif_Spending$date <- Spending_df$dates[1:609]
#Visualize
dif_Spending %>% ggplot(aes(date, dif_VAS)) +geom_line()
#Estimate VAR
##Make a time series
eco_df <- cbind(eco_gdp_dif, eco_CPI_dif, eco_Exchange_rate_dif, eco_M2_dif, eco_Interest_rate_dif, eco_World_oil_price_dif)
eco_df %>% glimpse()
VARselect(eco_df, lag.max = 10, type = "none")
p1ct <- VAR(eco_df, p = 1, type = "const", season = 4)
p1ct %>% summary()
plot(p1ct, names = "eco_gdp_dif", lag.acf = 8, lag.pacf = 8)
causality(p1ct, cause = "eco_gdp_dif")$Granger
varp.f <- predict(p1ct, n.ahead = 5, ci = 0.95)
varp.f %>% head()
varp.f$fcst
plot(varp.f, names = "eco_gdp_dif")
No comments:
Post a Comment