diff --git a/R/NAMESPACE b/R/NAMESPACE index 5e558a4..b4cf5f0 100644 --- a/R/NAMESPACE +++ b/R/NAMESPACE @@ -1,9 +1,9 @@ # Generated by roxygen2: do not edit by hand +S3method(plot,prophet) S3method(predict,prophet) export(fit.prophet) export(make_future_dataframe) -export(plot.prophet) export(predictive_samples) export(prophet) export(prophet_plot_components) diff --git a/R/R/prophet.R b/R/R/prophet.R index f81fff8..534cde7 100644 --- a/R/R/prophet.R +++ b/R/R/prophet.R @@ -780,12 +780,12 @@ sample_posterior_predictive <- function(m, df) { n.iterations <- length(m$params$k) samp.per.iter <- max(1, ceiling(m$uncertainty.samples / n.iterations)) nsamp <- n.iterations * samp.per.iter # The actual number of samples - + seasonal.features <- make_all_seasonality_features(m, df) sim.values <- list("trend" = matrix(, nrow = nrow(df), ncol = nsamp), "seasonal" = matrix(, nrow = nrow(df), ncol = nsamp), "yhat" = matrix(, nrow = nrow(df), ncol = nsamp)) - + for (i in 1:n.iterations) { # For each set of parameters from MCMC (or just 1 set for MAP), for (j in 1:samp.per.iter) { @@ -799,7 +799,23 @@ sample_posterior_predictive <- function(m, df) { } return(sim.values) } - + +#' Sample from the posterior predictive distribution. +#' +#' @param m Prophet object. +#' @param df Dataframe with dates for predictions (column ds), and capacity +#' (column cap) if logistic growth. +#' +#' @return A list with items "trend", "seasonal", and "yhat" containing +#' posterior predictive samples for that component. +#' +#' @export +predictive_samples <- function(m, df) { + df <- setup_dataframe(m, df)$df + sim.values <- sample_posterior_predictive(m, df) + return(sim.values) +} + #' Prophet uncertainty intervals. #' #' @param m Prophet object. @@ -812,7 +828,7 @@ predict_uncertainty <- function(m, df) { # Add uncertainty estimates lower.p <- (1 - m$interval.width)/2 upper.p <- (1 + m$interval.width)/2 - + intervals <- cbind( t(apply(t(sim.values$yhat), 2, stats::quantile, c(lower.p, upper.p), na.rm = TRUE)), @@ -821,13 +837,12 @@ predict_uncertainty <- function(m, df) { t(apply(t(sim.values$seasonal), 2, stats::quantile, c(lower.p, upper.p), na.rm = TRUE)) ) %>% dplyr::as_data_frame() - + colnames(intervals) <- paste(rep(c('yhat', 'trend', 'seasonal'), each=2), c('lower', 'upper'), sep = "_") return(intervals) } - #' Simulate observations from the extrapolated generative model. #' #' @param m Prophet object. @@ -1175,20 +1190,4 @@ plot_yearly <- function(m, uncertainty = TRUE, yearly_start = 0) { return(gg.yearly) } -#' Sample from the posterior predictive distribution. -#' -#' @param m Prophet object. -#' @param df Dataframe with dates for predictions (column ds), and capacity -#' (column cap) if logistic growth. -#' -#' @return A list with items "trend", "seasonal", and "yhat" containing -#' posterior predictive samples for that component. -#' -#' @export -predictive_samples <- function(m, df) { - df <- setup_dataframe(m, df)$df - sim.values <- sample_posterior_predictive(m, df) - return(sim.values) -} - -# fb-block 3 \ No newline at end of file +# fb-block 3 diff --git a/R/man/plot.prophet.Rd b/R/man/plot.prophet.Rd index 4595b44..25c520e 100644 --- a/R/man/plot.prophet.Rd +++ b/R/man/plot.prophet.Rd @@ -4,8 +4,8 @@ \alias{plot.prophet} \title{Plot the prophet forecast.} \usage{ -plot.prophet(x, fcst, uncertainty = TRUE, plot_cap = TRUE, xlabel = "ds", - ylabel = "y", ...) +\method{plot}{prophet}(x, fcst, uncertainty = TRUE, plot_cap = TRUE, + xlabel = "ds", ylabel = "y", ...) } \arguments{ \item{x}{Prophet object.} diff --git a/R/man/predictive_samples.Rd b/R/man/predictive_samples.Rd index 3ade7e5..0e80a0a 100644 --- a/R/man/predictive_samples.Rd +++ b/R/man/predictive_samples.Rd @@ -13,7 +13,7 @@ predictive_samples(m, df) (column cap) if logistic growth.} } \value{ -A list with items "trend", "seasonal", and "yhat" containing +A list with items "trend", "seasonal", and "yhat" containing posterior predictive samples for that component. } \description{ diff --git a/R/man/sample_posterior_predictive.Rd b/R/man/sample_posterior_predictive.Rd new file mode 100644 index 0000000..989eb2f --- /dev/null +++ b/R/man/sample_posterior_predictive.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/prophet.R +\name{sample_posterior_predictive} +\alias{sample_posterior_predictive} +\title{Prophet posterior predictive samples.} +\usage{ +sample_posterior_predictive(m, df) +} +\arguments{ +\item{m}{Prophet object.} + +\item{df}{Prediction dataframe.} +} +\value{ +List with posterior predictive samples for each component. +} +\description{ +Prophet posterior predictive samples. +}