Disabling plotting uncertainties if m.samples is False or 0

This commit is contained in:
Ryan Nazareth 2019-10-03 18:31:12 +01:00 committed by Ben Letham
parent 36f3ddc436
commit a2a0df904e

View file

@ -24,8 +24,9 @@ df_for_plotting <- function(m, fcst) {
#'
#' @param x Prophet object.
#' @param fcst Data frame returned by predict(m, df).
#' @param uncertainty Boolean indicating if the uncertainty interval for yhat
#' should be plotted. Must be present in fcst as yhat_lower and yhat_upper.
#' @param uncertainty Optional boolean indicating if the uncertainty interval for yhat
#' should be plotted, which will only be done if x$uncertainty.samples > 0.
#' Must be present in fcst as yhat_lower and yhat_upper.
#' @param plot_cap Boolean indicating if the capacity should be shown in the
#' figure, if available.
#' @param xlabel Optional label for x-axis
@ -58,7 +59,7 @@ plot.prophet <- function(x, fcst, uncertainty = TRUE, plot_cap = TRUE,
gg <- gg + ggplot2::geom_line(
ggplot2::aes(y = floor), linetype = 'dashed', na.rm = TRUE)
}
if (uncertainty && exists('yhat_lower', where = df)) {
if (uncertainty && x$uncertainty.samples && exists('yhat_lower', where = df)) {
gg <- gg +
ggplot2::geom_ribbon(ggplot2::aes(ymin = yhat_lower, ymax = yhat_upper),
alpha = 0.2,
@ -80,8 +81,9 @@ plot.prophet <- function(x, fcst, uncertainty = TRUE, plot_cap = TRUE,
#'
#' @param m Prophet object.
#' @param fcst Data frame returned by predict(m, df).
#' @param uncertainty Boolean indicating if the uncertainty interval should be
#' plotted for the trend, from fcst columns trend_lower and trend_upper.
#' @param uncertainty Optional boolean indicating if the uncertainty interval should be
#' plotted for the trend, from fcst columns trend_lower and trend_upper.This will
#' only be done if m$uncertainty.samples > 0.
#' @param plot_cap Boolean indicating if the capacity should be shown in the
#' figure, if available.
#' @param weekly_start Integer specifying the start day of the weekly
@ -164,7 +166,8 @@ prophet_plot_components <- function(
#' @param m Prophet model
#' @param fcst Dataframe output of `predict`.
#' @param name String name of the component to plot (column of fcst).
#' @param uncertainty Boolean to plot uncertainty intervals.
#' @param uncertainty Optional boolean to plot uncertainty intervals, which will
#' only be done if m$uncertainty.samples > 0.
#' @param plot_cap Boolean indicating if the capacity should be shown in the
#' figure, if available.
#'
@ -185,7 +188,7 @@ plot_forecast_component <- function(
gg.comp <- gg.comp + ggplot2::geom_line(
ggplot2::aes(y = floor), linetype = 'dashed', na.rm = TRUE)
}
if (uncertainty) {
if (uncertainty && m$uncertainty.samples) {
gg.comp <- gg.comp +
ggplot2::geom_ribbon(
ggplot2::aes_string(
@ -229,7 +232,8 @@ seasonality_plot_df <- function(m, ds) {
#' Plot the weekly component of the forecast.
#'
#' @param m Prophet model object
#' @param uncertainty Boolean to plot uncertainty intervals.
#' @param uncertainty Optional boolean to plot uncertainty intervals, which will
#' only be done if m$uncertainty.samples > 0.
#' @param weekly_start Integer specifying the start day of the weekly
#' seasonality plot. 0 (default) starts the week on Sunday. 1 shifts by 1 day
#' to Monday, and so on.
@ -252,7 +256,7 @@ plot_weekly <- function(m, uncertainty = TRUE, weekly_start = 0,
seas, ggplot2::aes_string(x = 'dow', y = name, group = 1)) +
ggplot2::geom_line(color = "#0072B2", na.rm = TRUE) +
ggplot2::labs(x = "Day of week")
if (uncertainty) {
if (uncertainty && m$uncertainty.samples) {
gg.weekly <- gg.weekly +
ggplot2::geom_ribbon(ggplot2::aes_string(ymin = paste0(name, '_lower'),
ymax = paste0(name, '_upper')),
@ -271,7 +275,8 @@ plot_weekly <- function(m, uncertainty = TRUE, weekly_start = 0,
#' Plot the yearly component of the forecast.
#'
#' @param m Prophet model object.
#' @param uncertainty Boolean to plot uncertainty intervals.
#' @param uncertainty Optional boolean to plot uncertainty intervals, which
#' will only be done if m$uncertainty.samples > 0.
#' @param yearly_start Integer specifying the start day of the yearly
#' seasonality plot. 0 (default) starts the year on Jan 1. 1 shifts by 1 day
#' to Jan 2, and so on.
@ -295,7 +300,7 @@ plot_yearly <- function(m, uncertainty = TRUE, yearly_start = 0,
ggplot2::geom_line(color = "#0072B2", na.rm = TRUE) +
ggplot2::labs(x = "Day of year") +
ggplot2::scale_x_datetime(labels = scales::date_format('%B %d'))
if (uncertainty) {
if (uncertainty && m$uncertainty.samples) {
gg.yearly <- gg.yearly +
ggplot2::geom_ribbon(ggplot2::aes_string(ymin = paste0(name, '_lower'),
ymax = paste0(name, '_upper')),
@ -315,7 +320,8 @@ plot_yearly <- function(m, uncertainty = TRUE, yearly_start = 0,
#'
#' @param m Prophet model object.
#' @param name String name of the seasonality.
#' @param uncertainty Boolean to plot uncertainty intervals.
#' @param uncertainty Optional boolean to plot uncertainty intervals, which
#' will only be done if m$uncertainty.samples > 0.
#'
#' @return A ggplot2 plot.
#'
@ -342,7 +348,7 @@ plot_seasonality <- function(m, name, uncertainty = TRUE) {
}
gg.s <- gg.s +
ggplot2::scale_x_datetime(labels = scales::date_format(fmt.str))
if (uncertainty) {
if (uncertainty && m$uncertainty.samples) {
gg.s <- gg.s +
ggplot2::geom_ribbon(
ggplot2::aes_string(
@ -396,8 +402,9 @@ add_changepoints_to_plot <- function(m, threshold = 0.01, cp_color = "red",
#'
#' @param x Prophet object.
#' @param fcst Data frame returned by predict(m, df).
#' @param uncertainty Boolean indicating if the uncertainty interval for yhat
#' should be plotted. Must be present in fcst as yhat_lower and yhat_upper.
#' @param uncertainty Optional boolean indicating if the uncertainty interval for yhat
#' should be plotted, which will only be done if x$uncertainty.samples > 0. Must be
#' present in fcst as yhat_lower and yhat_upper.
#' @param ... additional arguments
#' @importFrom dplyr "%>%"
#' @return A dygraph plot.
@ -423,7 +430,7 @@ dyplot.prophet <- function(x, fcst, uncertainty=TRUE,
df <- df_for_plotting(x, fcst)
# build variables to include, or not, the uncertainty data
if(uncertainty && exists("yhat_lower", where = df))
if(uncertainty && x$uncertainty.sampes && exists("yhat_lower", where = df))
{
colsToKeep <- c('y', 'yhat', 'yhat_lower', 'yhat_upper')
forecastCols <- c('yhat_lower', 'yhat', 'yhat_upper')