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.