R/map.R

Defines functions map

Documented in map

#' Maximum A Posteriori Precision Matrix
#'
#'
#' @param Y Matrix (or data frame) of dimensions \emph{n} (observations) by  \emph{p} (variables).
#'
#' @return An object of class \code{map}, including the precision matrix,
#'         partial correlation matrix, and regression parameters.
#'
#' @export
#'
#' @examples
#' Y <- BGGM::bfi[, 1:5]
#'
#' # map
#' map <- map(Y)
#' map
map <- function(Y){

  Y <- na.omit(Y)

  p <- ncol(Y)

  fit <- analytic_solve(Y)

  map <- fit$inv_map

  pcor <- fit$pcor_mat

  betas <- lapply(1:p, function(z) -1 * (map[z,-z] / map[z,z]) )
  betas <- do.call(rbind, betas)

  returned_object <- list(precision = round(map, 3),
                          pcor = round(pcor, 3),
                          betas = betas,
                          dat = Y)

  class(returned_object) <- c("BGGM",
                              "estimate",
                              "map")

  return(returned_object)
}

Try the BGGM package in your browser

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

BGGM documentation built on Aug. 20, 2021, 5:08 p.m.