R/mrmvinput_to_mvmr_format.R

Defines functions mrmvinput_to_mvmr_format

Documented in mrmvinput_to_mvmr_format

#' Convert an object of class MRMVInput from the MendelianRandomization package to the MVMR mvmr_format class
#'
#' Creates a data.frame with additional class `mvmr_format` from an object of class `MRMVInput` generated by [`MendelianRandomization::mr_mvinput()`].
#'
#' @param dat Object from [`MendelianRandomization::mr_mvinput()`].
#'
#' @export
#' @return Object of class `mvmr_format`, the MVMR format
#' @examples
#' if (require("MendelianRandomization", quietly = TRUE)) {
#'   bx <- as.matrix(rawdat_mvmr[, c("LDL_beta", "HDL_beta")])
#'   bxse <- as.matrix(rawdat_mvmr[, c("LDL_se", "HDL_se")])
#'   dat <- MendelianRandomization::mr_mvinput(
#'     bx = bx,
#'     bxse = bxse,
#'     by = rawdat_mvmr$SBP_beta,
#'     byse = rawdat_mvmr$SBP_se,
#'     snps = rawdat_mvmr$SNP
#'   )
#'   dat <- mrmvinput_to_mvmr_format(dat)
#'   head(dat)
#'   class(dat)
#' }
mrmvinput_to_mvmr_format <- function(dat) {
  if (!("MRMVInput" %in% class(dat))) {
    stop('The class of the input data object must be "MRMVInput"')
  }

  out <- format_mvmr(BXGs = dat@betaX,
                     BYG = dat@betaY,
                     seBXGs = dat@betaXse,
                     seBYG = dat@betaYse,
                     RSID = dat@snps)
  out
}
WSpiller/MVMR documentation built on June 13, 2025, 4:54 p.m.