Refactor R cross validation to match Py function structure

This commit is contained in:
Ben Letham 2021-03-04 14:04:48 -08:00
parent eb6b8f60ec
commit a4b31cd70b

View file

@ -131,8 +131,26 @@ cross_validation <- function(
predicts <- data.frame() predicts <- data.frame()
for (i in 1:length(cutoffs)) { for (i in 1:length(cutoffs)) {
# Copy the model df.c <- single_cutoff_forecast(df, model, cutoffs[i], horizon.dt, predict_columns)
cutoff <- cutoffs[i] predicts <- rbind(predicts, df.c)
}
return(predicts)
}
#' Forecast for a single cutoff.
#' Used in cross_validation function when evaluating for multiple cutoffs.
#'
#' @param df Dataframe with history for cutoff.
#' @param model Prophet model object.
#' @param cutoff Datetime of cutoff.
#' @param horizon.dt timediff forecast horizon.
#' @param predict_columns Array of names of columns to be returned in output.
#'
#' @return Dataframe with forecast, actual value, and cutoff.
#'
#' @keywords internal
single_cutoff_forecast <- function(df, model, cutoff, horizon.dt, predict_columns){
m <- prophet_copy(model, cutoff) m <- prophet_copy(model, cutoff)
# Train model # Train model
history.c <- dplyr::filter(df, ds <= cutoff) history.c <- dplyr::filter(df, ds <= cutoff)
@ -165,9 +183,7 @@ cross_validation <- function(
df.c <- df.c[c(predict_columns, "y")] df.c <- df.c[c(predict_columns, "y")]
df.c <- dplyr::select(df.c, y, predict_columns) df.c <- dplyr::select(df.c, y, predict_columns)
df.c$cutoff <- cutoff df.c$cutoff <- cutoff
predicts <- rbind(predicts, df.c) return(df.c)
}
return(predicts)
} }
#' Copy Prophet object. #' Copy Prophet object.