From ff9a940b072d43dcf445d6c6944d4b8bcbc07a8b Mon Sep 17 00:00:00 2001 From: Wojciekowski Date: Thu, 7 Dec 2023 12:41:17 +0100 Subject: [PATCH] - updated getContr() --- R/BMCPMod.R | 80 +++++++++++++++++++++++++++++------ R/s3methods.R | 6 +++ R/simulation.R | 2 +- man/assessDesign.Rd | 1 + man/getContr.Rd | 31 ++++++++++++++ man/getContrMat.Rd | 20 --------- man/getPosterior.Rd | 8 ++-- man/performBayesianMCP.Rd | 4 +- vignettes/analysis_normal.Rmd | 2 +- 9 files changed, 112 insertions(+), 42 deletions(-) create mode 100644 man/getContr.Rd delete mode 100644 man/getContrMat.Rd diff --git a/R/BMCPMod.R b/R/BMCPMod.R index 0a3f56c..cf46575 100644 --- a/R/BMCPMod.R +++ b/R/BMCPMod.R @@ -14,6 +14,8 @@ assessDesign <- function ( mods, prior_list, + sd = NULL, + n_sim = 1e3, alpha_crit_val = 0.05, simple = TRUE @@ -21,11 +23,16 @@ assessDesign <- function ( ) { dose_levels <- attr(prior_list, "dose_levels") + sd <- ifelse(is.null(sd), attr(prior_list, "sd_tot"), sd) + + stopifnot( + "sd length must coincide with number of dose levels" = + length(sd) == length(dose_levels)) data <- simulateData( n_patients = n_patients, dose_levels = dose_levels, - sd = attr(prior_list, "sd_tot"), + sd = sd, mods = mods, n_sim = n_sim) @@ -43,7 +50,7 @@ assessDesign <- function ( dose_weights = n_patients, alpha_crit_val = alpha_crit_val) - contr_mat_prior <- getContrMat( + contr_mat_prior <- getContr( mods = mods, dose_levels = dose_levels, dose_weights = n_patients, @@ -63,31 +70,75 @@ assessDesign <- function ( } -#' @title getContrMat +#' @title getContr #' #' @param mods tbd #' @param dose_levels tbd #' @param dose_weights tbd #' @param prior_list tbd +#' @param se_new_trial tbd +#' @param sd_posterior tbd #' #' @export -getContrMat <- function ( +getContr <- function ( mods, dose_levels, - dose_weights, - prior_list + dose_weights = NULL, + prior_list = NULL, + se_new_trial = NULL, + sd_posterior = NULL ) { - ess_prior <- suppressMessages(round(unlist(lapply(prior_list, RBesT::ess)))) + if (is.null(prior_list)) { # frequentist + + if (!is.null(se_new_trial)) { # re-estimate, se_new_trial + + w <- NULL + S <- diag((se_new_trial)^2) + + } else { # do not re-estimate, dose_weights + + w <- dose_weights + S <- NULL + + } + + } else { # Bayes + + if (!is.null(sd_posterior)) { # re-estimate, sd_posterior + + w <- NULL + S <- diag((sd_posterior)^2) + + } else { # do not re-estimate, dose_weights + prior_list + + w <- dose_weights + + suppressMessages(round(unlist(lapply(prior_list, RBesT::ess)))) + S <- NULL + + } + + } - contr_mat <- DoseFinding::optContr( - models = mods, - doses = dose_levels, - w = dose_weights + ess_prior) + if (is.null(w)) { + + contr <- DoseFinding::optContr( + models = mods, + doses = dose_levels, + S = S) + + } else { + + contr <- DoseFinding::optContr( + models = mods, + doses = dose_levels, + w = w) + + } - return (contr_mat) + return (contr) } @@ -204,7 +255,7 @@ addSignificance <- function ( } -#' @title BayesianMCP +#' @title performBayesianMCP #' #' @param posteriors_list tbd #' @param contr_mat tbd @@ -270,7 +321,8 @@ BayesMCPi <- function ( res <- c(sign = ifelse(max(post_probs) > crit_prob, 1, 0), p_val = max(post_probs), - post_probs = post_probs) + post_probs = post_probs, + crit_prob = crit_prob) # TODO attr crit_prob?? return (res) diff --git a/R/s3methods.R b/R/s3methods.R index 14aa663..a9bb96f 100644 --- a/R/s3methods.R +++ b/R/s3methods.R @@ -53,6 +53,12 @@ print.BayesianMCP <- function ( cat(" Estimated Success Rate: ", power, "\n") cat(" N Simulations: ", n_sim) + ## TODO if n_nim == 1 + # c(sign = ifelse(max(post_probs) > crit_prob, 1, 0), + # p_val = max(post_probs), + # post_probs = post_probs, + # crit_prob = crit_prob) + } ## ModelFits ---------------------------------------------- diff --git a/R/simulation.R b/R/simulation.R index 7c60082..2bd89b4 100644 --- a/R/simulation.R +++ b/R/simulation.R @@ -61,4 +61,4 @@ getModelData <- function ( return (model_data) -} \ No newline at end of file +} diff --git a/man/assessDesign.Rd b/man/assessDesign.Rd index 6883d23..5233e5b 100644 --- a/man/assessDesign.Rd +++ b/man/assessDesign.Rd @@ -8,6 +8,7 @@ assessDesign( n_patients, mods, prior_list, + sd = NULL, n_sim = 1000, alpha_crit_val = 0.05, simple = TRUE diff --git a/man/getContr.Rd b/man/getContr.Rd new file mode 100644 index 0000000..103d311 --- /dev/null +++ b/man/getContr.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/BMCPMod.R +\name{getContr} +\alias{getContr} +\title{getContr} +\usage{ +getContr( + mods, + dose_levels, + dose_weights = NULL, + prior_list = NULL, + se_new_trial = NULL, + sd_posterior = NULL +) +} +\arguments{ +\item{mods}{tbd} + +\item{dose_levels}{tbd} + +\item{dose_weights}{tbd} + +\item{prior_list}{tbd} + +\item{se_new_trial}{tbd} + +\item{sd_posterior}{tbd} +} +\description{ +getContr +} diff --git a/man/getContrMat.Rd b/man/getContrMat.Rd deleted file mode 100644 index d5393e3..0000000 --- a/man/getContrMat.Rd +++ /dev/null @@ -1,20 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/BMCPMod.R -\name{getContrMat} -\alias{getContrMat} -\title{getContrMat} -\usage{ -getContrMat(mods, dose_levels, dose_weights, prior_list) -} -\arguments{ -\item{mods}{tbd} - -\item{dose_levels}{tbd} - -\item{dose_weights}{tbd} - -\item{prior_list}{tbd} -} -\description{ -getContrMat -} diff --git a/man/getPosterior.Rd b/man/getPosterior.Rd index 501f5de..b404e81 100644 --- a/man/getPosterior.Rd +++ b/man/getPosterior.Rd @@ -4,16 +4,16 @@ \alias{getPosterior} \title{getPosterior} \usage{ -getPosterior(data, prior_list, mu_hat = NULL, sd_hat = NULL) +getPosterior(prior_list, data = NULL, mu_hat = NULL, se_hat = NULL) } \arguments{ -\item{data}{tbd} - \item{prior_list}{prior_list} +\item{data}{tbd} + \item{mu_hat}{tbd} -\item{sd_hat}{tbd} +\item{se_hat}{tbd} } \description{ getPosterior diff --git a/man/performBayesianMCP.Rd b/man/performBayesianMCP.Rd index e6b3543..6493c31 100644 --- a/man/performBayesianMCP.Rd +++ b/man/performBayesianMCP.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/BMCPMod.R \name{performBayesianMCP} \alias{performBayesianMCP} -\title{BayesianMCP} +\title{performBayesianMCP} \usage{ performBayesianMCP(posteriors_list, contr_mat, crit_prob) } @@ -14,5 +14,5 @@ performBayesianMCP(posteriors_list, contr_mat, crit_prob) \item{crit_prob}{tbd} } \description{ -BayesianMCP +performBayesianMCP } diff --git a/vignettes/analysis_normal.Rmd b/vignettes/analysis_normal.Rmd index 94542e7..610fdf0 100644 --- a/vignettes/analysis_normal.Rmd +++ b/vignettes/analysis_normal.Rmd @@ -123,7 +123,7 @@ crit_pval <- getCritProb( dose_weights = n_patients, alpha_crit_val = 0.1) -contr_mat_prior <- getContrMat( +contr_mat_prior <- getContr( mods = mods, dose_levels = dose_levels, dose_weights = n_patients,