Forecasting monthly data using random forest

I use this blog post(https://www.r-bloggers.com/time-series-forecasting-with-random-forest)coding to predict my data

library("tidyverse")
library("tsibble")
library("randomForest")
library("forecast")
library("ggplot2")
library("readxl")

forecasts_rf <- numeric(horizon)

for (i in 1:horizon){
  set.seed(2019)
  
  fit_rf <- randomForest(X_train, y_train)
 
  forecasts_rf[i] <- predict(fit_rf, X_test)
  y_train <- y_train[-1] 
  
  X_train <- X_train[-nrow(X_train), ] 
}

forecasts_rf
exp_term <- exp(cumsum(forecasts_rf)

# calculate the final predictions
backtransformed_forecasts <- last_observation * exp_term
backtransformed_forecasts

# convert to ts format
y_pred <- ts(
  backtransformed_forecasts,
  start = c(2016, 1),
  frequency = 12
)
y_pred
# add the forecasts to the original tibble
tax_tbl <- tax_tbl %>% 
  mutate(Forecast = c(rep(NA, length(tax_ts_org)), y_pred))
tax_tbl
# visualize the forecasts
plot_fc <- tax_tbl %>% 
  ggplot(aes(x = Date)) +
  geom_line(aes(y = water_level)) +
  geom_line(aes(y = Forecast), color = "blue") +
  theme_minimal() +
  labs(
    title = "Forecast of the Water level for the Year 2016",
    x = "Year",
    y = "water_level"
  )

plot_fc
accuracy(y_pred, y_test)

But my forecasting is going wrong with a huge error and give a message "Don't know how to automatically pick scale for object of type ts. Defaulting to continuous." Please any body can tell me what's the problem in my coding. Thanks in advance.

Hi, and welcome!

Please see the FAQ: What's a reproducible example (`reprex`) and how do I do one? Using a reprex, complete with representative data will attract quicker and more answers.

I can only tell you that the message comes from ggplot.

Thanks technocrat.But my forecasting has also a high error like 10 for MAPE. What should i do now??

Post a reprex is something that will help. (For one thing, it will show you immediately that the argument to library() isn't "quoted", just

library(tidyverse)
``

Critically, different data, different results, and `horizon` is missing.

There's nothing wrong with "quoting" library() calls. Not quoting them is an R shortcut.

2 Likes

I've reformatted your post so the code is easier to read. I hope you don't mind.

Using proper code formatting makes the site easier to read, prevents confusion (unformatted code can get garbled by the forum software :anguished:), and is generally considered the polite thing to do. Check out this FAQ to find out how — it's as easy as the click of a button! :grinning::

I agree with technocrat that we'll need a reprex to help much. Try to craft one that highlights these:

  • What is the exact problem? You say "error", but do you mean the error values in the forecasting model or R printing an error message?
  • What part is causing the problem? If it's the modeling, then the plotting code isn't needed.
  • What do you expect the result should be? You'll rarely get anyone here who's also an expert in your subject and understands your analysis goals, so you need to tell us what's "right".

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.