https://github.com/cran/robCompositions
Raw File
Tip revision: b0cf7e2c31b281933351acf2fd497c40e4e77996 authored by Matthias Templ on 08 February 2016, 15:39:39 UTC
version 2.0.0
Tip revision: b0cf7e2
variation.R
#' Robust and classical variation matrix
#' 
#' Estimates the variation matrix with robust methods.
#' 
#' The variation matrix is estimated for a given compositional data set.
#' Instead of using the classical standard deviations the
#' \code{\link[stats]{mad}} is used when parameter robust is set to TRUE.
#' 
#' @param x data frame or matrix with positive entries
#' @param robust if FALSE, standard measures are used.
#' @return The (robust) variation matrix.
#' @author Matthias Templ
#' @references Aitchison, J. (1986) \emph{The Statistical Analysis of
#' Compositional Data} Monographs on Statistics and Applied Probability.
#' Chapman \& Hall Ltd., London (UK). 416p.
#' @keywords multivariate robust
#' @export
#' @examples
#' 
#' data(expenditures)
#' variation(expenditures)
#' variation(expenditures, robust=FALSE)
#' 
`variation` <-
  function(x, robust=TRUE){
    rvars <- matrix(0, ncol=ncol(x), nrow=ncol(x))
    if(robust){
      for( i in 1:ncol(x)){
        for( j in 1:ncol(x)){
          if( i < j ){
            rvars[i,j] <- (mad(log(x[,i]/x[,j])))^2
            rvars[j,i] <- rvars[i,j]
          }
        }
      }
    } else{
      for( i in 1:ncol(x)){
        for( j in 1:ncol(x)){
          if( i < j ){ 
            rvars[i,j] <- (var(log(x[,i]/x[,j])))
            rvars[j,i] <- rvars[i,j]            
          }
        }
      }		
    }
    return(rvars) 
}
back to top