# [R] Forecast using VAR model

Dhivya Narayanasamy dhiv.shreya at gmail.com
Mon May 22 07:16:31 CEST 2017

```Hi Jon,

sorry for the inconvenience. I have done it in plain text now.

I am building a VAR model to forecast of bivariate time series. But it
shows flat forecast and i am in need of correcting  it. Is there any way to
correct this flat forecast?  or Do i have to go with other models?

Code:

> datax.ts <- ts(datax.zoo)
> v1b <- VARselect(datax.ts, lag.max = 10, type = "const")
> v1b\$selection
AIC(n)  HQ(n)  SC(n) FPE(n)
10      7      3     10
> var7 = VAR(datax.ts, p=7)
> serial.test(var7, lags.pt=10, type = "PT.asymptotic")

Portmanteau Test (asymptotic)

data:  Residuals of VAR object var7
Chi-squared = 31.991, df = 12, p-value = 0.001388

> gf1 <- forecast(var7, h = 600)
> plot(gf1, main = "var7")
> grangertest(datax.ts[,1] ~ datax.ts[,2], order = 7)
Granger causality test

Model 1: datax.ts[, 1] ~ Lags(datax.ts[, 1], 1:7) + Lags(datax.ts[, 2], 1:7)
Model 2: datax.ts[, 1] ~ Lags(datax.ts[, 1], 1:7)
Res.Df Df      F    Pr(>F)
1   9968
2   9975 -7 20.852 < 2.2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> grangertest(datax.ts[,2] ~  datax.ts[,1], order = 7)
Granger causality test

Model 1: datax.ts[, 2] ~ Lags(datax.ts[, 2], 1:7) + Lags(datax.ts[, 1], 1:7)
Model 2: datax.ts[, 2] ~ Lags(datax.ts[, 2], 1:7)
Res.Df Df      F   Pr(>F)
1   9968
2   9975 -7 3.0918 0.002948 **
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

The P value from "Portmanteau Test" is very much less than << 0.05 for
lagged value 7. Is this correct?
I have added my plot and raw data in the attachment for your further
reference. Thank you.

Regards| Mit freundlichen Grüßen,
> Dhivya Narayanasamy

Regards| Mit freundlichen Grüßen,

Dhivya Narayanasamy

Contact No: +91-8438505020

On Sun, May 21, 2017 at 5:40 PM, John C Frain <frainj at gmail.com> wrote:

> It would be much easier to see what you are doing if you reposted in plain
> text.
>
> John C Frain
> 3 Aranleigh Park
> Rathfarnham
> Dublin 14
> Ireland
> www.tcd.ie/Economics/staff/frainj/home.html
> mailto:frainj at tcd.ie
> mailto:frainj at gmail.com
>
> On 21 May 2017 at 06:05, Dhivya Narayanasamy <dhiv.shreya at gmail.com>
> wrote:
>
>> I am building a VAR model to forecast of bivariate timeseries. But it
>> shows
>> flat forecast.
>>
>> So I would like to use recursive window forecasting technique using VAR
>> model. Will it give what i expect (Avoid flat forecast) ? or should i have
>> to go with other package.
>>
>> > datax.zoo <- read.zoo(datax)> datax.ts <- ts(datax.zoo)> v1b <-
>> VARselect(datax.ts, lag.max = 10, type = "const")> v1b
>> \$selection
>> AIC(n)  HQ(n)  SC(n) FPE(n)
>>      9      7      5      9
>>
>> \$criteria
>>                   1            2            3            4
>> 5            6            7
>> AIC(n)     9.686513     9.657172     9.632444     9.625856
>> 9.621148     9.619425     9.615396
>> HQ(n)      9.688951     9.661234     9.638131     9.633167
>> 9.630085     9.629987     9.627583
>> SC(n)      9.693514     9.668839     9.648778     9.646856
>> 9.646815     9.649759     9.650397
>> FPE(n) 16099.014774 15633.507506 15251.665643 15151.510512
>> 15080.352425 15054.392389 14993.864861
>>                   8            9           10
>> AIC(n)     9.615430     9.615116     9.615990
>> HQ(n)      9.629241     9.630552     9.633051
>> SC(n)      9.655098     9.659451     9.664991
>> FPE(n) 14994.366572 14989.661383 15002.762011
>> > var7 = VAR(datax.ts, p=7)> serial.test(var7, lags.pt=10, type =
>> "PT.asymptotic")
>>
>>     Portmanteau Test (asymptotic)
>>
>> data:  Residuals of VAR object var7Chi-squared = 22.745, df = 12,
>> p-value = 0.02997
>> > grangertest(datax.ts[,1] ~ datax.ts[,2], order = 7)Granger causality
>> test
>> Model 1: datax.ts[, 1] ~ Lags(datax.ts[, 1], 1:7) + Lags(datax.ts[,
>> 2], 1:7)Model 2: datax.ts[, 1] ~ Lags(datax.ts[, 1], 1:7)
>>   Res.Df Df      F    Pr(>F)    1   5686                        2
>> 5693 -7 16.105 < 2.2e-16 ***---Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01
>> ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1> grangertest(datax.ts[,2] ~  datax.ts[,1],
>> order = 7)Granger causality test
>> Model 1: datax.ts[, 2] ~ Lags(datax.ts[, 2], 1:7) + Lags(datax.ts[,
>> 1], 1:7)Model 2: datax.ts[, 2] ~ Lags(datax.ts[, 2], 1:7)
>>   Res.Df Df      F Pr(>F)1   5686                 2   5693 -7 1.5618
>> 0.1418
>> > g <- forecast(var7, h = 600)> plot(g)
>>
>>
>> Also the 'P' value  from portmanteau test shows auto correlation is
>> present
>> is my VAR model. Here is my raw data you can find :
>> /view?usp=sharing
>>
>>
>> Thank you.
>>
>> Regards
>> > Dhivya Narayanasamy
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help