Converting Forecast class representation of dates to actual calendar dates - r

Good day,
I am building an auto.arima forecast in R. I was able to complete the forecast successfully, however the results is not displaying the date.
Forecast result:
The Plot
Data
So if you look at the x-axis, you see here it displays the years in periods.I would like to be able to export this data with actual dates
I use
library("tseries")
library("forecast")
library("xts")
The code:
Pulsedata$date <- as.Date(Pulsedata$date,format = "%d-%b-%y")
PD_ts <- msts(Pulsedata$Call_volume, start = c(2016, 01), end = c(2018,
365), seasonal.periods=c(365))
DPD_ts <- decompose(PD_ts, "multiplicative")
AA <- auto.arima(ts(PD_ts,frequency=365),D=1)
Myforecast <- forecast(AA,h=365)
plot(Myforecast)
I have tried:
Anydate
sweep
as.date
lubridate
setDT

Related

How to convert R date() values

I've got a dateframe with a lot of dates in it that were generated by the date() command in R, resembling the first dataframe below. On my computer with this version of R, the date values are formatted like this "Thu Mar 18 11:15:23 2021" - I believe this is all base R stuff.
I want to strip the weekday, the hours, minutes, and seconds away, and then transform it so that it looks like this "2021-03-18". My goal dataframe is the second dataframe below. I've tried various as.Date() or strftime functions to no avail.
df <- data.frame(date=c(date(),date()),value = c(1,2))
df <- data.frame(date =c("2021-03-18","2021-03-18"), value = c(1,2))
If you don't need strings, you can skip the strftime call and only use as.Date
df <- data.frame(
date=c(date(),date()),
value = c(1,2),
stringsAsFactors = FALSE
)
df$date <- strftime(as.Date(df$date, "%c"), "%Y-%m-%d")
https://stat.ethz.ch/R-manual/R-patched/library/base/html/strptime.html

Convert data frame to time series R

I have the following data
data_sample
date Sum
1 Feb 2015 3322.01
2 Mar 2015 6652.77
3 Apr 2015 3311.12
etc
I need to convert to time series for forecasting
> data <- xts(data_sample[,-1], order.by=as.Date(data_sample[,1], "%Y %m"))
Error in 1 - frac : non-numeric argument to binary operator
> data <- xts(data_sample[,-1], order.by=as.Date(data_sample[,1], "%m %Y"))
Error in 1 - frac : non-numeric argument to binary operator
> ts_ts(ts_long(data_sample))
Error in guess_time(x) :
No [time] column detected. To be explict, name time column as 'time'.
If you want to use as.Date(), you have to specify full dates.
Simply add 01 at the end of each entry.
date <- c("Feb 2015", "Mar 2015", "Apr 2015")
date <- as.Date(paste(date, "01"), format="%b %Y %d")
You can convert them back as follows,
format(date, "%b %Y")
or use as.yearmon from zoo library,
library("zoo")
as.yearmon(date)
Some examples here: Converting Date formats in R
R has multiple ways of representing time series. Since you are working with only Date and Sum, I have created a sample time series for you. I choose random dates and numbers.
Call for Packages
library(xts)
Create a Data Frame
data_sample <- data.frame(
date = as.Date(c("2012-01-01","2013-01-01","2014-01-01", )),
sum1 = c(3322.01, 6652.77, 3311.12))
head(data_sample)
Convert the date as in a format which R understands.
rdate<- as.Date(data_sample$date, "%m/%d/%y")
fix(rdate)
Plot the graph
plot(data_sample$sum1~rdate,type="l",col="red")
Execution of above code will gives below output.
Assuming data_sample is as shown reproducibly in the Note at the end, convert to a time series of class zoo using read.zoo and then either use it in that form or convert it to some other class such as xts or ts using the appropriate as.* function. Here we used yearmon class to represent the index as that directly represents year and month without day. This class will be used in zoo and xts and when converting to ts it will be converted appropriately.
library(xts) # this also loads zoo
z <- read.zoo(data_sample, FUN = as.yearmon, format = "%b %Y")
as.xts(z)
as.ts(z)
Date
It is also possible to use Date class for the index in zoo and xts but that does not work well with ts class. Using Date class implies that the distance between consecutive points varies according to the number of days per month as opposed to being a regularly spaced series so using Date for monthly data is normally not useful for forecasting.
zd <- aggregate(z, as.Date, c)
xd <- as.xts(zd)
Note
Input in reproducible form
Lines <- "date,Sum
1,Feb 2015,3322.01
2,Mar 2015,6652.77
3,Apr 2015,3311.12 "
data_sample <- read.csv(text = Lines)
air1 <- type.convert(.preformat.ts(AirPassengers))
airpassengers <- as.data.frame(air1)
View(airpassengers)
class(airpassengers)
[1] "data.frame"
It converts time series data to dataframe.

How to convert stock data from xts object to ts object

I used quantmod to download stock data from yahoo finance. Here msft is a xts object.
library(quantmod)
library(forecast)
library(xts)
library(zoo)
start <- as.Date('2018-01-01')
end <- as.Date('2018-08-14')
getSymbols('MSFT', src='yahoo', from=start, to=end)
msft <- MSFT[, 'MSFT.Adjusted']
I'm trying to convert xts object to ts object. Below is what I did. My result is kind of weird. What frequency should I put in this case? The stock data are daily data (weekdays only). Thanks a lot for help.
ts(msft, start=c(2018,1,1), frequency = 365)
You can use as.timeSeries.xts from the xts library.
msft <- as.timeSeries.xts(MSFT)
str(as.timeSeries.xts(msft))
plot(msft)
I hope this will help
msft1<- as.data.frame(msft) #converting it to Data frame
rownames(msft1) <- NULL #Nullify all the rownames
timeseries<-ts(msft1, start=c(2018,1,1), frequency = 365) #convert it into a ts object
plot(timeseries) ## Plot to verify the time series
you can select weekdays from the below code
# install.packages('timeDate')
require(timeDate)
# A ’timeDate’ Sequence
tS <- timeSequence(as.Date("1991/1/4"), as.Date("2010/3/1"))
tS
# Subset weekdays
tW <- tS[isWeekday(tS)]; tW
dayOfWeek(tW)

Creating time series in R

I have a CSV file containing data as follows-
date, group, integer_value
The date starts from 01-January-2013 to 31-October-2015 for the 20 groups contained in the data.
I want to create a time series for the 20 different groups. But the dates are not continuous and have sporadic gaps in it, hence-
group4series <- ts(group4, frequency = 365.25, start = c(2013,1,1))
works from programming point of view but is not correct due to gaps in data.
How can I use the 'date' column of the data to create the time series instead of the usual 'frequency' parameter of 'ts()' function?
Thanks!
You could use zoo::zoo instead of ts.
Since you don't provide sample data, let's generate daily data, and remove some days to introduce "gaps".
set.seed(2018)
dates <- seq(as.Date("2015/12/01"), as.Date("2016/07/01"), by = "1 day")
dates <- dates[sample(length(dates), 100)]
We construct a sample data.frame
df <- data.frame(
dates = dates,
val = cumsum(runif(length(dates))))
To turn df into a zoo timeseries, you can do the following
library(zoo)
ts <- with(df, zoo(val, dates))
Let's plot the timeseries
plot.zoo(ts)

R forecasting - issue with dates in prediction

I'm doing a forecast in R using the forecast package.
I have a time series with daily data (download the .CSV here):
library(forecast)
data <- read.csv('daily_electricity.csv')
time_series <- ts(data$value, start=c(2007,1,1), frequency=365.25)
fit <- stlf(time_series) # uses STL decomposition
plot(fit)
forecast(fit, h=365)
But when I issue the last forecast command to get predictions for the next 365 days, the output not only skips days - but the values aren't in regular date format:
2012.687 2480489
2012.689 2411931
2012.692 2582997
2012.695 2190245
2012.697 2603242
2012.700 2413211
How can I get forecasts for the next 365 days, with each value formatted with the correct date, with no missing days?
The first thing you have to do is convert the dates (which are brought in as row numbers) to an actual column.
setDT(fit, keep.rownames = TRUE)[]
Then you can use decimal_date() to convert to proper format:
fit$dates <- as.Date(date_decimal(as.numeric(fit$rn)))

Resources