mirror of
https://github.com/saymrwulf/prophet.git
synced 2026-11-18 08:53:42 +00:00
Refactor R cross validation to match Py function structure
This commit is contained in:
parent
eb6b8f60ec
commit
a4b31cd70b
1 changed files with 50 additions and 34 deletions
|
|
@ -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.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue