How to make forecast using the fpp2 package?

Viewed 62

It is the first time I am using fpp2 package to make linear forecast. I have successfully installed the package. However i am having an error when using the commands.

I have already converted the data in time series using ts command.

library(SPEI)

library(fpp2)

m<- read.delim("D:/PHD_UOM/PHD_Dissertation/PhD/PhD_R/mydata/mruspi.txt")

head(m)

y<- spi(ts(m$mru,freq=12,start=c(1971,1)), end=c(2019,12),scale =12)

y

forecast(y,12)

naive(y,12)'''

forecast(y,12) Error in is.constant(y) : 'list' object cannot be coerced to type 'double' naive(y,12) Error in x[, (1 + cs[i]):cs[i + 1]] <- xx : incorrect number of subscripts on matrix

2 Answers

The problem seems to be with the output of spi, according to the manual it outputs an object of class spi. This object is possibly not adequate to input is forecast function. You might need to use fitted component of the spi class object: y.fitted

For the documentation of SPEI package (specifically p. 6-7): SPEI Documentation

There is a newer version of fpp, called fpp3. I recommend installing fpp3 for starters:

install.package(fpp3)

There is an excellent book that demonstrates how to use fpp3, called Forecasting Principles and Practice. It can be purchased via Amazon, or viewed for free from the author online: https://otexts.com/fpp3/. I am working my way through the book on my own (not in a class), it is very clear and extremely well written. The book receives my highest recommendation, I very strongly recommend using it to learn forecasting.

I am unable to load the library spei, R returns this error:

"package ‘spei’ is not available for this version of R"

If you are able to update R and fpp, then an example of making a linear forecast would be:

library(fpp3)
library(tidyverse)
us_change %>% 
  model(TSLM(Unemployment ~ Consumption + Production + Savings + season() + trend())) %>% 
  report()

You can learn more about linear regression using fpp3 here: https://otexts.com/fpp3/regression-intro.html

Related