knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 8, fig.height = 5 )
library(healthyR.ts)
In this vignette we will discuss how to use the tidy_fft
function, what it does,
and what it produces.
The tidy_fft
function has only a few parameters, six to be exact. There are some
sensible defaults made. It is important that when you use this function, that you
supply it with a full time-series data set, one that has no missing data in it as
this will affect your results.
The function and its full parameters are as follows:
tidy_fft( .data, .date_col, .value_col, .frequency = 12L, .harmonics = 1L, .upsampling = 10L )
The .data
argument is the actual formatted data that will get passed to the function,
the time series data. The .date_col
argument is the column that holds the datetime
of interest. The .value
column is the column that holds the value that is being
analyzed by the function, this can be counts, averages, any type of value that is
in the time series. The .frequency
argument details the cyclical nature of the data,
is it 12 for monthly, 7 for weekly, etc. The .harmonics
argument will tell the
function how many times the fft
should be run internally and how many filters should
be made. Finally the .upsampling
argument will tell the function how much the
function should up sample the time parameter.
Let us now work through a simple example.
Lets get started with some data.
suppressPackageStartupMessages(library(dplyr)) suppressPackageStartupMessages(library(ggplot2)) suppressPackageStartupMessages(library(timetk)) data_tbl <- AirPassengers %>% ts_to_tbl() %>% select(-index)
Now that we have our sample data, let's check it out.
glimpse(data_tbl)
Lets take a look at a time series plot of the data.
suppressPackageStartupMessages(library(timetk)) data_tbl %>% plot_time_series( .date_var = date_col, .value = value )
Now that we know what our data looks like, lets go ahead and run it through the function
and assign it to a variable called output
output <- tidy_fft( .data = data_tbl, .date_col = date_col, .value_col = value, .harmonics = 8, .frequency = 12, .upsampling = 5 )
Now that we have run the function, let's take a look at the output.
The function invisibly returns a list object, hence the need to assign it to a variable. There are a total of 4 different sections of data in the list that are returned. These are:
In this section we will go over all of the data components that are returned. We
can access all of the data in the usual format output$data
, which in of itself
will return another list of objects, 7 to be specific. Lets go through them all.
The data element accessed by output$data$data
is the original data with a few
elements added to it. Let's take a look:
output$data$data %>% glimpse()
The error_data element accessed by output$data$error_data
is a tibble
that
has the original data, plus a few other elements and an error term that is the
actual value minus the harmonic output. This is done for each harmonic level.
output$data$error_data %>% glimpse()
The input_vector is just the value column that was passed to the function.
output$data$input_vector
The maximum_harmonic_tbl is a tibble
that has data regarding the maximum harmonic
entered into the function, this will be the most flexible data returned.
output$data$maximum_harmonic_tbl %>% glimpse()
The differenced_value_tbl
is a tibble
that has a lag 1 difference of the value
column supplied.
output$data$differenced_value_tbl %>% glimpse()
The dff_tbl
is a tibble
that is returned that has the fft values, the complex,
real and imaginary parts.
output$data$dff_tbl %>% glimpse()
The last data piece of the data section is the ts_obj
. This is a ts
version of
the input_vector
output$data$ts_obj
There are a total of five plots that are returned in the list. Three of them are ggplot
plots and two of them are plotly::ggplotly
plots.
The harmonic_plot
is a ggplot
plot that shows all of the harmonic waves on the same graph if you set .harmonics
greater than 1.
output$plots$harmonic_plot
The diff_plot
is a ggplot
plot of the lag 1 differenced_value_tbl
output$plots$diff_plot
The max_har_plot
is a ggplot
plot of the maximum harmonic wave entered into .harmonics
output$plots$max_har_plot
The harmonic_plotly
is a plotly::ggplotly
plot of the harmonic_plot
output$plots$harmonic_plotly
The max_har_plotly
is a plotly::ggplotly
plot of the max_har_plot
output$plots$max_har_plotly
The parameters
element is a list of input parameters and internal parameters.
output$parameters
The model
portion has four pieces to it which we will look at below.
The parameter m
is an internal parameter that is equal to .harmonics
/ 2. This is fed into TSA::harmonic
along with the ts_obj
The parameter harmonic_obj
is the object returned from TSA::harmonic
The parameter harmonic_model
is the harmonic model from the TSA::harmonic
The parameter model_summary
is a summary of the harmonic model.
output$model$m
output$model$harmonic_obj %>% head()
output$model$harmonic_model
output$model$model_summary
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.