https://github.com/cran/robCompositions
Raw File
Tip revision: d761b2fedaa3133904cf8bbd87ad4e6fcfdf79ac authored by Matthias Templ on 15 April 2019, 16:22:43 UTC
version 2.1.0
Tip revision: d761b2f
center.R
# library(robCompositions)
# data(expenditures)
# x <- expenditures
# ## still in original space reported:
# apply(x, 2, gm)
# ## this is equvalent to
# means <- matrix(apply(cenLR(x)$x.clr, 2, method), nrow=1)
# gms <- apply(x, 2, gm)
# fac <- means/gms
# means /fac
# ## but robust we need any ilr, but then to preserve the original scale is not possilbe/easy, as described at the previous mail ;)
# pivotCoordInv(matrix(covMcd(pivotCoord(x))$center, nrow=1))
# # compare
# apply(x, 2, gm)
# ## Karels suggestion to take only obs that considered finally in MCD
# mcd <- covMcd(pivotCoord(x))
# gms_rob <- apply(x[mcd$best, ], 2, gm)
# ## finished?
# gms_rob
# ## (the factor (fac) cannot be used for ilr transformed, since its length is longer (+1))
# 

center <- function(x, method = "robust"){
  if(method != "robust") centers <- apply(x, 2, gm)
  if(method == "robust"){
    z <- pivotCoord(x)
    zv <- covMcd(x)
    centers <- apply(x[zv$mcd.wt == 1, ], 2, gm)
  }
  return(centers)
}
back to top