Feng Chen
Feng Chen

Reputation: 2253

How to use vars package in R to predict multiple time series?

I am trying to use vars package in R to analyze multiple time series data. For example:

g <- tibble(r10=c(1,2,3),r20=c(3,2,4),
            Date=as.Date(c('2020-01-01','2020-01-02','2020-01-03'),format = '%Y-%m-%d'))

there are two time series (r10, r20) in a tibble. I need to predict 2 values for them. So it it multiple time series.

I want to follow the example in vars:

data(Canada) 
var.2c <- VAR(Canada, p = 2, type = "const") 
predict(var.2c, n.ahead = 8, ci = 0.95)

However, the example 'Canada' in vars manual has such format:

enter image description here

I try to directly use my tibble in the function but it failed. It seems that the model does not work if I directly use tibble in vars package. I also tried the method from How to convert dataframe into time series?, which still does not work. So, my question is: how to convert the tibble into a format that can be used in vars package?

Upvotes: 2

Views: 242

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389235

I think the format of data that you need is :

library(vars)
#convert to dataframe
g  <- data.frame(g)
#Assign date as row names
rownames(g) <- g$Date
#Remove Date column
g$Date <- NULL
#Convert to time-series
g <- ts(g)
var.2c <- VAR(g)
predict(var.2c)

You might need to adjust some paramaters in VAR and predict function to get the desired numbers.

Upvotes: 3

Related Questions