Skip to content

Commit

Permalink
Adding the two-hierarchical parameter model
Browse files Browse the repository at this point in the history
  • Loading branch information
kaybenleroll committed Aug 24, 2023
1 parent 835b38f commit da67397
Show file tree
Hide file tree
Showing 28 changed files with 11,112 additions and 2,168 deletions.
108 changes: 55 additions & 53 deletions construct_cdnow_fixed_pnbd_models.html

Large diffs are not rendered by default.

35 changes: 20 additions & 15 deletions construct_cdnow_fixed_pnbd_models.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,6 @@ transaction data.

# Load and Construct Datasets

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("1997-01-01")
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")
```



## Load CD-NOW Transaction Data

Expand Down Expand Up @@ -173,11 +161,20 @@ customer_valid_subset_tbl |> glimpse()
```


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

# Fit First P/NBD Model

We now construct our Stan model and prepare to fit it with our synthetic
dataset.
```{r set_start_end_dates}
dates_lst <- read_rds("data/cdnow_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
```


Before we start on that, we set a few parameters for the workbook to organise
our Stan code.
Expand All @@ -189,6 +186,14 @@ stan_modeldir <- "stan_models"
stan_codedir <- "stan_code"
```



# Fit First P/NBD Model

We now construct our Stan model and prepare to fit it with our synthetic
dataset.


We also want to set a number of overall parameters for this workbook

To start the fit data, we want to use the 1,000 customers. We also need to
Expand Down
1,092 changes: 559 additions & 533 deletions construct_cdnow_onehier_pnbd_models.html

Large diffs are not rendered by default.

61 changes: 43 additions & 18 deletions construct_cdnow_onehier_pnbd_models.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,13 @@ synthetic data with the longer timeframe.

# Load and Construct Datasets

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("1997-01-01")
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")
```



## Load CD-Now Data
## Load CD-NOW Transaction Data

We now want to load the online retail transaction data.
We now want to load the CD-NOW transaction data.


```{r load_cdnow_data}
```{r load_cdnow_transaction_data}
#| echo: TRUE
customer_cohortdata_tbl <- read_rds("data/cdnow_customer_cohort_data_tbl.rds")
Expand All @@ -110,6 +98,31 @@ customer_subset_id |> glimpse()
```


We re-produce the visualisation of the transaction times we used in previous
workbooks.

```{r plot_customer_transaction_times}
#| echo: TRUE
plot_tbl <- customer_transactions_tbl |>
group_nest(customer_id, .key = "cust_data") |>
filter(map_int(cust_data, nrow) > 3) |>
slice_sample(n = 30) |>
unnest(cust_data)
ggplot(plot_tbl, aes(x = tnx_timestamp, y = customer_id)) +
geom_line() +
geom_point() +
labs(
x = "Date",
y = "Customer ID",
title = "Visualisation of Customer Transaction Times"
) +
theme(axis.text.y = element_text(size = 10))
```





## Load Derived Data
Expand Down Expand Up @@ -146,10 +159,23 @@ 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/cdnow_simulation_dates.rds")
Finally, we need to set our directories where we save our Stan code and the
model outputs.
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
```


Before we start on that, we set a few parameters for the workbook to organise
our Stan code.

```{r setup_workbook_parameters}
#| echo: TRUE
Expand All @@ -159,7 +185,6 @@ stan_codedir <- "stan_code"
```



# Fit First Hierarchical Lambda Model

Our first hierarchical model puts a hierarchical prior around the mean of our
Expand Down
3,817 changes: 3,817 additions & 0 deletions construct_cdnow_twohier_pnbd_models.html

Large diffs are not rendered by default.

Loading

0 comments on commit da67397

Please sign in to comment.