Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geomtric/Brownian Motion plotting helper function #404

Closed
spsanderson opened this issue Jan 9, 2023 · 0 comments
Closed

Geomtric/Brownian Motion plotting helper function #404

spsanderson opened this issue Jan 9, 2023 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@spsanderson
Copy link
Owner

spsanderson commented Jan 9, 2023

Create a helper function to plot out the geometric/brownian motion augment functions.

Function:

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)
    
}

Example:

library(healthyR.ts)
library(tidyverse)

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)

image

@spsanderson spsanderson added the enhancement New feature or request label Jan 9, 2023
@spsanderson spsanderson added this to the healthyR.ts 0.2.7 milestone Jan 9, 2023
@spsanderson spsanderson self-assigned this Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

1 participant