mirror of
https://github.com/saymrwulf/prophet.git
synced 2026-07-07 17:15:37 +00:00
Allow shifting the start date of the weekly seasonality plot
This commit is contained in:
parent
d937f47612
commit
cacfdc635e
4 changed files with 35 additions and 10 deletions
|
|
@ -987,13 +987,16 @@ plot.prophet <- function(x, fcst, uncertainty = TRUE, plot_cap = TRUE,
|
|||
#' plotted for the trend, from fcst columns trend_lower and trend_upper.
|
||||
#' @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
|
||||
#' seasonality plot. 0 (default) starts the week on Sunday. 1 shifts by 1 day
|
||||
#' to Monday, and so on.
|
||||
#'
|
||||
#' @return Invisibly return a list containing the plotted ggplot objects
|
||||
#'
|
||||
#' @export
|
||||
#' @importFrom dplyr "%>%"
|
||||
prophet_plot_components <- function(m, fcst, uncertainty = TRUE,
|
||||
plot_cap = TRUE) {
|
||||
plot_cap = TRUE, weekly_start = 0) {
|
||||
df <- df_for_plotting(m, fcst)
|
||||
# Plot the trend
|
||||
panels <- list(plot_trend(df, uncertainty, plot_cap))
|
||||
|
|
@ -1003,7 +1006,7 @@ prophet_plot_components <- function(m, fcst, uncertainty = TRUE,
|
|||
}
|
||||
# Plot weekly seasonality, if present
|
||||
if ("weekly" %in% colnames(df)) {
|
||||
panels[[length(panels) + 1]] <- plot_weekly(m, uncertainty)
|
||||
panels[[length(panels) + 1]] <- plot_weekly(m, uncertainty, weekly_start)
|
||||
}
|
||||
# Plot yearly seasonality, if present
|
||||
if ("yearly" %in% colnames(df)) {
|
||||
|
|
@ -1083,12 +1086,16 @@ plot_holidays <- function(m, df, uncertainty = TRUE) {
|
|||
#'
|
||||
#' @param m Prophet model object
|
||||
#' @param uncertainty Boolean to plot uncertainty intervals.
|
||||
#' @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.
|
||||
#'
|
||||
#' @return A ggplot2 plot.
|
||||
plot_weekly <- function(m, uncertainty = TRUE) {
|
||||
plot_weekly <- function(m, uncertainty = TRUE, weekly_start = 0) {
|
||||
# Compute weekly seasonality for a Sun-Sat sequence of dates.
|
||||
df.w <- data.frame(
|
||||
ds=seq.Date(zoo::as.Date('2017-01-01'), by='d', length.out=7), cap=1.)
|
||||
ds=seq.Date(zoo::as.Date('2017-01-01'), by='d', length.out=7) +
|
||||
weekly_start, cap=1.)
|
||||
df.w <- setup_dataframe(m, df.w)$df
|
||||
seas <- predict_seasonal_components(m, df.w)
|
||||
seas$dow <- factor(weekdays(df.w$ds), levels=weekdays(df.w$ds))
|
||||
|
|
|
|||
|
|
@ -4,12 +4,16 @@
|
|||
\alias{plot_weekly}
|
||||
\title{Plot the weekly component of the forecast.}
|
||||
\usage{
|
||||
plot_weekly(m, uncertainty = TRUE)
|
||||
plot_weekly(m, uncertainty = TRUE, weekly_start = 0)
|
||||
}
|
||||
\arguments{
|
||||
\item{m}{Prophet model object}
|
||||
|
||||
\item{uncertainty}{Boolean to plot uncertainty intervals.}
|
||||
|
||||
\item{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.}
|
||||
}
|
||||
\value{
|
||||
A ggplot2 plot.
|
||||
|
|
|
|||
|
|
@ -6,7 +6,8 @@
|
|||
Prints a ggplot2 with panels for trend, weekly and yearly seasonalities if
|
||||
present, and holidays if present.}
|
||||
\usage{
|
||||
prophet_plot_components(m, fcst, uncertainty = TRUE, plot_cap = TRUE)
|
||||
prophet_plot_components(m, fcst, uncertainty = TRUE, plot_cap = TRUE,
|
||||
weekly_start = 0)
|
||||
}
|
||||
\arguments{
|
||||
\item{m}{Prophet object.}
|
||||
|
|
@ -18,6 +19,10 @@ plotted for the trend, from fcst columns trend_lower and trend_upper.}
|
|||
|
||||
\item{plot_cap}{Boolean indicating if the capacity should be shown in the
|
||||
figure, if available.}
|
||||
|
||||
\item{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.}
|
||||
}
|
||||
\value{
|
||||
Invisibly return a list containing the plotted ggplot objects
|
||||
|
|
|
|||
|
|
@ -914,7 +914,8 @@ class Prophet(object):
|
|||
fig.tight_layout()
|
||||
return fig
|
||||
|
||||
def plot_components(self, fcst, uncertainty=True, plot_cap=True):
|
||||
def plot_components(self, fcst, uncertainty=True, plot_cap=True,
|
||||
weekly_start=0):
|
||||
"""Plot the Prophet forecast components.
|
||||
|
||||
Will plot whichever are available of: trend, holidays, weekly
|
||||
|
|
@ -926,6 +927,9 @@ class Prophet(object):
|
|||
uncertainty: Optional boolean to plot uncertainty intervals.
|
||||
plot_cap: Optional boolean indicating if the capacity should be shown
|
||||
in the figure, if available.
|
||||
weekly_start: Optional int 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.
|
||||
|
||||
Returns
|
||||
-------
|
||||
|
|
@ -951,7 +955,8 @@ class Prophet(object):
|
|||
artists += self.plot_holidays(fcst, ax=ax,
|
||||
uncertainty=uncertainty)
|
||||
elif plot == 'weekly':
|
||||
artists += self.plot_weekly(ax=ax, uncertainty=uncertainty)
|
||||
artists += self.plot_weekly(ax=ax, uncertainty=uncertainty,
|
||||
weekly_start=weekly_start)
|
||||
elif plot == 'yearly':
|
||||
artists += self.plot_yearly(ax=ax, uncertainty=uncertainty)
|
||||
|
||||
|
|
@ -1027,7 +1032,7 @@ class Prophet(object):
|
|||
ax.set_ylabel('holidays')
|
||||
return artists
|
||||
|
||||
def plot_weekly(self, ax=None, uncertainty=True):
|
||||
def plot_weekly(self, ax=None, uncertainty=True, weekly_start=0):
|
||||
"""Plot the weekly component of the forecast.
|
||||
|
||||
Parameters
|
||||
|
|
@ -1035,6 +1040,9 @@ class Prophet(object):
|
|||
ax: Optional matplotlib Axes to plot on. One will be created if this
|
||||
is not provided.
|
||||
uncertainty: Optional boolean to plot uncertainty intervals.
|
||||
weekly_start: Optional int 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.
|
||||
|
||||
Returns
|
||||
-------
|
||||
|
|
@ -1045,7 +1053,8 @@ class Prophet(object):
|
|||
fig = plt.figure(facecolor='w', figsize=(10, 6))
|
||||
ax = fig.add_subplot(111)
|
||||
# Compute weekly seasonality for a Sun-Sat sequence of dates.
|
||||
days = pd.date_range(start='2017-01-01', periods=7)
|
||||
days = (pd.date_range(start='2017-01-01', periods=7) +
|
||||
pd.Timedelta(days=weekly_start))
|
||||
df_w = pd.DataFrame({'ds': days, 'cap': 1.})
|
||||
df_w = self.setup_dataframe(df_w)
|
||||
seas = self.predict_seasonal_components(df_w)
|
||||
|
|
|
|||
Loading…
Reference in a new issue