Skip to content

Commit

Permalink
Fixes #404
Browse files Browse the repository at this point in the history
spsanderson committed Jan 27, 2023
1 parent 4ea84b8 commit 5aac3d6
Showing 7 changed files with 160 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -54,6 +54,7 @@ export(ts_auto_theta)
export(ts_auto_xgboost)
export(ts_brownian_motion)
export(ts_brownian_motion_augment)
export(ts_brownian_motion_plot)
export(ts_calendar_heatmap_plot)
export(ts_compare_data)
export(ts_event_analysis_plot)
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ None
1. Fix #397 - Add function `ts_geometric_brownian_motion()`
2. Fix #402 - Add function `ts_brownian_motion_augment()`
3. Fix #403 - Add function `ts_geometric_brownian_motion_augment()`
4. Fix #404 - Add function `ts_brownian_motion_plot()`

## Minor Fixes and Improvements
1. Fix #395 - Update and optimize `ts_brownian_motion()` 49x speedup by way of
100 changes: 100 additions & 0 deletions R/brownian-motion-plot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#' Auto-Plot a Geometric/Brownian Motion Augment
#'
#' @family Plot
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @description Plot an augmented Geometric/Brownian Motion.
#'
#' @details This function will take output from either the `ts_brownian_motion_augment()`
#' or the `ts_geometric_brownian_motion_augment()` function and plot them. The
#' legend is set to "none" if the simulation count is higher than 9.
#'
#' @param .data The data you are going to pass to the function to augment.
#' @param .date_col The column that holds the date
#' @param .value_col The column that holds the value
#' @param .interactive The default is FALSE, TRUE will produce an interactive
#' plotly plot.
#'
#' @examples
#' library(dplyr)
#'
#' df <- ts_to_tbl(AirPassengers) %>% select(-index)
#'
#' augmented_data <- df %>%
#' ts_brownian_motion_augment(
#' .date_col = date_col,
#' .value_col = value,
#' .time = 144
#' )
#'
#' augmented_data %>%
#' ts_brownian_motion_plot(.date_col = date_col, .value_col = value)
#'
#' @return
#' A ggplot2 object or an interactive `plotly` plot
#'
#' @name ts_brownian_motion_plot
NULL

#' @export
#' @rdname ts_brownian_motion_plot

ts_brownian_motion_plot <- function(.data, .date_col, .value_col,
.interactive = FALSE){

# Tidyeval ---
plotly_plot <- as.logical(.interactive)
date_var_expr <- rlang::enquo(.date_col)
value_var_expr <- rlang::enquo(.value_col)

# Attributes
atb <- attributes(.data)

# Checks ---
if (!is.data.frame(.data)){
rlang::abort(
message = "'.data' must be either a data.frame/tibble.",
use_cli_format = TRUE
)
}

if (!is.logical(.interactive)){
rlang::abort(
message = "'.interactive' must be a logical of TRUE/FALSE.",
use_cli_format = TRUE
)
}

if (!".motion_type" %in% names(atb)){
rlang::abort(
message = "The '.data' must come from a ts_brownian or ts_geometric
function."
)
}

# Data Manipulation ---
df <- dplyr::as_tibble(.data)

# Graph ---
g <- df %>%
ggplot2::ggplot(ggplot2::aes(x = {{ date_var_expr }}, y = {{ value_var_expr }},
group = sim_number, color = sim_number)) +
ggplot2::geom_line() +
ggplot2::theme_minimal() +
ggplot2::labs(
title = atb$.motion_type,
subtitle = paste0("Simulations: ", atb$.num_sims,
" - Initial Value: ", round(atb$.initial_value, 2),
" - Delta Time: ", round(atb$.delta_time, 2))
) +
ggplot2::theme(legend.position = if(atb$.num_sims > 9) {"none"})

# Return ---
if (plotly_plot){
g <- plotly::ggplotly(g)
}

return(g)

}
55 changes: 55 additions & 0 deletions man/ts_brownian_motion_plot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/ts_event_analysis_plot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/ts_qq_plot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/ts_scedacity_scatter_plot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5aac3d6

Please sign in to comment.