Store dates from history with y NaN for make_future_dataframe

This commit is contained in:
Ben Letham 2017-03-23 15:47:29 +02:00
parent 1a57d19148
commit 5677d8c7ce
2 changed files with 9 additions and 5 deletions

View file

@ -107,7 +107,8 @@ prophet <- function(df = df,
changepoints.t = NULL,
stan.fit = NULL,
params = list(),
history = NULL
history = NULL,
history.dates = NULL
)
validate_inputs(m)
class(m) <- append("prophet", class(m))
@ -455,6 +456,7 @@ logistic_growth_init <- function(df) {
fit.prophet <- function(m, df, ...) {
history <- df %>%
dplyr::filter(!is.na(y))
m$history.dates <- sort(zoo::as.Date(df$ds))
out <- setup_dataframe(m, history, initialize_scales = TRUE)
history <- out$df
@ -839,10 +841,10 @@ sample_predictive_trend <- function(model, df, iteration) {
#' @export
make_future_dataframe <- function(m, periods, freq = 'd',
include_history = TRUE) {
dates <- seq(max(m$history$ds), length.out = periods + 1, by = freq)
dates <- seq(max(m$history.dates), length.out = periods + 1, by = freq)
dates <- dates[2:(periods + 1)] # Drop the first, which is max(history$ds)
if (include_history) {
dates <- c(m$history$ds, dates)
dates <- c(m$history.dates, dates)
}
return(data.frame(ds = dates))
}

View file

@ -87,6 +87,7 @@ class Prophet(object):
self.stan_fit = None
self.params = {}
self.history = None
self.history_dates = None
self.validate_inputs()
def validate_inputs(self):
@ -350,6 +351,7 @@ class Prophet(object):
The fitted Prophet object.
"""
history = df[df['y'].notnull()].copy()
self.history_dates = pd.to_datetime(df['ds']).sort_values()
history = self.setup_dataframe(history, initialize_scales=True)
self.history = history
@ -608,7 +610,7 @@ class Prophet(object):
return trend * self.y_scale
def make_future_dataframe(self, periods, freq='D', include_history=True):
last_date = self.history['ds'].max()
last_date = self.history_dates.max()
dates = pd.date_range(
start=last_date,
periods=periods + 1, # An extra in case we include start
@ -617,7 +619,7 @@ class Prophet(object):
dates = dates[:periods] # Return correct number of periods
if include_history:
dates = np.concatenate((np.array(self.history['ds']), dates))
dates = np.concatenate((np.array(self.history_dates), dates))
return pd.DataFrame({'ds': dates})