Skip to content

Commit

Permalink
updated pkgdown site and fixed some small bugs with this
Browse files Browse the repository at this point in the history
  • Loading branch information
n8thangreen committed Dec 15, 2023
1 parent 2b4b2c9 commit 611be4b
Show file tree
Hide file tree
Showing 33 changed files with 515 additions and 212 deletions.
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ export(strategy_stc)
export(trial_treatment_effect)
export(trial_variance)
importFrom(Rdpack,reprompt)
importFrom(copula,mvdc)
importFrom(copula,normalCopula)
importFrom(copula,rMvdc)
importFrom(rstanarm,posterior_predict)
importFrom(rstanarm,stan_glm)
8 changes: 3 additions & 5 deletions R/IPD_stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
#' @export
#'
strategy_maic <- function(formula = as.formula("y ~ X3 + X4 + trt*X1 + trt*X2"),
R = 1000,
ald) {

R = 1000) {
if (class(formula) != "formula")
stop("formula argument must be of formula class.")

Expand Down Expand Up @@ -247,7 +245,7 @@ new_strategy <- function(strategy, ...) {
hat_Delta_stats <- function(AC.IPD, BC.ALD, strategy, CI = 0.95, ...) {

if (CI <= 0 || CI >= 1) stop("CI argument must be between 0 and 1.")

##TODO: as method instead?
if (!inherits(strategy, "strategy"))
stop("strategy argument must be of a class strategy.")
Expand Down Expand Up @@ -329,7 +327,7 @@ IPD_stats.maic <- function(strategy,
statistic = maic.boot,
R = strategy$R,
formula = strategy$formula,
ald = strategy$dat_ALD)
ald = ald)

list(mean = mean(maic_boot$t),
var = var(maic_boot$t))
Expand Down
15 changes: 8 additions & 7 deletions R/gcomp_ml.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ gcomp_ml.boot <- function(data, indices,
#'
#' @return Difference of log-odds
#' @seealso [strategy_gcomp_ml()], [gcomp_ml.boot()]
#' @importFrom copula normalCopula mvdc rMvdc
#' @export
#'
gcomp_ml_log_odds_ratio <- function(formula, dat) {
Expand All @@ -56,9 +57,9 @@ gcomp_ml_log_odds_ratio <- function(formula, dat) {
# AC IPD pairwise correlations
t_rho <- t(rho) # extract along rows
cop <-
normalCopula(param = t_rho[lower.tri(t_rho)],
dim = n_covariates,
dispstr = "un")
copula::normalCopula(param = t_rho[lower.tri(t_rho)],
dim = n_covariates,
dispstr = "un")

# BC covariate means & standard deviations
mean_sd_margins <- list()
Expand All @@ -69,12 +70,12 @@ gcomp_ml_log_odds_ratio <- function(formula, dat) {
}

# sample covariates from approximate joint distribution using copula
mvd <- mvdc(copula = cop,
margins = rep("norm", n_covariates), # Gaussian marginals
paramMargins = mean_sd_margins)
mvd <- copula::mvdc(copula = cop,
margins = rep("norm", n_covariates), # Gaussian marginals
paramMargins = mean_sd_margins)

# simulated BC pseudo-population
x_star <- as.data.frame(rMvdc(n = 1000, mvd))
x_star <- as.data.frame(copula::rMvdc(n = 1000, mvd))
colnames(x_star) <- covariate_names

# outcome logistic regression fitted to IPD using maximum likelihood
Expand Down
26 changes: 14 additions & 12 deletions R/gcomp_stan.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#' @template args-ald
#'
#' @return A list of \eqn{y^*_A} and \eqn{y^*_C} posterior predictions
#' @importFrom copula normalCopula mvdc rMvdc
#' @importFrom rstanarm stan_glm posterior_predict
#' @export
#'
gcomp_stan <- function(formula = as.formula("y ~ X3 + X4 + trt*X1 + trt*X2"),
Expand All @@ -27,11 +29,11 @@ gcomp_stan <- function(formula = as.formula("y ~ X3 + X4 + trt*X1 + trt*X2"),

# covariate simulation for BC trial using copula package
cop <-
normalCopula(param = c(rho[1,2],rho[1,3],rho[1,4],
rho[2,3],rho[2,4],
rho[3,4]),
dim = n_covariates,
dispstr = "un") # AC IPD pairwise correlations
copula::normalCopula(param = c(rho[1,2],rho[1,3],rho[1,4],
rho[2,3],rho[2,4],
rho[3,4]),
dim = n_covariates,
dispstr = "un") # AC IPD pairwise correlations

# sample covariates from approximate joint distribution using copula
mvd <- mvdc(copula = cop,
Expand All @@ -48,11 +50,11 @@ gcomp_stan <- function(formula = as.formula("y ~ X3 + X4 + trt*X1 + trt*X2"),
colnames(x_star) <- cov_names

# outcome logistic regression fitted to IPD using MCMC (Stan)
outcome.model <- stan_glm(formula,
data = ipd,
family = binomial,
algorithm = "sampling",
iter = 4000, warmup = 2000, chains = 2)
outcome.model <- rstanarm::stan_glm(formula,
data = ipd,
family = binomial,
algorithm = "sampling",
iter = 4000, warmup = 2000, chains = 2)
# counterfactual datasets
data.trtA <- data.trtC <- x_star

Expand All @@ -62,7 +64,7 @@ gcomp_stan <- function(formula = as.formula("y ~ X3 + X4 + trt*X1 + trt*X2"),

# draw binary responses from posterior predictive distribution
list(
y.star.A = posterior_predict(outcome.model, newdata=data.trtA),
y.star.C = posterior_predict(outcome.model, newdata=data.trtC))
y.star.A = rstanarm::posterior_predict(outcome.model, newdata=data.trtA),
y.star.C = rstanarm::posterior_predict(outcome.model, newdata=data.trtC))
}

89 changes: 69 additions & 20 deletions docs/articles/Basic_example.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 611be4b

Please sign in to comment.