R/Mixprior.R

Defines functions Mixprior

Documented in Mixprior

#' Constructing a mixture prior for longitudinal data with informative dropout
#'
#' The \code{Mixprior} function constructs a mixture prior for longitudinal data with informative dropout
#'  by mixing an informative prior (built from the
#' historical arm/posterior draws) and a non-informative (e.g., unit-information)
#' prior. It supports both continuous (\code{normMix}) and binary (\code{betaMix})
#' outcomes and simply delegates to \code{RBesT::mixcombine}.
#'
#' @param informative_prior Informative prior for \eqn{\theta} derived from
#'   historical data, represented as \code{RBesT::normMix} (continuous) or
#'   \code{"betaMix"} (binary).
#' @param noninformative_prior Non-informative prior (e.g., unit-information for continuous outcome or Uniform(0,1) for binary outcome)
#'   of the same class as \code{informative_prior}:
#'   \itemize{
#'     \item Continuous: \code{normMix}. See also \code{unit_information_prior()}.
#'     \item Binary: \code{"betaMix"}; commonly \code{RBesT::mixbeta(c(1,1,1))}.
#'   }
#' @param weight Mixture weight assigned to the informative component
#'   (\eqn{0 \leq} \code{weight} \eqn{\leq 1}); may be a scalar \eqn{w} or a
#'   vector \code{c(w, 1-w)}.
#'
#' @return A prior of the same class as \code{informative_prior}:
#'   \code{normMix} for continuous endpoints or \code{"betaMix"} for binary endpoints.
#'
#' @export
#'
#' @examples
#' # --- Continuous endpoint (normMix) ---
#' set.seed(1000)
#' hist_draws <- rnorm(1000, mean = -3.2, sd = 0.35)
#' inf_prior_c  <- RBesT::mixfit(hist_draws, type = "norm", Nc = 3)  # informative
#' nf_prior_c   <- unit_information_prior(inf_prior_c, n_hist = 200) # unit-info
#' w_c <- 0.6  # (e.g., from SLB_weight)
#' prior_c <- Mixprior(informative_prior   = inf_prior_c,
#'                       noninformative_prior = nf_prior_c,
#'                       weight = w_c)
#' prior_c
#'
#' # --- Binary endpoint (betaMix) ---
#' inf_prior_b <- RBesT::mixbeta(c(1, 20, 30))    # Beta(20,30) as one-component mix
#' nf_prior_b  <- RBesT::mixbeta(c(1, 1, 1))      # Uniform(0,1)
#' w_b <- 0.5
#' prior_b <- Mixprior(informative_prior   = inf_prior_b,
#'                       noninformative_prior = nf_prior_b,
#'                       weight = w_b)
#' prior_b
Mixprior <- function(informative_prior, noninformative_prior, weight) {

  # Weight validation: scalar w in [0,1] or length-2 c(w, 1-w)
  if (length(weight) == 1L) {
    stopifnot(is.finite(weight), weight >= 0, weight <= 1)
    wvec <- c(weight, 1 - weight)
  } else {
    stopifnot(length(weight) == 2L, all(is.finite(weight)),
              all(weight >= 0), abs(sum(weight) - 1) < 1e-8)
    wvec <- weight
  }

  RBesT::mixcombine(informative_prior, noninformative_prior, weight = wvec)
}

Try the BDIB package in your browser

Any scripts or data that you put into this service are public.

BDIB documentation built on Oct. 18, 2026, 5:08 p.m.