I have an issue trying to assign a value. This looks strange because after I apply the same commands to another dataset, it works.
Below you will find some screenshots:
Dataset #1 Step 1: Load dataset 1 and use ts function.
Step 2: Create a new object, based on dataset 1 and reformat the date. (Created wrongly)
Dataset #2 Step 1: Load dataset 1 and use ts function.
Step 2: Create a new object, based on dataset 1 and reformat the date. (Created correctly)
Here is the code shown above for the correct code:
# Load required libraries
library(forecast)
library(lubridate)
library(tidyverse)
library(scales)
library(ggfortify)
# Load historical SLA definitions
bwi <- read.csv(file="C:/Users/nsoria/Documents/Data Science/SLA Prediction/TEC_BWI.csv", header=TRUE, sep=';', dec=",")
# Create time series object
ts_bwi <- ts(bwi$SLA, frequency = 12, start = c(2015,1))
############################################ STL Model Algorithm
# Pull out the seasonal, trend, and irregular components from the time series
model_stl <- stl(ts_bwi, s.window = "periodic")
############################################ ARIMA Model Algorithm
# Pull out the seasonal, trend, and irregular components from the time series
#model_arima <- auto.arima(ts_bwi)
# Predict the next 5 month of SLA (STL)
pred <- forecast(model_stl, h = 5)
# Predict the next 5 month of SLA (ARIMA)
#pred <- forecast(model_arima, h = 5)
# Convert pred from list to data frame object
df1 <- fortify(pred) %>% as_tibble()
# Convert ts decimal time to Date class
df1$Date <- as.Date(date_decimal(df1$Index), "%Y-%m-%d")
Here is the link for the dataset that is failing.
Thank you. Nicolás.
It's doing what you ask it to do.
The "wrong" df1$Index
is already a date. You don't need to convert it to anything. When you do, espicially they way you did, funny things happen.
The date_decimal
function wants a number like 2015.123
and gives you "2015-02-14 21:28:48 UTC".
If you do something crazy like:
date_decimal(as.Date("2015-01-01"))
then it first treats as.Date("2015-01-01")
as the number it actually is (16436, i.e. the number of days since 1970-01-01) and then it gives you 16436-01-01
, (i.e. exactly what you asked for.)
Your solution is to notice you already have a date, so there's nothing to convert into date.
Also, your code is giving you warnings. Don't ignore warnings.