R/meanvar.R

Defines functions meanvar.mtrunmnt meanvar

Documented in meanvar

#' Mean and variance for a truncated multivariate normal distribution.
#' 
#' \code{meanvar} is a S3 generic function of the class \code{\link{mtrunmnt}}. 
#'     Using the \code{\link{prodmnt}}, it compute the mean and 
#'     variance-covariance matrix for a truncated multivariate normal 
#'     distribution. 
#'     
#' @param Obj An mtrunmnt object created by the \code{\link{mtrunmnt}}.
#'     
#' @seealso \code{\link{mtrunmnt}}, \code{\link[MomTrunc]{meanvarTMD}}, 
#'     \code{\link[tmvtnorm]{mtmvnorm}} and \code{\link{prodmnt}}.
#'     
#' @return A list with the mean and variance-covariance matrix.
#' 
#' @export
#' 
#' @examples
#' ###  A simple example ####
#' 
#' set.seed(123)
#' sigma2e <- 1
#' sigma2a <- 2
#' n <- 5
#' mu <- seq(-1,1, length.out = n)
#' y <- mu + rnorm(1, sd = sqrt(sigma2a)) + rnorm(n, sd = sqrt(sigma2e))
#' a  <- rep(-Inf, n)
#' b  <- rep(Inf, n)
#' a[y >= 0] <- 0
#' b[y <  0] <- 0
#' obj <- mtrunmnt(mu, lower = a, upper = b, Sigmae = sigma2e, D = sigma2a) 
#' meanvar(obj)
#' 
meanvar <- function(Obj) {
  UseMethod("meanvar")  
}  

#' @export
meanvar.mtrunmnt <- function(Obj){
  MeanVar <- getMeanVar(Obj$lower, Obj$upper, Obj$mu, Obj$sigmae, Obj$probab, Obj$Z, Obj$D, Obj$weights, Obj$nodes)
  MeanVar$tmean <- as.numeric(MeanVar$tmean)
  return(MeanVar)
}

Try the trunmnt package in your browser

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

trunmnt documentation built on Dec. 1, 2025, 5:09 p.m.