Prophet (time series)

Author

Charlie Kopp

Published

April 28, 2026

Data

Code
setwd("/Users/isaiahmireles/Desktop/M148 GP")
dt <- fread("dat_train1.csv")
Code
# drop incomplete rows

train_cutoff_date <- as.IDate(max(dt$event_timestamp))

# flatten data by user
user_dt <- dt[, .(
    first_action_date = as.IDate(min(event_timestamp)),
    last_action_date = as.IDate(max(event_timestamp)),
    has_shipped = any(event_name == "order_shipped"),
    total_actions = .N
), by = id]

# define days since last action as days_inactive

user_dt[, days_inactive := as.integer(train_cutoff_date - last_action_date)]

user_dt[, journey_outcome := fcase(
    has_shipped == TRUE, "success",
    has_shipped == FALSE & days_inactive > 60, "failure",
    default = "incomplete"
)]

table(user_dt$journey_outcome)

   failure incomplete    success 
    992129     158953     279363 
Code
# drop incomplete journeys

user_dt_complete <- user_dt[journey_outcome != "incomplete", ]
Code
# want to find distr. journey length for a successes and add upper end to cutoff date to reduce survivorship bias

successes_only <- user_dt_complete[journey_outcome == "success"]

purchase_times <- dt[event_name == "order_shipped", 
                     .(purchase_date = as.IDate(min(event_timestamp))), 
                     by = id]

# join back to find the journey length
journey_lengths <- merge(successes_only, purchase_times, by = "id")
journey_lengths[, days_to_buy := as.integer(purchase_date - first_action_date)]

# find the 95th percentile of how long it takes people to buy
p95_days_success <- quantile(journey_lengths$days_to_buy, probs = 0.95, na.rm = TRUE)
print(paste("95% of successful users buy within", round(p95_days_success), "days"))
[1] "95% of successful users buy within 221 days"
Code
# define safe cohort start date
# Safe Date = Cutoff - (95th Percentile Days + 60 Days Inactivity)
safe_cutoff <- train_cutoff_date - (round(p95_days_success) + 60)
Code
cohort_users <- user_dt_complete[first_action_date <= safe_cutoff]

prophet_train_data <- cohort_users[, .(
    total_users = .N,
    successes = sum(journey_outcome == "success"),
    y = sum(journey_outcome == "success") / .N # Prophet requires the target to be named 'y'
), by = .(ds = first_action_date)] # Prophet requires the date to be named 'ds'
Code
library(prophet)
library(ggplot2)

m <- prophet(yearly.seasonality = TRUE, weekly.seasonality=TRUE)
m <- add_country_holidays(m, country_name = "US")
m <- fit.prophet(m, prophet_train_data)


# alculate exactly how many days of "incomplete" cohorts we need to forecast
days_to_forecast <- as.numeric(train_cutoff_date - safe_cutoff)

# create a dataframe containing both historical dates AND future dates
future_dates <- make_future_dataframe(m, periods = days_to_forecast)

# generate the forecast
# This creates a massive dataframe containing the prediction (yhat) and all components
forecast <- predict(m, future_dates)
Code
# Plot 1: The Main Forecast 
plot(m, forecast) +
  ggtitle("FingerHut Cohort Success Rate Forecast") +
  xlab("Cohort Start Date") + 
  ylab("Baseline Success Proportion") +
  theme_minimal()

Code
# Plot 2: The Components Breakdown
prophet_plot_components(m, forecast)

Code
forecast |> tail()
            ds     trend additive_terms additive_terms_lower
807 2023-01-18 0.1892449    -0.01646934          -0.01646934
808 2023-01-19 0.1892265    -0.01313258          -0.01313258
809 2023-01-20 0.1892081    -0.01160381          -0.01160381
810 2023-01-21 0.1891897    -0.01781729          -0.01781729
811 2023-01-22 0.1891713    -0.01768908          -0.01768908
812 2023-01-23 0.1891529    -0.01992692          -0.01992692
    additive_terms_upper Christmas Day Christmas Day_lower Christmas Day_upper
807          -0.01646934             0                   0                   0
808          -0.01313258             0                   0                   0
809          -0.01160381             0                   0                   0
810          -0.01781729             0                   0                   0
811          -0.01768908             0                   0                   0
812          -0.01992692             0                   0                   0
    Christmas Day (Observed) Christmas Day (Observed)_lower
807                        0                              0
808                        0                              0
809                        0                              0
810                        0                              0
811                        0                              0
812                        0                              0
    Christmas Day (Observed)_upper Columbus Day Columbus Day_lower
807                              0            0                  0
808                              0            0                  0
809                              0            0                  0
810                              0            0                  0
811                              0            0                  0
812                              0            0                  0
    Columbus Day_upper holidays holidays_lower holidays_upper Independence Day
807                  0        0              0              0                0
808                  0        0              0              0                0
809                  0        0              0              0                0
810                  0        0              0              0                0
811                  0        0              0              0                0
812                  0        0              0              0                0
    Independence Day_lower Independence Day_upper Independence Day (Observed)
807                      0                      0                           0
808                      0                      0                           0
809                      0                      0                           0
810                      0                      0                           0
811                      0                      0                           0
812                      0                      0                           0
    Independence Day (Observed)_lower Independence Day (Observed)_upper
807                                 0                                 0
808                                 0                                 0
809                                 0                                 0
810                                 0                                 0
811                                 0                                 0
812                                 0                                 0
    Juneteenth National Independence Day
807                                    0
808                                    0
809                                    0
810                                    0
811                                    0
812                                    0
    Juneteenth National Independence Day_lower
807                                          0
808                                          0
809                                          0
810                                          0
811                                          0
812                                          0
    Juneteenth National Independence Day_upper
807                                          0
808                                          0
809                                          0
810                                          0
811                                          0
812                                          0
    Juneteenth National Independence Day (Observed)
807                                               0
808                                               0
809                                               0
810                                               0
811                                               0
812                                               0
    Juneteenth National Independence Day (Observed)_lower
807                                                     0
808                                                     0
809                                                     0
810                                                     0
811                                                     0
812                                                     0
    Juneteenth National Independence Day (Observed)_upper Labor Day
807                                                     0         0
808                                                     0         0
809                                                     0         0
810                                                     0         0
811                                                     0         0
812                                                     0         0
    Labor Day_lower Labor Day_upper Martin Luther King Jr. Day
807               0               0                          0
808               0               0                          0
809               0               0                          0
810               0               0                          0
811               0               0                          0
812               0               0                          0
    Martin Luther King Jr. Day_lower Martin Luther King Jr. Day_upper
807                                0                                0
808                                0                                0
809                                0                                0
810                                0                                0
811                                0                                0
812                                0                                0
    Memorial Day Memorial Day_lower Memorial Day_upper New Year's Day
807            0                  0                  0              0
808            0                  0                  0              0
809            0                  0                  0              0
810            0                  0                  0              0
811            0                  0                  0              0
812            0                  0                  0              0
    New Year's Day_lower New Year's Day_upper New Year's Day (Observed)
807                    0                    0                         0
808                    0                    0                         0
809                    0                    0                         0
810                    0                    0                         0
811                    0                    0                         0
812                    0                    0                         0
    New Year's Day (Observed)_lower New Year's Day (Observed)_upper
807                               0                               0
808                               0                               0
809                               0                               0
810                               0                               0
811                               0                               0
812                               0                               0
    Thanksgiving Thanksgiving_lower Thanksgiving_upper Veterans Day
807            0                  0                  0            0
808            0                  0                  0            0
809            0                  0                  0            0
810            0                  0                  0            0
811            0                  0                  0            0
812            0                  0                  0            0
    Veterans Day_lower Veterans Day_upper Washington's Birthday
807                  0                  0                     0
808                  0                  0                     0
809                  0                  0                     0
810                  0                  0                     0
811                  0                  0                     0
812                  0                  0                     0
    Washington's Birthday_lower Washington's Birthday_upper        weekly
807                           0                           0 -0.0005349246
808                           0                           0  0.0028371045
809                           0                           0  0.0043340205
810                           0                           0 -0.0019785001
811                           0                           0 -0.0020151077
812                           0                           0 -0.0044807149
     weekly_lower  weekly_upper      yearly yearly_lower yearly_upper
807 -0.0005349246 -0.0005349246 -0.01593442  -0.01593442  -0.01593442
808  0.0028371045  0.0028371045 -0.01596968  -0.01596968  -0.01596968
809  0.0043340205  0.0043340205 -0.01593783  -0.01593783  -0.01593783
810 -0.0019785001 -0.0019785001 -0.01583879  -0.01583879  -0.01583879
811 -0.0020151077 -0.0020151077 -0.01567397  -0.01567397  -0.01567397
812 -0.0044807149 -0.0044807149 -0.01544620  -0.01544620  -0.01544620
    multiplicative_terms multiplicative_terms_lower multiplicative_terms_upper
807                    0                          0                          0
808                    0                          0                          0
809                    0                          0                          0
810                    0                          0                          0
811                    0                          0                          0
812                    0                          0                          0
    yhat_lower yhat_upper trend_lower trend_upper      yhat
807  0.1341272  0.2130271   0.1731252   0.2042782 0.1727756
808  0.1350747  0.2192537   0.1729711   0.2043666 0.1760939
809  0.1376785  0.2152940   0.1728170   0.2044527 0.1776043
810  0.1306714  0.2100364   0.1726809   0.2045068 0.1713724
811  0.1320545  0.2108734   0.1725575   0.2045687 0.1714822
812  0.1293686  0.2101233   0.1724511   0.2046578 0.1692260