2

When I run the following code, I do NOT get this error:

## https://www.dataiku.com/learn/guide/code/r/time_series.html

library(readxl)
library(forecast)
library(dplyr)
library(prophet)
library(rstan)
library(Hmisc)
library(caret)

data<-read_excel("Time Series/Items.xlsx", col_types = c("text", "numeric"))
Nper=0.75

stmodels=c("meanf","naive","snaive","rwf","croston","stlf","ses","holt","hw","splinef","thetaf","ets","auto.arima","tbats","prophet")

gkuniforecast = function(data, Np, Ncolumn, tsfreq, model) {
  ## Preparation
  N = ceiling(Np*nrow(data))

  ## Models
  if (model=="prophet"){
    df=data
    names(df)=c("ds","y")
    df$ds=as.Date(paste(df$ds,"-01",sep=""), "%Y-%b-%d")
    train.df = df[1:N,]
    na.df=data.frame(ds=rep(NA, N),y=rep(NA, N))
    test.df <- rbind(na.df, df[(N+1):nrow(data),])
    m <- prophet(train.df)

    future <- make_future_dataframe(m, periods = nrow(data)-N, freq = 'month')

    pro_forecast <- predict(m, future)
    plot(m, pro_forecast)
    ##prophet_plot_components(m, forecast)
    acc=matrix(rep(NA, 16),nrow=2,ncol=8,dimnames=list(c("Training set", "Test set"),c("ME","RMSE","MAE","MPE","MAPE","MASE","ACF1","Theil's U")))
    acc["Test set","RMSE"]=sqrt(mean((pro_forecast$yhat - test.df)^2, na.rm = TRUE))
  }else{
    x=pull(data,Ncolumn)
    train.x = ts(x[1:N], frequency=tsfreq)
    test.x <- ts(c(rep(NA, N), x[(N+1):NROW(x)]), frequency=tsfreq)
    str1=paste0("m_",model," = ",model,"(train.x)")
    if (Np==1) {str2=paste0("f_",model," = forecast(m_",model,", h=NROW(x)")
    } else {str2=paste0("f_",model," = forecast(m_",model,", h=NROW(x)-N)")}
    str3=paste0("plot(f_",model,")")
    str4="lines(test.x)"
    str5=paste0("acc=accuracy(f_",model,",test.x)")
    str=paste0(str1,";",str2,";",str3,";",str4,";",str5)
    eval(parse(text=str))
  }
  return(acc)
  }
acc = lapply(stmodels, gkuniforecast, data=data, Np=Nper, Ncolumn=2,tsfreq=12)

But when I run this code, I do:

##Forecast data prep
tsfreq=5
x=pull(data,1)
train.x = ts(x[1:N], frequency=tsfreq)
test.x <- ts(c(rep(NA, N), x[(N+1):NROW(x)]), frequency=tsfreq)


stmodels=c("meanf","naive","snaive","rwf","croston","stlf","ses","holt","hw"##,"splinef"
           ,"thetaf","ets","auto.arima","tbats")

for (i in 1:length(stmodels)){
str1=paste0("m_",stmodels[i]," = ",stmodels[i],"(train.x)")
str2=paste0("f_",stmodels[i]," = forecast(m_",stmodels[i],", h=NROW(x)-N)")
str3=paste0("plot(f_",stmodels[i],")")
str4="lines(test.x)"
str5=paste0('acc[["',stmodels[i],'"]]=accuracy(f_',stmodels[i],',test.x)')
str=paste0(str1,";",str2,";",str3,";",str4,";",str5)
eval(parse(text=str))
}

There seems to be a problem with 'hw' (splinef is commented out, because it gives me another error), but I do not understand why in the first dataset, I get no errors and I do with the second dataset. What is also different is the frequency.

Again the error is: Please select a longer horizon when the forecasts are first computed

  • Hi Georgious, welcome to SO. Right now your question is considered "Off-topic" and might be closed. In order to prevent that, you should include the actual data you used in your forecast models. Ideally, this is minimal (only enough to re-make the problem) and be pasted into your question as a `read.table(text = "data...")`, but it could also be links to download the files from Dropbox or something similar. Remember people are taking their time to help you, so take your time to help them. Cheers – Nate May 01 '18 at 15:13
  • and here is the [R FAQ](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for future reference – Nate May 01 '18 at 15:14

1 Answers1

3

You are mixing functions that create forecasts directly (like meanf()) with functions that generate models (like ets()). For functions that generate forecasts directly, you need to specify the forecast horizon when you call the function. See https://otexts.org/fpp2/the-forecast-package-in-r.html for a list of functions that produce forecasts directly.

Rob Hyndman
  • 30,301
  • 7
  • 73
  • 85