在r中的预测包中首次计算预测时,请选择更长的范围

时间:2018-05-01 14:07:56

标签: r forecasting

当我运行以下代码时,我不会收到此错误:

df.fillna(0).eq(0).sum()
Out[8]: 
a    2
b    3
c    5
dtype: int64

但是当我运行这段代码时,我会这样做:

## 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)

'hw'似乎有问题(splinef被注释掉了,因为它给了我另一个错误),但是我不明白为什么在第一个数据集中,我没有错误,我使用第二个数据集。另外一个不同的是频率。

错误再次出现:  请在首次计算预测时选择更长的范围

1 个答案:

答案 0 :(得分:1)

您正在混合使用生成模型的函数(如meanf())直接创建预测的函数(如ets())。对于直接生成预测的函数,您需要在调用函数时指定预测范围。有关直接生成预测的函数列表,请参阅https://otexts.org/fpp2/the-forecast-package-in-r.html