R/summary.mc.R

Defines functions print.summary.mc summary.mcnode summary.mc

Documented in print.summary.mc summary.mc summary.mcnode

#' Summary of mcnode and mc Object
#'
#' Provides a summary of a \samp{mcnode}, a \samp{mc} or a \samp{mccut} object.
#'
#' The mean, the standard deviation and the \samp{probs} quantiles will be evaluated in the
#' variability dimension. The median, the mean and the \samp{lim} quantiles will then be
#' evaluated on these statistics in the uncertainty dimension.
#'
#' For multivariate nodes: if the \samp{"outm"} attribute is \samp{"none"}, the node is not
#' evaluated; if it is \samp{"each"}, the variates are evaluated one by one; if it is a function
#' name (e.g. \samp{"mean"}), that function is applied on the variates dimension before output.
#'
#' @param object a \samp{mcnode}, \samp{mc} or \samp{mccut} object.
#' @param x a \samp{summary.mc} object as returned by the \samp{summary.mc} function.
#' @param probs a vector of values used for the quantile function (variability dimension).
#' @param lim a vector of values used for the quantile function (uncertainty dimension).
#' @param digits number of digits in the print.
#' @param ... for generic function consistency.
#' @return a list.
#' @seealso \code{\link{mcnode}} for mcnode objects, \code{\link{mc}} for mc objects,
#'   \code{\link{mccut}} for mccut objects, \code{\link{quantile}}.
#' @keywords univar
#' @name summary.mc
#' @examples
#' data(total)
#' summary(xVUM3)
#' summary(total)
#' @export
summary.mc <- function(object,probs = c(0,0.025,0.25,0.5,0.75,0.975,1),lim=c(0.025,0.975),...)
{
  yaprob <- length(probs) > 0
  if(yaprob) {nom1 <- paste(probs*100,"%",sep="")
  	nom1[probs==0] <- "Min"
  	nom1[probs==1] <- "Max"} else nom1 <- NULL
	nom1 <- c("mean","sd",nom1,"nsv","Na's")

  yalim <- length(lim) > 0
  if(yalim) nom2 <- paste(lim*100,"%",sep="") else nom2 <- NULL
  nom2 <- c("median","mean",nom2)

  warnna <- FALSE
  stat1 <- function(x){	# x is a array(nvar, nunc, 1)
    # column by column: no copy of the whole array (as with apply())
    nr <- dim(x)[1]
    nc <- dim(x)[2]
    col <- function(j) x[(j - 1L) * nr + seq_len(nr)]
    nsimna <- vapply(seq_len(nc), function(j) sum(is.na(col(j))), 0L)
    if(yaprob) quant <- t(vapply(seq_len(nc), function(j) quantile(col(j), na.rm=TRUE, probs=probs, names = FALSE), numeric(length(probs))))
      else quant <- NULL
    if(warnna==FALSE && any(nsimna!=0)) {
        warning("NA's value in mc object",call.=FALSE)
        warnna <<- TRUE}

        cbind(    colMeans(x,na.rm=TRUE),
                  vapply(seq_len(nc), function(j) sd(col(j), na.rm=TRUE), 0),
                  quant,
                  rep.int(nr, nc),
                  nsimna)
     }

    stat2 <- function(output){
        output <- rbind( rowMeans(output,na.rm=TRUE),
                         apply(output,1,quantile,na.rm=TRUE,probs=c(0.5,lim),names = FALSE))
   	    output[c(1,2),] <- output[c(2,1),]
   	    return(output)
     }

  outm <- lapply(object,attr,which="outm")
  object <- object[outm != "none",drop=FALSE]
  if(length(object)==0) return(NULL)
  outm <- lapply(object,attr,which="outm")
  nom <- names(object)
  typen <- sapply(object,attr,which="type")
  dimm <- sapply(object,dim)
  nvariates <- dimm[3,]

    # Deal with multivariate nodes

  gerout <- function(node,name,outm,nvariates){
    if(outm[1]=="each"){
      if(nvariates==1) return(node)
      return(lapply(1:nvariates,function(x) node[,,x,drop=FALSE]))
    }
    res <- lapply(outm, function(outm) .outm_reduce(node, outm))
    names(res) <- outm
    if(length(outm)==1) return(res[[1]])
    return(res)
  }

  object <- mapply(gerout,object,nom,outm,nvariates,SIMPLIFY=FALSE)

  # Function for stat

  lesstat <-function(object,typen){

        if(is.list(object)) return(mapply(lesstat,object,typen,SIMPLIFY=FALSE))

        if(typen=="VU") {
          out <- stat2(t(stat1(object)))          # object has one variate here
          nomtmp <- list(nom2,nom1)}

        else if(typen=="V") {
          out <- stat1(object)
          nomtmp <- list("NoUnc",nom1)}

        else if(typen=="U") {
          out <- stat2(object[,,1,drop=FALSE])
          nomtmp <- list(nom2,"NoVar")}

        else if(typen=="0") {
          out <- matrix(object[,,1])
          nomtmp <- list("NoVar","NoUnc")
        }
      dimnames(out) <- nomtmp
      return(out)
      }

  output <- mapply(lesstat,object,typen,SIMPLIFY=FALSE)
  output <- mapply("attr<-",output,"type",typen,SIMPLIFY=FALSE)

	class(output) <- c("summary.mc","listof")

  return(output)
}

#' @rdname summary.mc
#' @export
summary.mcnode <- function(object,probs = c(0,0.025,0.25,0.5,0.75,0.975,1),lim=c(0.025,0.975),digits = 3,...)
{
  summary.mc(list(node=object), probs = probs, lim=lim, digits = digits,... )
}

#' @rdname summary.mc
#' @export
print.summary.mc <- function(x,digits=3,...)
{
  x <- lapply(x,function(y) if(is.list(y)) lapply(y,"attr<-","type",NULL) else {attr(y,"type") <- NULL;y})
  NextMethod(x,digits=digits,...)
  }

Try the mc2d package in your browser

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

mc2d documentation built on Oct. 23, 2026, 1:06 a.m.