Search code examples
rforecastingpredictionkalman-filter

Model is not generated from ARFIMA in R


I want to forecast ARFIMA with Kalman filter and not able to fit the arfima model into the Kalmanforecast.

 library(base)
 library(stats)
 library(parallel)
 library(forecast)
 sink(file='/home/nero/KF_arfima.log')
 f=COST$COST
 x=logb(p,10)
 # Start the clock!
 ptm <- proc.time()
 p=arfima(x[1:50], drange=c(0, 0.5),estim=c("mle"))
 pr <- KalmanForecast(2, p$model)
 y=x[51:52] 
 yhat=pr$pred #predicted value
 map=mean(abs((y - yhat)/y)) #MAPE
 proc.time() - ptm
 print(map)

I am getting the error

Error in KalmanForecast(2, p$model) : invalid argument type"

I also check and found that there is no object called model. I lost three days to solve it. I tried with various R packages, but none of them has solved it. Please let me know how to fix it.

Data Sample:

Timestamp,COST
2015-09-21T00:00:00+00:00,6
2015-09-21T00:06:00+00:00,7
2015-09-21T00:12:00+00:00,7
2015-09-21T00:18:00+00:00,7
2015-09-21T00:24:00+00:00,7
2015-09-21T00:30:00+00:00,7
2015-09-21T00:36:00+00:00,7
2015-09-21T00:42:00+00:00,6
2015-09-21T00:48:00+00:00,7
2015-09-21T00:54:00+00:00,6
2015-09-21T01:00:00+00:00,6
2015-09-21T01:06:00+00:00,7
2015-09-21T01:12:00+00:00,7
2015-09-21T01:18:00+00:00,7
2015-09-21T01:24:00+00:00,7
2015-09-21T01:30:00+00:00,7
2015-09-21T01:36:00+00:00,7
2015-09-21T01:42:00+00:00,6
2015-09-21T01:48:00+00:00,7
2015-09-21T01:54:00+00:00,6
2015-09-21T02:00:00+00:00,6
2015-09-21T02:06:00+00:00,8
2015-09-21T02:12:00+00:00,8
2015-09-21T02:18:00+00:00,7
2015-09-21T02:24:00+00:00,8
2015-09-21T02:30:00+00:00,7
2015-09-21T02:36:00+00:00,7
2015-09-21T02:42:00+00:00,7
2015-09-21T02:48:00+00:00,8
2015-09-21T02:54:00+00:00,7
2015-09-21T03:00:00+00:00,6
2015-09-21T03:06:00+00:00,7
2015-09-21T03:12:00+00:00,7
2015-09-21T03:18:00+00:00,7
2015-09-21T03:24:00+00:00,7
2015-09-21T03:30:00+00:00,7
2015-09-21T03:36:00+00:00,7
2015-09-21T03:42:00+00:00,7
2015-09-21T03:48:00+00:00,6
2015-09-21T03:54:00+00:00,6
2015-09-21T04:00:00+00:00,6
2015-09-21T04:06:00+00:00,7
2015-09-21T04:12:00+00:00,7
2015-09-21T04:18:00+00:00,7
2015-09-21T04:24:00+00:00,7
2015-09-21T04:30:00+00:00,6
2015-09-21T04:36:00+00:00,6
2015-09-21T04:42:00+00:00,6
2015-09-21T04:48:00+00:00,6
2015-09-21T04:54:00+00:00,7
2015-09-21T05:00:00+00:00,6
2015-09-21T05:06:00+00:00,7
2015-09-21T05:12:00+00:00,7

Solution

  • The help file for KalmanForecast clearly describes what sort of model is required. The arfima function does not produce output of the required kind.

    Rather than use KalmanForecast, you can use the forecast function from the forecast package to produce the forecasts. It also uses a Kalman filter to compute the forecasts.

    If you really want to use the KalmanForecast to do the work, you will have to figure out how to create the mod argument yourself.