mirror of
https://github.com/saymrwulf/prophet.git
synced 2026-11-19 09:00:59 +00:00
Refactor setup_dataframe R
This commit is contained in:
parent
2ddcf54930
commit
8066634cb4
4 changed files with 87 additions and 35 deletions
|
|
@ -333,7 +333,55 @@ setup_dataframe <- function(m, df, initialize_scales = FALSE) {
|
||||||
df <- df %>%
|
df <- df %>%
|
||||||
dplyr::arrange(ds)
|
dplyr::arrange(ds)
|
||||||
|
|
||||||
if (initialize_scales) {
|
m <- initialize_scales_fn(m, initialize_scales, df)
|
||||||
|
|
||||||
|
if (m$logistic.floor) {
|
||||||
|
if (!('floor' %in% colnames(df))) {
|
||||||
|
stop("Expected column 'floor'.")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
df$floor <- 0
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m$growth == 'logistic') {
|
||||||
|
if (!(exists('cap', where=df))) {
|
||||||
|
stop('Capacities must be supplied for logistic growth.')
|
||||||
|
}
|
||||||
|
df <- df %>%
|
||||||
|
dplyr::mutate(cap_scaled = (cap - floor) / m$y.scale)
|
||||||
|
}
|
||||||
|
|
||||||
|
df$t <- time_diff(df$ds, m$start, "secs") / m$t.scale
|
||||||
|
if (exists('y', where=df)) {
|
||||||
|
df$y_scaled <- (df$y - df$floor) / m$y.scale
|
||||||
|
}
|
||||||
|
|
||||||
|
for (name in names(m$extra_regressors)) {
|
||||||
|
df[[name]] <- as.numeric(df[[name]])
|
||||||
|
props <- m$extra_regressors[[name]]
|
||||||
|
df[[name]] <- (df[[name]] - props$mu) / props$std
|
||||||
|
if (anyNA(df[[name]])) {
|
||||||
|
stop('Found NaN in column ', name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return(list("m" = m, "df" = df))
|
||||||
|
}
|
||||||
|
|
||||||
|
#' Initialize model scales.
|
||||||
|
#'
|
||||||
|
#' Sets model scaling factors using df.
|
||||||
|
#'
|
||||||
|
#' @param m Prophet object.
|
||||||
|
#' @param initialize_scales Boolean set the scales or not.
|
||||||
|
#' @param df Dataframe for setting scales.
|
||||||
|
#'
|
||||||
|
#' @return Prophet object with scales set.
|
||||||
|
#'
|
||||||
|
#' @keywords internal
|
||||||
|
initialize_scales_fn <- function(m, initialize_scales, df) {
|
||||||
|
if (!initialize_scales) {
|
||||||
|
return(m)
|
||||||
|
}
|
||||||
if ((m$growth == 'logistic') && ('floor' %in% colnames(df))) {
|
if ((m$growth == 'logistic') && ('floor' %in% colnames(df))) {
|
||||||
m$logistic.floor <- TRUE
|
m$logistic.floor <- TRUE
|
||||||
floor <- df$floor
|
floor <- df$floor
|
||||||
|
|
@ -366,38 +414,7 @@ setup_dataframe <- function(m, df, initialize_scales = FALSE) {
|
||||||
m$extra_regressors[[name]]$std <- std
|
m$extra_regressors[[name]]$std <- std
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return(m)
|
||||||
|
|
||||||
if (m$logistic.floor) {
|
|
||||||
if (!('floor' %in% colnames(df))) {
|
|
||||||
stop("Expected column 'floor'.")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
df$floor <- 0
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m$growth == 'logistic') {
|
|
||||||
if (!(exists('cap', where=df))) {
|
|
||||||
stop('Capacities must be supplied for logistic growth.')
|
|
||||||
}
|
|
||||||
df <- df %>%
|
|
||||||
dplyr::mutate(cap_scaled = (cap - floor) / m$y.scale)
|
|
||||||
}
|
|
||||||
|
|
||||||
df$t <- time_diff(df$ds, m$start, "secs") / m$t.scale
|
|
||||||
if (exists('y', where=df)) {
|
|
||||||
df$y_scaled <- (df$y - df$floor) / m$y.scale
|
|
||||||
}
|
|
||||||
|
|
||||||
for (name in names(m$extra_regressors)) {
|
|
||||||
df[[name]] <- as.numeric(df[[name]])
|
|
||||||
props <- m$extra_regressors[[name]]
|
|
||||||
df[[name]] <- (df[[name]] - props$mu) / props$std
|
|
||||||
if (anyNA(df[[name]])) {
|
|
||||||
stop('Found NaN in column ', name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return(list("m" = m, "df" = df))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#' Set changepoints
|
#' Set changepoints
|
||||||
|
|
|
||||||
22
R/man/initialize_scales_fn.Rd
Normal file
22
R/man/initialize_scales_fn.Rd
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
% Generated by roxygen2: do not edit by hand
|
||||||
|
% Please edit documentation in R/prophet.R
|
||||||
|
\name{initialize_scales_fn}
|
||||||
|
\alias{initialize_scales_fn}
|
||||||
|
\title{Initialize model scales.}
|
||||||
|
\usage{
|
||||||
|
initialize_scales_fn(m, initialize_scales, df)
|
||||||
|
}
|
||||||
|
\arguments{
|
||||||
|
\item{m}{Prophet object.}
|
||||||
|
|
||||||
|
\item{initialize_scales}{Boolean set the scales or not.}
|
||||||
|
|
||||||
|
\item{df}{Dataframe for setting scales.}
|
||||||
|
}
|
||||||
|
\value{
|
||||||
|
Prophet object with scales set.
|
||||||
|
}
|
||||||
|
\description{
|
||||||
|
Sets model scaling factors using df.
|
||||||
|
}
|
||||||
|
\keyword{internal}
|
||||||
|
|
@ -255,6 +255,15 @@ class Prophet(object):
|
||||||
return df
|
return df
|
||||||
|
|
||||||
def initialize_scales(self, initialize_scales, df):
|
def initialize_scales(self, initialize_scales, df):
|
||||||
|
"""Initialize model scales.
|
||||||
|
|
||||||
|
Sets model scaling factors using df.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
initialize_scales: Boolean set the scales or not.
|
||||||
|
df: pd.DataFrame for setting scales.
|
||||||
|
"""
|
||||||
if not initialize_scales:
|
if not initialize_scales:
|
||||||
return
|
return
|
||||||
if self.growth == 'logistic' and 'floor' in df:
|
if self.growth == 'logistic' and 'floor' in df:
|
||||||
|
|
|
||||||
|
|
@ -472,7 +472,11 @@ class TestProphet(TestCase):
|
||||||
m.fit(DATA.copy())
|
m.fit(DATA.copy())
|
||||||
seasonal_features, prior_scales = m.make_all_seasonality_features(
|
seasonal_features, prior_scales = m.make_all_seasonality_features(
|
||||||
m.history)
|
m.history)
|
||||||
self.assertEqual(prior_scales, [2.] * 10 + [10.] * 6 + [4.])
|
if seasonal_features.columns[0] == 'monthly_delim_1':
|
||||||
|
true = [2.] * 10 + [10.] * 6 + [4.]
|
||||||
|
else:
|
||||||
|
true = [10.] * 6 + [2.] * 10 + [4.]
|
||||||
|
self.assertEqual(prior_scales, true)
|
||||||
|
|
||||||
def test_added_regressors(self):
|
def test_added_regressors(self):
|
||||||
m = Prophet()
|
m = Prophet()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue