随机森林 - 插入符 - 时间序列

Random Forest - Caret - Time Series

我有一个时间序列(苹果股票价格 - 收盘价 - 使用插入符变成一个数据框以适应随机森林。我在 1 天、2 天和 6 天滞后。我想预测接下来的 2天。提前两步预测。但是 caret 使用不允许参数 hpredict 函数作为 forecast 函数。我看到有些人试图提出论点 n.ahead 但对我不起作用。有什么建议吗?请参阅代码

df<-data.frame(APPL)
df$f1<-lag(df$APPL,1)
df$f2=lag(df$APPL,2)
df$f3=lag(df$APPL,6)

# change column names

colnames(df)<-c("price", "price_1", "price_2", "price_6")

# remove rows (days) with NA.
df<-df[complete.cases(df),]

fitControl <- trainControl(
  method = "repeatedcv",
  number = 10,
  repeats = 1,
  classProbs = FALSE,
  verboseIter = TRUE,
  preProcOptions=list(thresh = 0.95, na.remove = TRUE, verbose = TRUE))

set.seed(1234)

rf_grid= expand.grid(mtry = c(1:3))

fit <- train(price~.,
                 data=df,
                 method="rf",
                 preProcess=c("center","scale"),
                 tuneGrid = rf_grid,
                 trControl=fitControl,
                 ntree = 200,
                 metric="RMSE")


nextday <- predict(fit,`WHAT GOES HERE?`)

如果我把 predict(fit) 用作 newdata 整个数据集。我认为这是错误的。我正在考虑的另一件事是做一个循环。预测提前 1 步,因为我有 1,2 和 6 天前的数据。提前 2 步的填充预测 1 天前 "cell" 与我之前所做的预测。

目前,您无法将其他选项传递给基础预测方法。不过,有一个 proposed change 可能会启用此功能。

在您的情况下,您应该为预测函数提供一个数据框,该数据框具有用于接下来几个观察的适当预测变量。

#1:: colnames(df)<-c("price","price_1","price_2","price_6") ;; "after price6
#2:: Predict{stats} is a generic function for predictions from the results of various model fitting functions

::predict(model object , dataframe)
we have 3 cases here for dataframe ::
case 1 :: train data::on which model is fitted :: Insample prediction
case 2 :: test data::Out of sample prediction
case 3 :: forecasted  data :: forecasted values of the independent variables : we get the forecasted values of the dependent variable according to the model

The column names in case 2 & 3 should be same as column names of the train data