Skip to content

Commit

Permalink
narrate_forecast fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
denisabd committed Jul 5, 2023
1 parent 1101eed commit 5eb0540
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ inst/doc
docs
sandbox
package_prep.R
profiling.R
.DS_Store
37 changes: 21 additions & 16 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ ytd_volume <- function(
if (nrow(df) == 0) stop("df must have at least one row, execution is stopped")

# Getting the right data types
df <- df %>%
dplyr::ungroup() %>%
tibble::as_tibble() %>%
readr::type_convert(na = c("")) %>%
suppressMessages() %>%
suppressWarnings()
# df <- df %>%
# dplyr::ungroup() %>%
# tibble::as_tibble() %>%
# readr::type_convert(na = c("")) %>%
# suppressMessages() %>%
# suppressWarnings()

# Summarization Assertion
if (!summarization %in% c("sum", "count", "average")) stop("summarization must of be one of: 'sum', 'count' or 'mean'.")
Expand Down Expand Up @@ -187,6 +187,7 @@ ytd_volume <- function(
#' @param summarization summarization field in series, one of the following methods - 'count', 'mean', 'sum'
#' @param current_year add current year to avoid situation when some of the groups don't have data in latest periods and current year is estimated incorrectly
#' @param py_date date to be used for PYTD calculation, if NULL then \code{get_py_date()} will be used by default
#' @param frequency time series frequency
#'
#' @return \code{numeric} value
#' @noRd
Expand All @@ -196,19 +197,20 @@ pytd_volume <- function(
date = NULL,
summarization = "sum",
current_year = NULL,
py_date = NULL) {
py_date = NULL,
frequency = NULL) {

# Table must be a data.frame and have at least one row
if (!is.data.frame(df)) stop("df must be a data.frame or tibble")
if (nrow(df) == 0) stop("df must have at least one row, execution is stopped")

# Getting the right data types
df <- df %>%
dplyr::ungroup() %>%
tibble::as_tibble() %>%
readr::type_convert(na = c("")) %>%
suppressMessages() %>%
suppressWarnings()
# df <- df %>%
# dplyr::ungroup() %>%
# tibble::as_tibble() %>%
# readr::type_convert(na = c("")) %>%
# suppressMessages() %>%
# suppressWarnings()

# Summarization Assertion
if (!summarization %in% c("sum", "count", "average")) stop("summarization must of be one of: 'sum', 'count' or 'mean'.")
Expand Down Expand Up @@ -242,7 +244,7 @@ pytd_volume <- function(

# PY Date
if (is.null(py_date)) {
py_date <- get_py_date(df)
py_date <- get_py_date(df, frequency = frequency)
} else {
py_date <- as.Date(py_date)
}
Expand Down Expand Up @@ -393,6 +395,7 @@ get_descriptive_outliers <- function(
#' @param coverage Portion of variability to be covered by narrative, 0 to 1
#' @param coverage_limit Maximum number of elements to be narrated, overrides
#' coverage to avoid extremely verbose narrative creation
#' @param frequency Time series frequency
#'
#' @noRd
get_trend_outliers <- function(
Expand All @@ -402,7 +405,8 @@ get_trend_outliers <- function(
total = NULL,
summarization = "sum",
coverage = 0.5,
coverage_limit = 5) {
coverage_limit = 5,
frequency = NULL) {

table <- df %>%
dplyr::group_by(dplyr::across(dplyr::all_of(dimension))) %>%
Expand All @@ -413,7 +417,8 @@ get_trend_outliers <- function(
measure = measure),
prev_volume = purrr::map_dbl(data, pytd_volume,
summarization = summarization,
measure = measure),
measure = measure,
frequency = frequency),
change = curr_volume - prev_volume,
change_p = paste(round(change/prev_volume*100, 2), "%"),
abs_change = abs(change),
Expand Down
6 changes: 5 additions & 1 deletion R/narrate_forecast.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ narrate_forecast <- function(
presence_penalty = 0,
forecast = "Forecast",
actuals = "Actuals",
template_cy = "Forecasted volume for {current_year} is {format_num(cy_forecast)}",
template_cy = "Forecasted volumes for {current_year} are equal to {format_num(cy_forecast)}",
template_ftm = "Overall forecast for the next 12 months is {format_num(ftm_forecast)}",
template_ftm_change = "Projected {trend} in the next 12 months is equal to {format_num(ftm_change)} ({ftm_change_p}%).",
use_renviron = FALSE,
Expand Down Expand Up @@ -106,6 +106,10 @@ narrate_forecast <- function(
}
}

# leaving only the neccessary fields
df <- df %>%
dplyr::select(dplyr::all_of(c(date, forecast, actuals)))

if (!date %in% names(df)) stop("{date} is not a column in the data frame")

if (!any(class(df[[date]]) %in% c("Date", "POSIXct", "POSIXlt"))) {
Expand Down
27 changes: 25 additions & 2 deletions R/narrate_trend.R
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,8 @@ narrate_trend <- function(
"count" = NULL),
summarization = summarization,
coverage = coverage,
coverage_limit = coverage_limit)
coverage_limit = coverage_limit,
frequency = frequency)

if (is.null(output)) next

Expand All @@ -375,6 +376,27 @@ narrate_trend <- function(
outlier_change <- format_num(outlier_change)
}

###
# inc <- which(trend == "increase")
# dec <- which(trend == "decrease")
#
# outlier_insight_inc <- list(
# outlier_levels[inc], " (", outlier_change[inc], ", ", outlier_change_p[inc], ", ",
# outlier_prev_volume[inc], " to ", outlier_curr_volume[inc],")"
# ) %>%
# purrr::pmap(paste0) %>%
# unlist() %>%
# toString()
#
# outlier_insight_dec <- list(
# outlier_levels[dec], " (", outlier_change[dec], ", ", outlier_change_p[dec], ", ",
# outlier_prev_volume[dec], " to ", outlier_curr_volume[dec],")"
# ) %>%
# purrr::pmap(paste0) %>%
# unlist() %>%
# toString()
###

outlier_insight <- list(
outlier_levels, " (", outlier_change, ", ", outlier_change_p, ", ",
outlier_prev_volume, " to ", outlier_curr_volume,")"
Expand Down Expand Up @@ -463,7 +485,8 @@ narrate_trend <- function(
"count" = NULL),
summarization = summarization,
coverage = coverage,
coverage_limit = coverage_limit)
coverage_limit = coverage_limit,
frequency = frequency)

if (is.null(output)) next

Expand Down
3 changes: 2 additions & 1 deletion man/narrate_forecast.Rd

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

0 comments on commit 5eb0540

Please sign in to comment.