Skip to content

Commit

Permalink
Sync'd up the code in the various workbooks and making sure it all runs
Browse files Browse the repository at this point in the history
  • Loading branch information
kaybenleroll committed Jul 25, 2023
1 parent 991fdc6 commit 835b38f
Show file tree
Hide file tree
Showing 39 changed files with 17,403 additions and 3,366 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.Rproj.user/
.vscode/

*_cache/*
*_files/*
Expand All @@ -12,3 +13,5 @@
*.log

temp*.R


3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime \
fs \
furrr \
loo \
markdown \
modeltime \
posterior \
projpred \
Expand All @@ -70,8 +71,6 @@ RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime \
timetk


COPY build/logging.conf /etc/rstudio/

WORKDIR /tmp

COPY build/docker_install_sys_rpkgs.R /tmp
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ construct_longsynth_onehier_pnbd_models.html: construct_longsynth_fixed_pnbd_mod
construct_onlineretail_onehier_pnbd_models.html: construct_onlineretail_fixed_pnbd_models.html
construct_cdnow_onehier_pnbd_models.html: construct_cdnow_fixed_pnbd_models.html

construct_shortsynth_twohier_pnbd_models.html: construct_shortsynth_onehier_pnbd_models.html
construct_longsynth_twohier_pnbd_models.html: construct_longsynth_onehier_pnbd_models.html
construct_onlineretail_twohier_pnbd_models.html: construct_onlineretail_onehier_pnbd_models.html
construct_cdnow_twohier_pnbd_models.html: construct_cdnow_onehier_pnbd_models.html


mrproper: clean-cache clean-data clean-html clean-precompute clean-models
Expand Down
3 changes: 0 additions & 3 deletions build/logging.conf

This file was deleted.

71 changes: 0 additions & 71 deletions build/test_report.qmd

This file was deleted.

122 changes: 31 additions & 91 deletions construct_cdnow_fixed_pnbd_models.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion construct_cdnow_fixed_pnbd_models.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ try to model real-life data.

```{r set_start_end_dates}
use_fit_start_date <- as.Date("1997-01-01")
use_fit_end_date <- as.Date("1997-12-31")
use_fit_end_date <- as.Date("1998-01-01")
use_valid_start_date <- as.Date("1998-01-01")
use_valid_end_date <- as.Date("1998-07-01")
Expand Down
124 changes: 32 additions & 92 deletions construct_cdnow_onehier_pnbd_models.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion construct_cdnow_onehier_pnbd_models.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ try to model real-life data.

```{r set_start_end_dates}
use_fit_start_date <- as.Date("1997-01-01")
use_fit_end_date <- as.Date("1997-12-31")
use_fit_end_date <- as.Date("1998-01-01")
use_valid_start_date <- as.Date("1998-01-01")
use_valid_end_date <- as.Date("1999-01-10")
Expand Down
1,059 changes: 554 additions & 505 deletions construct_longsynth_fixed_pnbd_models.html

Large diffs are not rendered by default.

79 changes: 56 additions & 23 deletions construct_longsynth_fixed_pnbd_models.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,6 @@ synthetic data with the longer timeframe.
We start by modelling the P/NBD model using our synthetic datasets before we
try to model real-life data.

```{r set_start_end_dates}
use_fit_start_date <- as.Date("2010-01-01")
use_fit_end_date <- as.Date("2022-01-01")
use_valid_start_date <- as.Date("2022-01-01")
use_valid_end_date <- as.Date("2023-01-01")
```



## Load Long Time-frame Synthetic Data

Expand All @@ -111,9 +102,9 @@ customer_transactions_tbl |> glimpse()
```


## Load Derived Datasets
## Load Derived Data

```{r write_data_disk}
```{r load_modelling_data}
#| echo: TRUE
id_1000 <- read_rds("data/longsynth_id_1000.rds")
Expand All @@ -128,15 +119,48 @@ customer_summarystats_tbl <- read_rds("data/longsynth_customer_summarystats_tbl.
obs_fitdata_tbl <- read_rds("data/longsynth_obs_fitdata_tbl.rds")
obs_validdata_tbl <- read_rds("data/longsynth_obs_validdata_tbl.rds")
customer_subset_id <- id_1000
```


## Load Subset Data

# Fit First P/NBD Model
We also want to construct our data subsets for the purposes of speeding up our
valuations.

```{r construct_customer_subset_data}
#| echo: TRUE
customer_fit_subset_tbl <- obs_fitdata_tbl |>
filter(customer_id %in% customer_subset_id)
customer_fit_subset_tbl |> glimpse()
customer_valid_subset_tbl <- obs_validdata_tbl |>
filter(customer_id %in% customer_subset_id)
customer_valid_subset_tbl |> glimpse()
```

We now use these datasets to set the start and end dates for our various
validation methods.


```{r set_start_end_dates}
dates_lst <- read_rds("data/longsynth_simulation_dates.rds")
use_fit_start_date <- dates_lst$use_fit_start_date
use_fit_end_date <- dates_lst$use_fit_end_date
use_valid_start_date <- dates_lst$use_valid_start_date
use_valid_end_date <- dates_lst$use_valid_end_date
```

We need to set up a number of workbook parameters, such as some directories
for the Stan model.

Finally, we need to set our directories where we save our Stan code and the
model outputs.

```{r setup_workbook_parameters}
#| echo: TRUE
Expand All @@ -147,6 +171,9 @@ stan_codedir <- "stan_code"



# Fit First P/NBD Model


## Compile and Fit Stan Model

We now compile this model using `CmdStanR`.
Expand Down Expand Up @@ -270,10 +297,12 @@ run the simulations and combine the datasets.
```{r run_pnbd_long_fixed1_assessment}
#| echo: TRUE
pnbd_stanfit <- pnbd_long_fixed1_stanfit |>
recover_types(customer_fit_stats_tbl)
pnbd_long_fixed1_assess_data_lst <- run_model_assessment(
model_stanfit = pnbd_long_fixed1_stanfit,
insample_tbl = customer_fit_stats_tbl,
outsample_tbl = customer_valid_stats_tbl,
model_stanfit = pnbd_stanfit,
insample_tbl = customer_fit_subset_tbl,
fit_label = "pnbd_long_fixed1",
fit_end_dttm = use_fit_end_date |> as.POSIXct(),
valid_start_dttm = use_valid_start_date |> as.POSIXct(),
Expand Down Expand Up @@ -446,10 +475,12 @@ run the simulations and combine the datasets.
```{r run_pnbd_long_fixed2_assessment}
#| echo: TRUE
pnbd_stanfit <- pnbd_long_fixed2_stanfit |>
recover_types(customer_fit_stats_tbl)
pnbd_long_fixed2_assess_data_lst <- run_model_assessment(
model_stanfit = pnbd_long_fixed2_stanfit,
insample_tbl = customer_fit_stats_tbl,
outsample_tbl = customer_valid_stats_tbl,
model_stanfit = pnbd_stanfit,
insample_tbl = customer_fit_subset_tbl,
fit_label = "pnbd_long_fixed2",
fit_end_dttm = use_fit_end_date |> as.POSIXct(),
valid_start_dttm = use_valid_start_date |> as.POSIXct(),
Expand Down Expand Up @@ -619,10 +650,12 @@ run the simulations and combine the datasets.
```{r run_pnbd_long_fixed3_assessment}
#| echo: TRUE
pnbd_stanfit <- pnbd_long_fixed3_stanfit |>
recover_types(customer_fit_stats_tbl)
pnbd_long_fixed3_assess_data_lst <- run_model_assessment(
model_stanfit = pnbd_long_fixed3_stanfit,
insample_tbl = customer_fit_stats_tbl,
outsample_tbl = customer_valid_stats_tbl,
model_stanfit = pnbd_stanfit,
insample_tbl = customer_fit_subset_tbl,
fit_label = "pnbd_long_fixed3",
fit_end_dttm = use_fit_end_date |> as.POSIXct(),
valid_start_dttm = use_valid_start_date |> as.POSIXct(),
Expand Down
Loading

0 comments on commit 835b38f

Please sign in to comment.