Random Time Prediction Using R Forest

I am trying to do a time series analysis using randomforest. Pfb my code

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

Sample data:

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


####Length of training & testing set Splitting it 80-20####

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)

I get a stationary result when I use the prediction function in the test data set. All predicted values ​​are 369, I tried to use a different data set. I got the same result. Can someone tell me what I'm doing wrong here?

+4
source share
1 answer

Let me try to rephrase your question to make sure that I understand exactly what you want to do.

, . , , - . :

Date        Sales
2014-08-11  378
2014-08-18  348
2014-08-25  314
2014-09-01  324
2014-09-08  352
2014-09-15  453
...

, RandomForest , . - , y ( : ) x (). x, , . , , , .

:

1) . , , , ARIMA. . , ? , , ? , ,

2) , RandomForest . , , (, ...) . R- lubridate . :

library(lubridate)
Subsales <- mutate(Subsales, Weekday = wday(Date, label = TRUE))

, !

+5

Source: https://habr.com/ru/post/1673879/


All Articles