Skip to content

Commit

Permalink
Move towards single wrap function with others imported
Browse files Browse the repository at this point in the history
  • Loading branch information
athowes committed Nov 15, 2024
1 parent e346fd8 commit aa24b86
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 863 deletions.
29 changes: 12 additions & 17 deletions inst/marginal_model-scratch.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ sim_obs <- simulate_gillespie() |>
sdlog = sdlog
) |>
observe_process() |>
filter_obs_by_obs_time(obs_time = obs_time) |>
filter(.data$stime_upr <= obs_time) |>
dplyr::slice_sample(n = sample_size, replace = FALSE)

# Create cohort version of data
Expand Down Expand Up @@ -49,8 +49,8 @@ summary(fit_direct_weighted)

lognormal <- brms::lognormal()

primarycensored_lognormal_uniform <- brms::custom_family(
"primarycensored_lognormal_uniform",
primarycensored_family <- brms::custom_family(
"primarycensored_wrapper",
dpars = lognormal$dpar,
links = c(lognormal$link, lognormal$link_sigma),
type = "int",
Expand All @@ -65,20 +65,15 @@ data <- cohort_obs |>
q = pmax(d - pwindow, 0)
)

stanvars_functions <- brms::stanvar(
pcd_stanvars_functions <- brms::stanvar(
block = "functions",
scode = .stan_chunk("cohort_model/primarycensored-edit.stan")
scode = pcd_load_stan_functions()
)

# stanvars_tparameters <- brms::stanvar(
# block = "tparameters",
# scode = .stan_chunk("cohort_model/tparameters.stan")
# )

# stanvars_tdata <- brms::stanvar(
# block = "tdata",
# scode = .stan_chunk("cohort_model/tdata.stan")
# )
stanvars_functions <- brms::stanvar(
block = "functions",
scode = .stan_chunk("cohort_model/functions.stan")
)

pwindow <- data$pwindow

Expand All @@ -88,11 +83,11 @@ stanvars_data <- brms::stanvar(
scode = .stan_chunk("cohort_model/data.stan")
)

stanvars_all <- stanvars_functions + stanvars_data
stanvars_all <- pcd_stanvars_functions + stanvars_functions + stanvars_data

stancode <- brms::make_stancode(
formula = d | weights(n) + vreal(q) ~ 1,
family = primarycensored_lognormal_uniform,
family = primarycensored_family,
data = data,
stanvars = stanvars_all,
)
Expand All @@ -101,7 +96,7 @@ model <- rstan::stan_model(model_code = stancode)

fit_pcd <- brms::brm(
formula = d | weights(n) + vreal(q) ~ 1,
family = primarycensored_lognormal_uniform,
family = primarycensored_family,
data = data,
stanvars = stanvars_all,
backend = "cmdstanr"
Expand Down
8 changes: 8 additions & 0 deletions inst/stan/marginal_model/functions.stan
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
real primarycensored_wrapper_lpmf(data int d, real mu, real sigma, real q, data real pwindow) {
int dist_id = 1; // lognormal
array[2] real params = {mu, sigma};
int d_upper = d + 1;
int primary_id = 1; // Uniform
array[0] real primary_params;
return primarycensored_lpmf(d | dist_id, params, pwindow, d_upper, positive_infinity(), primary_id, primary_params);
}
Loading

0 comments on commit aa24b86

Please sign in to comment.