Converting zoo to ts before forecasting - r

I am struggling to convert a zoo objects to a ts object.
I have a huge data.frame "test" with quarterly hour data, which looks like this:
date <- c("2010-07-04 09:45:00", "2010-07-04 10:00:00", "2010-07-04 10:15:00", "2010-07-04 10:30:00", "2010-07-04 10:45:00", "2010-07-04 11:00:00")
nrv <- c("-147.241", "-609.778", "-432.289", "-340.418", "-73.96" , "-533.108")
tt <- c("3510.7", "3608.5", "3835.7", "4003.7", "4018.8", "4411.9")
test <- data.frame(date,nrv,tt)
test
I want to make some predictions (mostly ARIMA) and thought the forecastpackage would be a good idea for that.
First of I formated the data away from characters.
test$date <- strptime(test$date,format="%Y-%m-%d %H:%M")
test$nrv <- as.numeric(as.character(test$nrv))
test$tt <- as.numeric(as.character(test$tt))
str(test) #date is POSIXlt object
Since I needed to do an interpolation and construct lags, I also used the zoo package using the date variable as index, which worked great. The `zoo package was recommended to me while dealing with time series data.
library(zoo)
test.zoo <- zoo(test[,2:3],test[,1])
test.zoo #date is now the Index and and the zoo objects works nicely
But then I realized that forecasting only seems to work with ts objects. (Is that true?)
When I tried to convert the zoo object to a ts object, my time index disappeared. I think this might be due to not using a proper frequency. However I am somewhat lost as to what would be a working frequency for this dataset and with ts objects in general.
test.ts <- as.ts(test.zoo)
test.ts
How do I convert this zoo object back to a ts object I can use for forecasting?
Thanks!

The forecast package only works with ts objects as you suspected.
You can use test.ts with the forecast package. For example
plot(forecast(test.ts[,1]))

I had the same problem and solved it by using zooreg function.
step1: use zooreg to transform zoo object to non-zoo but ts alike objet
step2: use ts function to transform further to ts object

Related

R: ts object shows weekly seasonality, but not xts (with same data and frequency parameter)

I have a dataframe which captures daily data:
$dt: Date, format: "2019-01-01" "2019-01-02" "2019-01-03" "2019-01-04"
$new_user_growth: num NA -0.0254 -0.0469 -0.1257 0.3125
I converted the dataframe above to ts by:
ts_h7_2019 <- ts(data=df$new_user_growth, frequency = 7)
I set frequency to 7 because I want to focus on weekly seasonality. When I decompose the data using mstl (automatic stl algorithm), it shows Seasonal7 trend.
So far so good.
But then, I found working with xts is easier, so I created an xts object:
df_xts <- xts(x=df$new_user_growth, order.by=df$dt, frequency=7)
or alternatively, I also tried:
df_xts2 <- xts(x=df$new_user_growth, order.by=df$dt, deltat=7)
Notice that both ts object (ts_h7_2019) and xts object (df_xts, df_xts2) are derived from a same dataframe (df). However, the mstl decomposition return no seasonality and consequently, the manual stl can't be run on the xts objects with this error:
y is not a seasonal ts object
What's wrong here? both xts and ts should have exactly same seasonality as both are derived from a single dataframe.
Why does the frequency parameter works on ts but not on xts?
Have you tried using the mstsclass (taken from: https://otexts.com/fpp2/complexseasonality.html).
Potentially something like this:
forecast::mstl(msts(data = xts(df$....), seasonal.periods = 7))

Convert data into xts in R with date format YYYYMMDD

I have a time series table named ff5 imported into R using read.csv with the date column in format of "YYYYMMDD".
I installed the xts package to better handle time series data. I tried to use the below code to convert the original data ff5 to xts format:
library(xts)
ff5_xts <- xts(ff5, order.by = as.Date(ff5["date"], "%Y%m%d"))
And I got this error message:
Error in as.Date.default(x, ...) : do not know how to convert 'x'
to class “Date”
I tried a few other ways with or without xts but could not figure out how to convert this original data into time series.
Would appreciate any help!
Does this work?
ff5 <- data.frame(date=c("20180615", "20180617", "20180616"))
ff5$date <- as.Date(ff5$date, "%Y%m%d")
library(xts)
ff5_xts <- xts(ff5, order.by = ff5$date)
ff5_xts
date
2018-06-15 "2018-06-15"
2018-06-16 "2018-06-16"
2018-06-17 "2018-06-17"

Moving from zoo to xts object

I have various financial data that I am trying to merge into an xts object so I can perform multiple statistical analyses. I am having difficulty, however, with dates when moving from the original data to a zoo object to an xts object.
For instance, I read in some hedge fund return data, change the report date variable using the ymd function from the lubridate package, create a zoo object, then just as a check create a timeSeries object. All seems to be OK, but I continue to get an error when I attempt to create the xts object, as shown below:
hfIndexes$ReportDt <- ymd(hfIndexes$ReportDt)
hfIndexesZoo <- zoo(hfIndexes,order.by="ReportDt")
hfIndexesTimeSeries <- as.timeSeries(hfIndexesZoo)
hfIndexesXTS <- as.xts(hfIndexesZoo)
Error in xts(coredata(x), order.by = order.by, frequency = frequency, :
order.by requires an appropriate time-based object
What do I need to do to ensure that I have the correct time-based object to create the desired xts object?
Consider this answer: https://stackoverflow.com/a/4297342/3253015
order.by is an argument needed in xts objects. As we are dealing with timeseries, you can consider it to be one, that creates a frame of sorts, into which the data is put. So you tell as.xts that the data you want inside is spaced out by the time-based object given in order.by.

Get frequency for TS from and XTS for X12

I'm trying to automate some seasonal adjustment with the x12 package. To do this I need a ts object. However, I do not need a simple ts object, but one whose start date and frequency has been set. For any given series I could type that, but I will be feeding a mix of monthly or weekly data in. I can get the data from a quantmod as an xta object, but can't seem to figure out how to extract the frequency from the xts.
Here is some sample code that works the the whole way through, but I would like to pull the frequency info from the xts, rather than explicitly set it:
getSymbols("WILACR3URN",src="FRED", from="2000-01-01") # get data as an XTS
lax <- WILACR3URN #shorten name
laxts <- ts(lax$WILACR3URN, start=c(2000,1), frequency=12) #explicitly it works
plot.ts(laxts)
x12out <- x12(laxts,x12path="c:\\x12arima\\x12a.exe",transform="auto", automdl=TRUE)
laxadj <- as.ts(x12out$d11) # extract seasonally adjusted series
Any suggestions? Or is it not possible and I should determine/feed the frequency explicitly?
Thanks
This is untested for this specific case, but try using xts::periodicity for the frequency:
freq <- switch(periodicity(lax)$scale,
daily=365,
weekly=52,
monthly=12,
quarterly=4,
yearly=1)
And use the year and mon elements of POSIXlt objects to calculate the start year and month.
pltStart <- as.POSIXlt(start(lax))
Start <- c(pltStart$year+1900,pltStart$mon+1)
laxts <- ts(lax$WILACR3URN, start=Start, frequency=freq)
plot.ts(laxts)
The xts::periodicity suggestion was helpful to me. I've also found the following approach using xts::convertIndex works well for monthly and quarterly data. It is untested for weekly data.
require("quantmod")
require("dplyr")
getSymbols("WILACR3URN",src="FRED", from="2000-01-01") # get data as an XTS
lax <- WILACR3URN #shorten name
laxts <- lax %>%
convertIndex("yearmon") %>% # change index of xts object
as.ts(start = start(.), end = end(.)) # convert to ts
plot.ts(laxts)

How to plot a range of data with time in hh:mm:ss.000 format in R?

I have a set of data that need to be plotted (1M rows) with R. The time column (column 1) is in hh:mm:ss.000 format. I would like to plot the graph in a time range, say from 08:05:00 to 09:00:00. How do I do it? I have searched the web and couldn't find a way to set the xlim properly.
Here's a short example of the data. Column 1 is time, Column 2, 3, 4.. will be on y axis.
07:51:19.553,10.785,0.000,0.392,1.512,1.527,1.553,1.560,2.838
08:05:00.661,-1.555,0.000,0.041,0.310,0.314,0.321,0.327,1.474
08:06:58.250,30.781,0.000,0.093,0.156,0.160,0.168,0.173,1.411
08:30:02.506,-0.002,0.000,0.052,0.120,0.123,0.132,0.137,1.361
09:05:00.997,-1.802,0.000,0.032,0.078,0.080,0.087,0.090,1.258
10:05:00.661,-1.555,0.000,0.041,0.310,0.314,0.321,0.327,1.474
Thanks in advance for your help.
You really want to use a proper time series class such as zoo or xts
Subsetting, plotting, ... then come for free. Start with the excellent zoo documentation before maybe switching to xts for even better performance and subsetting.
Now, one million rows is too many as you end up with more data than pixels -- but at least this will give you a chance to summarize your data.
Here is a quick illustration:
> options(digits.sec=3) ## important: turn on milli-sec via print()
> library(xts)
Loading required package: zoo
> X <- xts(cumsum(rnorm(100)), order.by=Sys.time()+cumsum(runif(100)/10))
> plot(X)
To change character vector to "date & time" object, POSIXlt(ct) object, function strptime() will come handy. Here's a short example how it's done.
dtm <- strptime(c("1.1.2010 11:35"), format = "%d.%m.%Y %H:%M", tz = "CET")

Resources