2

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?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Feng Chen
  • 2,139
  • 4
  • 33
  • 62
  • Which function are you trying to use from `vars` package exactly? – Ronak Shah Mar 01 '20 at 03:18
  • hey, thanks for your concern. i edit my question and put an example in vars package in it, which is what i want to use in my case. please take a look. – Feng Chen Mar 01 '20 at 04:46

1 Answers1

3

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.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213