1
$\begingroup$
    #read data
    offered_contact <- read.csv(file="si_act_claim_off_act.csv", header=TRUE)
    offered_contact_ts <- ts(offered_contact, freq=365.25/7,  start=decimal_date(ymd("2016-1-4")))

    sold_item <- offered_contact_ts[, 2:2]
    offered_contacts <- offered_contact_ts[, 4:4]

    plot.ts(offered_contact_ts[,2:4])
    summary(offered_contact_ts[,2:4])

    #Run Augmented Dickey-Fuller & kpss tests to determine stationarity and differences to achieve stationarity.
    ndiffs(sold_item, alpha = 0.05, test = c("kpss"))
    adf.test(sold_item,k=12)
    ndiffs(offered_contacts, alpha = 0.05, test = c("kpss"))
    adf.test(offered_contacts,k=12)

    #Difference to achieve stationarity
    sold_item_diff = diff(sold_item, differences = 1)
    offered_contact_diff = diff(offered_contacts, differences = 1)



    #combine the data
    offered_contact_ts_diff = cbind(sold_item_diff, offered_contact_diff)
    plot.ts(offered_contact_ts_diff)

    #Lag optimisation
    VARselect(offered_contact_ts_diff, lag.max = 12, type = "both")

    #Vector autoregression with lags set according to results of lag optimisation 
    var1 <- VAR(offered_contact_ts_diff,p=1)
    serial.test(var1,lags.pt =10,type="PT.asymptotic")

    #Vector autoregression with lags set according to results of lag optimisation
    var2 <- VAR(offered_contact_ts_diff,p=2)
    serial.test(var2,lags.pt =10,type="PT.asymptotic")

    #Vector autoregression with lags set according to results of lag optimisation
    var3 <- VAR(offered_contact_ts_diff,p=3)
    serial.test(var3,lags.pt =10,type="PT.asymptotic")

    #Vector autoregression with lags set according to results of lag optimisation
    var4 <- VAR(offered_contact_ts_diff,p=4)
    serial.test(var4,lags.pt =10,type="PT.asymptotic")

    #Vector autoregression with lags set according to results of lag optimisation
    var5 <- VAR(offered_contact_ts_diff,p=5)
    serial.test(var5,lags.pt =10,type="PT.asymptotic")

    #Vector autoregression with lags set according to results of lag optimisation
    var6 <- VAR(offered_contact_ts[,2:4],p=6)
    serial.test(var6,lags.pt =10,type="PT.asymptotic")

    #Vector autoregression with lags set according to results of lag optimisation
    var7 <- VAR(offered_contact_ts_diff,p=7)
    serial.test(var7,lags.pt =10,type="PT.asymptotic")


    #at p=7, I get p-value as 0.1253
    #Portmanteau Test (asymptotic)

    #data:  Residuals of VAR object var7
    #Chi-squared = 17.695, df = 12, p-value = 0.1253

    #ARCH test (Autoregressive conditional heteroscedasdicity)
    arch.test(var7, lags.multi =10)
    summary(var7)

    grangertest(sold_item_diff ~ offered_contact_diff, order = 1)
    grangertest(offered_contact_diff ~ sold_item_diff , order = 1)

    prd <- predict(var7, n.ahead = 35, ci = 0.95, dumvar = NULL)
    print(prd)


  • I want to convert this forecast which I have got from the single differenced data. How do I do it?
forecast_si <- apply(rbind(sold_item_actual_prev,forecast_diff_sold_item),2,cumsum)
  • How do I include external factors like holidays or disruptive events in VAR?

  • How do I add back the seasonality component?

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.