Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I used the Thanksgiving break to push a new update of the TSstudio package to CRAN (version 0.1.3). The new version includes an update for the ts_backtesting
function along with two new function – ts_to_prophet
for converting time series objects to a prophet input format (i.e., ds
and y
columns), and ccf_plot
for lags plot between two time series. The package can be installed from either CRAN or Github:
# CRAN install.packages("TSstudio") # Github # install.packages("devtools") devtools::install_github("RamiKrispin/TSstudio") library(TSstudio) packageVersion("TSstudio") ## [1] '0.1.3'
Converting time series object to a prophet format
The ts_to_prophet
function converting ts
, xts
and zoo
objects into prophet input format (i.e., data frame with two columns – ds for date and y for the series values). For instance, convertig the USgas
series to a prophet object:
data("USgas") ts_info(USgas) ## The USgas series is a ts object with 1 variable and 225 observations ## Frequency: 12 ## Start time: 2000 1 ## End time: 2018 9 USgas_prophet <- ts_to_prophet(USgas) head(USgas) ## [1] 2510.5 2330.7 2050.6 1783.3 1632.9 1513.1 head(USgas_prophet) ## ds y ## 1 2000-01-01 2510.5 ## 2 2000-02-01 2330.7 ## 3 2000-03-01 2050.6 ## 4 2000-04-01 1783.3 ## 5 2000-05-01 1632.9 ## 6 2000-06-01 1513.1
In the case of a ts
object, where the index is not a date object, the function extracts the time component from the first observation and use it along with the frequency of the series to estimate the date column of the prophet data frame. For instance, in the case of a monthly series, where the time object provides only the year and the month, by default the day component of the date object will be set to 1. Alternatively, if known, you can set the date of the first observation with the start
argument. For example, if the USgas
series is being captured during the mid of the month (or every 15th of the month):
USgas_prophet <- ts_to_prophet(USgas, start = as.Date("2000-01-15")) head(USgas_prophet) ## ds y ## 1 2000-01-15 2510.5 ## 2 2000-02-15 2330.7 ## 3 2000-03-15 2050.6 ## 4 2000-04-15 1783.3 ## 5 2000-05-15 1632.9 ## 6 2000-06-15 1513.1
Similarly, the function can handle xts
and zoo
objects:
data("EURO_Brent") ts_info(EURO_Brent) ## The EURO_Brent series is a zoo object with 1 variable and 378 observations ## Frequency: monthly ## Start time: May 1987 ## End time: Oct 2018 head(EURO_Brent) ## May 1987 Jun 1987 Jul 1987 Aug 1987 Sep 1987 Oct 1987 ## 18.58 18.86 19.86 18.98 18.31 18.76 ts_to_prophet(EURO_Brent) %>% head() ## ds y ## 1 1987-05-01 18.58 ## 2 1987-06-01 18.86 ## 3 1987-07-01 19.86 ## 4 1987-08-01 18.98 ## 5 1987-09-01 18.31 ## 6 1987-10-01 18.76
Lags plots of two series
The second function, ccf_plot
, provides an interactive and intuitive visualization of the cross-correlation between two time series, by plotting a series against another series (and its lags) and calculating the correlation between the two with the ccf
function. For instance, let’s use the function to plot the relationship between the unemployment rate and the total vehicle sales in the US:
data("USUnRate") ts_info(USUnRate) ## The USUnRate series is a ts object with 1 variable and 850 observations ## Frequency: 12 ## Start time: 1948 1 ## End time: 2018 10 data("USVSales") ts_info(USVSales) ## The USVSales series is a ts object with 1 variable and 514 observations ## Frequency: 12 ## Start time: 1976 1 ## End time: 2018 10 ccf_plot(x = USVSales, y = USUnRate)
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.