Skip to content

Commit

Permalink
trying to get the cgd vignette to run but it crashes. TODO
Browse files Browse the repository at this point in the history
  • Loading branch information
n8thangreen committed Jun 25, 2024
1 parent 2bc3912 commit 09db0ae
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 43 deletions.
8 changes: 2 additions & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
# Generated by roxygen2: do not edit by hand

export(bmcm_stan)
export(cf_forest_cutpoint)
export(cf_forest_plot)
export(create_stancode)
export(prep_latent_data)
import(adegenet)
import(dplyr)
import(epicontacts)
import(ggplot2)
import(grid)
import(gridExtra)
import(purrr)
import(rstanarm)
import(survival)
import(tidybayes)
importFrom("epicontacts,",adegenet)
importFrom(dplyr,mutate)
importFrom(glue,glue)
importFrom(glue,glue_collapse)
Expand Down
2 changes: 0 additions & 2 deletions R/bmcm_stan.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ bmcm_stan <- function(input_data,
t_max = 70,
...) {
call <- match.call()

rtn_wd <- getwd()
setwd(here::here("inst/stan"))
on.exit(setwd(rtn_wd))
Expand Down Expand Up @@ -70,7 +69,6 @@ bmcm_stan <- function(input_data,
}}

formula_latent <- parse_formula(formula, input_data, family = distns)

if (is_pooled(formula_cure)) {
formula_cure$cf_idx <- 1L
formula_cure$cf_name <- "pooled"
Expand Down
3 changes: 1 addition & 2 deletions R/cf_forest_cutpoint.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
#' @param save_name File name appended text
#' @param is_hier Is hierarchical model (or separate)? i.e. whether to use a global parameter
#'
#' @importFrom epicontacts, adegenet
#' @import dplyr ggplot2 tidybayes purrr
#' @import dplyr ggplot2 tidybayes purrr epicontacts adegenet
#'
#' @return
#' @export
Expand Down
1 change: 0 additions & 1 deletion R/default_prior_cure.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
default_prior_cure <- function(formula_cure,
prior_cure = list(),
bg_model = 2) {

nTx <- formula_cure$fe_nlevels[1]
n_groups <- formula_cure$n_groups
nvars <- formula_cure$nvars
Expand Down
8 changes: 5 additions & 3 deletions R/parse_formula.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ parse_formula <- function(formula, data, family = NA) {
# if (!inherits(formula, "formula")) {
# stop("'formula' must be a formula.")
# }

formula <- as.formula(formula)

# all variables of entire formula
Expand All @@ -32,15 +31,18 @@ parse_formula <- function(formula, data, family = NA) {
bars <- lme4::findbars(formula)[[1]]
re_parts <- split_at_bars(bars)

mf <- model.frame(lme4::subbars(formula), data = data)
# Substitute the '+' function for the '|' function
formsub <- lme4::subbars(formula)
mf <- model.frame(formsub, data = data)

# names of variable without event type
fe_vars <- attr(terms(fe_form), "term.labels")
fe_nvars <- length(fe_vars)

re_nlevels <- length(unique(mf[, re_parts$re_group_var]))

mf[, re_parts$re_group_var] <- as.factor(mf[, re_parts$re_group_var])
if (!is.null(re_parts$re_group_var))
mf[, re_parts$re_group_var] <- as.factor(mf[, re_parts$re_group_var])

fe_nlevels <- apply(mf[, fe_vars, drop=FALSE], 2, \(x) nlevels(as.factor(x)))

Expand Down
7 changes: 4 additions & 3 deletions vignettes/cgd_analysis.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Drop any levels which only appeared in the data we've just removed and convert t

```{r filter-center}
cgd_center <- cgd_first |>
# filter(center %in% c("Amsterdam", "NIH", "Univ. of Zurich", "Scripps Institute")) |>
filter(center %in% c("Amsterdam", "NIH", "Univ. of Zurich", "Scripps Institute")) |>
droplevels() |>
mutate(center_id = as.numeric(center),
hos.cat_id = as.numeric(hos.cat),
Expand Down Expand Up @@ -126,12 +126,13 @@ out <-
formula = "Surv(time=time, event=status) ~ 1",
cureformula = "~ treat + (1 | center_id)",
family_latent = "exponential",
prior_cure = NA,
# prior_cure = NA,
centre_coefs = TRUE,
bg_model = "bg_fixed",
bg_varname = "rate",
bg_hr = 1,
t_max = 365)
t_max = 400)
# t_max = 365)
```

### By hospital category
Expand Down
38 changes: 12 additions & 26 deletions vignettes/how_to_use.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This is a basic introduction to how to use `rstanbmcm` to fit Bayesian mixture c

## Data

We will use the Checkmate 067 study data set.
We will use the Checkmate 067 study data set @ref.
The data have already been arranged in to the correct format and saved within the package so we can load it as follows.

```{r eval=FALSE}
Expand Down Expand Up @@ -73,34 +73,20 @@ options(mc.cores = parallel::detectCores() - 1)
```
Now we are ready to do the model fitting.
There are 3 options to use.
* `bmcm_joint_stan_string`: Constructs the Stan code on-the-fly, meaning it is flexible and can run all distributions combinations. It calls the Stan file directly from R without pre-compiling. This is useful for development.
* `bmcm_joint_stan_file()`: Same as above but for pre-written code. Only available for same-distribution OS-PFS pairs.
* `bmcm_joint_stan()`: Uses the pre-compiled Stan code. Quicker to run. Like `bmcm_joint_stan_file()` only for a subset of OS-PFS distribution pairs.
An example call to `bmcm_joint_stan_string` is given below.
We recommend using this because it is the most widely-applicable.
The other functions can be used for testing.
```{r eval=FALSE}
out <-
bmcm_joint_stan_string(
input_data = surv_input_data,
model_os = i,
model_pfs = k,
tx_name = j,
params_cf =
list(mu_cf_gl = array(-0.8, 1),
sigma_cf_gl = array(2, 1),
sd_cf_os = array(0.5, 1),
sd_cf_pfs = array(0.5, 1))
cf_model = 3,
joint_model = FALSE,
bg_model = 2,
warmup = 100,
iter = 1000,
thin = 10)
bmcm_stan(
input_data = input_data,
formula = "Surv(time=time, event=status) ~ 1",
cureformula = "~ treat + (1 | center_id)",
family_latent = "exponential",
# prior_cure = NA,
centre_coefs = TRUE,
bg_model = "bg_fixed",
bg_varname = "rate",
bg_hr = 1,
t_max = 400)
```
### Explanation of function arguments
Expand Down

0 comments on commit 09db0ae

Please sign in to comment.