R/calculate_weighted_estimate.R

Defines functions calculate_weighted_estimate

Documented in calculate_weighted_estimate

#' Calculate Weighted Estimate
#'
#' Provide an estimate based on user-specified weights for the choices of the research design.
#'
#' @param df The data frame as generated by \code{exhaust_design(..., weight = TRUE)}.
#' @param est A character value indicating the variable in \code{df} that contains the point estimate.
#' @param lb A character value indicating the variable in \code{df} that contains
#'   the lower bound of the estimate.
#' @param ub A character value indicating the variable in \code{df} that contains
#'   the upper bound of the estimate.
#' @return A list containing the
#' \describe{
#'  \item{"Estimate"}{Weighted estimate}
#'  \item{"Lower bound"}{Weighted lower bound}
#'  \item{"Upper bound"}{Weighted upper bound}
#'  \item{"n"}{Number of model estimates that have been used to generate the weighted estimate}
#' }
#' @details
#'   See the vignette of the package for further details on how to implement the RDF workflow.
#' @examples
#' \dontrun{
#'   print("Sorry. No examples yet.")
#' }
#' @export

calculate_weighted_estimate <- function(df, est, lb, ub) {
  l <- list(
    stats::weighted.mean(df[, est], df$weight),
    stats::weighted.mean(df[, lb], df$weight),
    stats::weighted.mean(df[, ub], df$weight),
    nrow(df)
  )
  names(l) <- c(est, lb, ub, "n")
  l
}
joachim-gassen/rdfanalysis documentation built on Aug. 22, 2023, 5:29 p.m.