prophet icon indicating copy to clipboard operation
prophet copied to clipboard

Feature request: Allow access to raw samples for predictions.

Open jonlachmann opened this issue 2 years ago • 1 comments

As prophet is using stan as backend, I noticed that there are internal functions available to access the raw samples for a prediction instead of just the mean and confidence limits. This would be awesome if it was exposed in a function, perhaps as an option to predict.prophet.

I created an example of a separate function that does this. Let me know if you would like it integrated in another function or as a standalone function. I can then create a pull request for it.

Best regards.

Example function:

predictsamples.prophet <- function (object, df = NULL, ...) {
    if (is.null(object$history)) {
    stop("Model must be fit before predictions can be made.")
  }
  if (is.null(df)) {
    df <- object$history
  } else {
    if (nrow(df) == 0) {
      stop("Dataframe has no rows.")
    }
    out <- prophet:::setup_dataframe(object, df)
    df <- out$df
  }

  df$trend <- prophet:::predict_trend(object, df)
  samples <- prophet:::sample_posterior_predictive(object, df)

  return(samples$yhat)
}

jonlachmann avatar Mar 15 '22 09:03 jonlachmann

Does the predictive_samples() function suit your use case?

tcuongd avatar May 22 '22 17:05 tcuongd