0

Can I use the forecast function with randomforest? PFB my code for creating regression model with randomforest

Subsales<-read.csv('Sales.csv')
head(Subsales)

Sample DataSet

Date               SKU                            City   Sales
      <date>                               <chr>   <chr> <dbl>
1 2014-08-11 Vaseline Petroleum Jelly Pure 60 ml Jeddah1   378
2 2014-08-18 Vaseline Petroleum Jelly Pure 60 ml Jeddah1   348
3 2014-08-25 Vaseline Petroleum Jelly Pure 60 ml Jeddah1   314
4 2014-09-01 Vaseline Petroleum Jelly Pure 60 ml Jeddah1   324
5 2014-09-08 Vaseline Petroleum Jelly Pure 60 ml Jeddah1   352
6 2014-09-15 Vaseline Petroleum Jelly Pure 60 ml Jeddah1   453

Code

train_len=round(nrow(SubSales)*0.8) 
test_len=nrow(SubSales)

######Splitting dataset into training and testing#####

#### Training Set
training<-slice(SubSales,1:train_len) 
#### Testing Set
testing<-slice(SubSales,train_len+1:test_len)

training=training[c(1,4)]
testing=testing[c(1,4)]

library(randomForest)
set.seed(1234)
regressor = randomForest(formula=Sales~.,
                data=training,
                ntree=100)
y_pred = predict(regressor,newdata = testing)

Can I use forecast function instead of predict?

Shivam Sarin
  • 551
  • 1
  • 7
  • 20
  • 1
    Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – zx8754 Apr 04 '17 at 06:48
  • @zx8754 How do I give a reproducible example on something I don't know even works? I just want to know I can I use Forecast function when I train a model with Random Forest. Everywhere I have seen predict function with RF just curious if it can work with forecast. – Shivam Sarin Apr 04 '17 at 11:45
  • @zx8754 I've edited the question with all the details if it wasn't clear before. Cheers. – Shivam Sarin Apr 04 '17 at 11:52
  • It is not only about reproducible example but also it is too broad. The answer to "Can I use X?" question is a simple yes/no, which is not very helpful. Why not you try first, and tell if you get stuck? By the way data is still not reproducible, try `dput(SubSales)` – zx8754 Apr 04 '17 at 11:53
  • I agree it's not a reproducible example as I don't know how it works that is why I'm asking here. I thought it's understood if somebody is explaining an Yes/No answer he would give a reason behind the Yes/No. I'll keep this in mind the next time thanks. – Shivam Sarin Apr 04 '17 at 12:08

1 Answers1

-1

Since you ask a pretty broad question, I'll give you a broad answer. You'll have to start by training the random forest model. The function to then make predictions with the trained model is predict

WHoekstra
  • 173
  • 7
  • Hi, I'm sorry if it was not clear before, I have trained a model with random forest. I have a training and test set. Everywhere I have the usage of predict function with RF. I am just curious if I can use forecast function with my random forest model. – Shivam Sarin Apr 04 '17 at 11:48
  • thanks for the clarification. The answer is no - the 'forecast' function is specific to the forecast package, and will not work with randomforest models from the randomForest package or random forest models from the caret package. Please note that ARIMA models are specifically designed to be able to handle time series data. This is not the case for random forests. So without any additional information about your use scenario, the combination of these algorithms seems strange. – WHoekstra Apr 04 '17 at 11:55
  • Thanks that explains it. :) – Shivam Sarin Apr 04 '17 at 12:08