ts_time_event_analysis_tbl {healthyR.ts} | R Documentation |
Event Analysis
Description
Given a tibble/data.frame, you can get information on what happens before, after,
or in both directions of some given event, where the event is defined by some
percentage increase/decrease in values from time t
to t+1
Usage
ts_time_event_analysis_tbl(
.data,
.date_col,
.value_col,
.percent_change = 0.05,
.horizon = 12,
.precision = 2,
.direction = "forward",
.filter_non_event_groups = TRUE
)
Arguments
.data |
The date.frame/tibble that holds the data. |
.date_col |
The column with the date value. |
.value_col |
The column with the value you are measuring. |
.percent_change |
This defaults to 0.05 which is a 5% increase in the
|
.horizon |
How far do you want to look back or ahead. |
.precision |
The default is 2 which means it rounds the lagged 1 value percent change to 2 decimal points. You may want more for more finely tuned results, this will result in fewer groupings. |
.direction |
The default is |
.filter_non_event_groups |
The default is TRUE, this drops groupings with no events on the rare occasion it does occur. |
Details
This takes in a data.frame
/tibble
of a time series. It requires a date column,
and a value column. You can convert a ts
/xts
/zoo
/mts
object into a tibble by
using the ts_to_tbl()
function.
You will provide the function with a percentage change in the form of -1 to 1
inclusive. You then provide a time horizon in which you want to see. For example
you may want to see what happens to AirPassengers
after a 0.1 percent increase
in volume.
The next most important thing to supply is the direction. Do you want to see what typically happens after such an event, what leads up to such an event, or both.
Value
A tibble.
Author(s)
Steven P. Sanderson II, MPH
See Also
Other Time_Filtering:
ts_compare_data()
Examples
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(ggplot2))
df_tbl <- ts_to_tbl(AirPassengers) %>% select(-index)
tst <- ts_time_event_analysis_tbl(df_tbl, date_col, value, .direction = "both",
.horizon = 6)
glimpse(tst)
tst %>%
ggplot(aes(x = x, y = mean_event_change)) +
geom_line() +
geom_line(aes(y = event_change_ci_high), color = "blue", linetype = "dashed") +
geom_line(aes(y = event_change_ci_low), color = "blue", linetype = "dashed") +
geom_vline(xintercept = 7, color = "red", linetype = "dashed") +
theme_minimal() +
labs(
title = "'AirPassengers' Event Analysis at 5% Increase",
subtitle = "Vertical Red line is normalized event epoch - Direction: Both",
x = "",
y = "Mean Event Change"
)