Skip to content
Snippets Groups Projects
genDiag.R 18.8 KiB
Newer Older
Raimon Tolosana-Delgado's avatar
Raimon Tolosana-Delgado committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
#### MAF calculations (2 versions) ----------------
# compute the MAF basis from two matrices of (alr/ilr/clr)-covariance 
gsi.getMAFbasis = function(C0, CT, tol=1e-12){
  svdT = svd(CT)
  A1 = with(svdT, u %*% diag(ifelse(d>tol, 1/sqrt(d), 0)) )
  B = t(A1) %*% C0 %*% A1
  svdB = svd(B)
  o = nrow(svdB$u):1  
  A = A1[,o] %*% svdB$u[o,]
  return(A)
}

  
gsi.computeExplainedVariance <- function(X, Winv){
  rs = sapply(1:ncol(X), function(i) mvar(rmult(outer(X[,i], Winv[i,]))))
  return(rs)
}


#' @describeIn Maf.data.frame  Generalised diagonalisations
#' @param ... generic functionality arguments
#' @export
Maf = function(x,...) UseMethod("Maf",x)

#' Generalised diagonalisations
#' Calculate several generalized diagonalisations out of a data set and its empirical 
#' variogram
#' @param x a data set, typically of class "data.frame" or of a compositional class 
#' @param vg empirical variogram, of a kind fitting to the data provided
#' @param i a slicer for the variogram, typically this will be one or more indices of 
#' the lag distance to take. %For other options see code{getEmpVariogramSlice}.
#'
#' @return An object extending \code{c("princomp.CLASSOF(x)",}"\code{\link{princomp}}") 
#' with classes "\code{genDiag}", and an extra class argument depending on the 
#' diagonalisation method chosen.
#' 
#' Function \code{Maf} results carry the extra class "\code{maf}", and they correspond
#' to minimum/maximum autocorrelation factors (MAF) as proposed by Switzer and Green 
#' (XXXX) or XXXXX (XXXX). In this case, the
#' slicer is typically just the index of one lag distance (defaults to i=2). MAF 
#' provides the analytical solution to the joint diagonalisation of two matrices,
#' the covariance of increments provided by the slicing and the conventional covariance
#' matrix (of the idt transformed values, if appropriate). Resulting factors are ordered
#' in decreasing order of spatial continuity, which might produce surprising 
#' scree-plots for those who are used to see a screeplot of a principal component analysis.
#' 
#' Function \code{UWEDGE} (Uniformly Weighted Exhaustive Diagonalization with Gauss 
#' iterations) results carry the extra class "\code{uwedge}". The function 
#' is a wrapper on \code{jointDiag::uwedge} from package \code{jointDiag}. In this case the 
#' slicer is typically just a subset of indices of lag distances to consider 
#' (defaults to the nearest indexes to mininum, maximum and mean lag distances of the 
#' variogram). UWEDGE iteratively seeks for a pair of matrices (a mixing and a
#' demixing matrices) diagonalises the set of matrices \eqn{M_1, M_2, \ldots, M_K} 
#' given, by minimizing the target quantity 
#' \deqn{Q_{uwedge}(A, W) = \sum_{k=1}^K Tr[E_k^t\cdot E_k],}
#' where \eqn{E_k = (W^t\cdot M_k \cdot W- A\cdot \Lambda_k\cdot A^t)} and 
#' \eqn{\Lambda_k = diag(W^t\cdot M_k \cdot W)} is the resulting diagonalized version of 
#' each matrix. Obtained factors are ordered
#' in decreasing order of spatial continuity, which might produce surprising 
#' scree-plots for those who are used to see a screeplot of a principal component analysis.
#' 
#' Function \code{RJD} results carry the extra class "\code{rjd}". The function 
#' is a wrapper on \code{JADE::rjd}, implementing the Rotational joint diagonalisation method . In this case the 
#' slicer is typically just a subset of indices of lag distances to consider 
#' (defaults to the nearest indexes to mininum, maximum and mean lag distances).
#' This algorithm also served for quasi-diagonalising a set of matrices as in UWEDGE,
#' just that in this case the quantity to minimise is the sum of sequares of all off-diagonal
#' elements of \eqn{A^t\cdot M_k\cdot A} for all \eqn{k=1, 2, \ldots K}.
#' 
#' All these functions produce output mimicking \code{\link{princomp}}, i.e. with 
#' elements
#' \describe{
#'   \item{sdev}{contrary to the output in PCA, this contains the square root of the
#'   metric variance of the predictions obtained for each individual factor; this is the
#'   quantity needed for \code{\link{screeplot}} to create plots of explained variance
#'   by factor}
#'   \item{loadings}{matrix of contributions of each (cdt-transformed) original variable to the new factors}
#'   \item{center}{center of the data set (eventually, represented through \code{\link{cdt}}), 
#'   in compositional methods}
#'   \item{scale}{the scalings applied to each original variable}
#'   \item{n.obs}{number of observations}
#'   \item{scores}{the scores of the supplied data on the new factors}
#'   \item{call}{the call to the function (attention: it actually may come much later)}
#' } 
#' and additionally some of the following arguments, in different order
#' \describe{
#'   \item{invLoadings}{matrix of contributions of each factor onto each original variable}
#'   \item{Center}{compositional methods return here the cdt-backtransformed center}
#'   \item{InvLoadings}{compositional methods return here the clr-backtransformed inverse loadings, so that
#'   each column of this matrix can be understood as a composition on itself}
#'   \item{DownInvLoadings}{compositional methods return here the clr-backtransformed "minus inverse loadings", so that
#'   each column of this matrix can be understood as a composition on itself; details in 
#'   \code{\link{princomp.acomp}} }
#'   \item{C1, C2}{Maf returns the two matrices that were diagonalised}
#'   \item{eigenvalues}{Maf returns the generalized eigenvalues of the diagonalisation of C1 and C2}
#'   \item{gof}{UWEDGE returns the values of the goodness of fit criterion across sweeps}
#'   \item{diagonalized}{RJD returns the diagonalized matrices, in an array of (K,D,D)-dimensions, being
#'   D the number of variables in \code{x}}
#'   \item{type}{a string describing which package and which function was used as a workhorse for
#'   the calculation}
#' }
#'  
#' 
#' NOTE: if the arguments you provide to RJD and UWEDGE are not of the appropriate type
#' (i.e. data.frames or equivalent) the default method of these functions will just attempt
#' to call the basis functions JADE:rjd and JointDiag:uwedge respectively. 
#' This will be the case if you provide \code{x} as a "\code{matrix}", or as
#' an "\code{array}". In those cases, the output will NOT be structured as an extension
#' to princomp results; instead they will be native output from those functions.
#' 
#'  
#' @export
#' @method Maf data.frame
#' @aliases genDiag
#' 
#' @family generalised Diagonalisations
#'
#' @examples
#' require("magrittr")
#' require("gstat")
#' require("compositions")
#' data("jura", package="gstat")
#' gs = gstat(id="Cd", formula=log(Cd)~1, locations=~Xloc+Yloc, data=jura.pred) %>% 
#' gstat(id="Co", formula=log(Cd)~1, locations=~Xloc+Yloc, data=jura.pred) %>% 
#' gstat(id="Cr", formula=log(Cr)~1, locations=~Xloc+Yloc, data=jura.pred) %>% 
#' gstat(id="Cu", formula=log(Cu)~1, locations=~Xloc+Yloc, data=jura.pred) %>% 
#' gstat(id="Ni", formula=log(Ni)~1, locations=~Xloc+Yloc, data=jura.pred) %>% 
#' gstat(id="Pb", formula=log(Pb)~1, locations=~Xloc+Yloc, data=jura.pred) %>% 
#' gstat(id="Zn", formula=log(Zn)~1, locations=~Xloc+Yloc, data=jura.pred)
#' vg = variogram(gs)
#' mf = Maf(aplus(jura.pred[, -(1:6)]), vg=vg)
#' mf
#' mf$loadings
#' biplot(mf)
Maf.data.frame <- function(x, vg, i=2,...){
    cl <- match.call()
  C1 = unclass(var(idt(x)))
  mn = colMeans(cdt(x))
  C2 = 0*C1
  ## BEGIN: to change in the future to use gmEVario 
  vg = as.gstatVariogram(vg)
  #dists = unique(vg$dist)
  #vv = vg[abs(dists[i]-vg$dist)<1e-9*dists[i],]
  vv = sapply(split(vg$gamma, vg$id), function(x)x[i])
  vv = vv[order(names(vv))]
  C2[lower.tri(C2,diag=TRUE)]=vv
  dd = diag(diag(C2))
  C2 = unclass(C2 + t(C2) - dd)
  ## END: to change in the future to use gmEVario 
  
  
  dt.centred = scale(x, center = TRUE, scale=FALSE)
  eT = eigen(C1)
  A1 = with(eT, vectors %*% diag(ifelse(values>1e-12, 1/sqrt(values), 0)) ) ## C1^{-0.5}
  B = t(A1) %*% C2 %*% A1 
  eB = eigen(B)
  A2 = eB$vectors
  
  eigenvalues = eB$values
  ord = order(eigenvalues)
  A2 = A2[, ord]
  W = unclass(A1) %*% unclass(A2)
  rownames(W) = colnames(x)
  # do not normalize
  # W = unclass(t(normalize(rmult(t(W))))) 
  W = unclass(W)
  Winv = solve(W)
   colnames(Winv) = colnames(W)
  facscores = unclass(dt.centred) %*% W
  colnames(facscores) = paste("maf", 1:ncol(facscores), sep="")
  sdevs = sqrt(gsi.computeExplainedVariance(facscores, Winv))
  res = list(sdev=sdevs, loadings = W, center=mn, scale=rep(1, ncol(x)),
             n.obs=nrow(x), scores = facscores, C1=C1, C2=C2, eigenvalues = eigenvalues[ord], 
             invLoadings=Winv)
  if(class(x)!="data.frame"){
    res$Center = cdtInv(mn)
    res$InvLoadings = cdtInv(Winv, orig=x)
    res$InvDownLoadings = cdtInv(-Winv, orig=x)
  }
  res$call = cl
  res$type="gmGeostats::MAF"
  class(res)= c("maf","genDiag",paste("princomp", class(x), sep="."), "princomp")
  return(res)
}

#' @describeIn Maf.data.frame  Generalised diagonalisations
#' @method Maf rmult
#' @export
Maf.rmult <- Maf.data.frame

#' @describeIn Maf.data.frame  Generalised diagonalisations
#' @method Maf aplus
#' @export
Maf.aplus <- Maf.data.frame

#' @describeIn Maf.data.frame  Generalised diagonalisations
#' @method Maf rplus
#' @export
Maf.rplus <- Maf.data.frame

#' @describeIn Maf.data.frame  Generalised diagonalisations
#' @method Maf ccomp
#' @export
Maf.ccomp <- Maf.data.frame


#' @describeIn Maf.data.frame  Generalised diagonalisations
#' @method Maf rcomp
#' @export
Maf.rcomp  <- function(x, vg, i=2,...){
  requireNamespace("compositions", quietly=TRUE)
  cl <- match.call()
  C1 = unclass(clrvar2ilr(cov(x)))
  C2 = unclass(clrvar2ilr(variation2clrvar(vg$vg[i,,]))) 
  clr.centred = scale(cdt(x), center = TRUE, scale=FALSE)
  
  eT = eigen(C1)
  A1 = with(eT, vectors %*% diag(ifelse(values>1e-12, 1/sqrt(values), 0)) ) ## C1^{-0.5}
  B = t(A1) %*% C2 %*% A1 
  eB = eigen(B)
  A2 = eB$vectors
  
  eigenvalues = eB$values
  ord = order(eigenvalues)
  A2 = A2[, ord]
  W = unclass(A1) %*% unclass(A2)
  # do not normalize
  #W = unclass(t(normalize(rmult(t(W))))) 
  Wclr = unclass(t(ilr2clr(t(W))))
  rownames(Wclr) = colnames(x)
  Winv = solve(W)
  Wclr.inv = unclass(ilr2clr(Winv))
    colnames(Wclr.inv) = colnames(x)
  facscores = unclass(clr.centred) %*% Wclr
  colnames(facscores) = paste("maf", 1:ncol(facscores), sep="")
  sdevs = sqrt(gsi.computeExplainedVariance(facscores, Wclr.inv))
  res = list(sdev=sdevs, loadings = Wclr, center=mean(cdt(x),...), scale=rep(1, ncol(x)),
             n.obs=nrow(x), scores = facscores, 
             C1=C1, C2=C2, eigenvalues = eigenvalues[ord], invLoadings=Wclr.inv,
             Center = mean(x, ...),
             InvLoadings = cdtInv(Wclr.inv, orig=x),
             InvDownLoadings = cdtInv(-Wclr.inv, orig=x),
             call = cl, type="gmGeostats::MAF")
  kkk = paste("princomp", class(x), sep=".")
  class(res)= c("maf","genDiag", kkk,  "princomp")
  return(res)
} 

#' @describeIn Maf.data.frame  Generalised diagonalisations
#' @method Maf acomp
#' @export
Maf.acomp <- Maf.rcomp



#### UWEDGE calculations ----------

#' @describeIn Maf.data.frame  Generalised diagonalisations
#' @export
UWEDGE = function(x,...){
  if(!requireNamespace("jointDiag", quietly=FALSE)) stop("UWEDGE requires package 'jointDiag' installed")
  UseMethod("UWEDGE",x)
}

#' @describeIn Maf.data.frame  Generalised diagonalisations
#' @method UWEDGE default
#' @export
UWEDGE.default <- function(x, ...) jointDiag::uwedge(M=x, ...)

#' @describeIn Maf.data.frame  Generalised diagonalisations
#' @method UWEDGE acomp
#' @export
UWEDGE.acomp <-  function(x, vg, i=NULL, ...){
  requireNamespace("compositions", quietly=TRUE)
  cl <- match.call()
  vg = as.logratioVariogram(vg) 
  if(is.null(i)){
    i = c(1, floor(c(0.5,1)*dim(vg$vg)[1]))
  }
  V = loadings(princomp(x))
  C1 = clrvar2ilr(cov(x), V = V)
  d = ncol(x)-1
  M = -0.5*apply(vg$vg[c(1,i),,], 1, clrvar2ilr, V=V )
  dim(M)=c(d,d,length(i)+1)
  M[,,1]=C1
  res = jointDiag::uwedge(M)
  
  ord = order(diag(res$B %*% M[,,length(i)] %*% t(res$B)))
  res$B = res$B[ord,]
  W = t(res$B)
  # do not normalize
  # W = unclass(t(normalize(rmult(t(W))))) 
  W = unclass(W)
  ilrx = ilr(x, V=V)
  mns = mean(ilrx)
  ilrxc = ilrx-mns
  facscores = unclass(ilrxc) %*% W
  Wclr = V %*% W
  rownames(Wclr) = colnames(x)
  colnames(Wclr) <- paste("uwedge", 1:d, sep="")
  Wclr.inv = gsiInv(Wclr)
  colnames(Wclr.inv) = colnames(x)
  colnames(facscores) <- paste("uwedge", 1:d, sep="")
  sdevs = sqrt(gsi.computeExplainedVariance(facscores, Wclr.inv))
  out = list(sdev=sdevs, loadings = Wclr, center=mean(cdt(x)), 
             scale=rep(1, ncol(x)),
             n.obs=nrow(x), scores = facscores, 
             gof=res$criter, invLoadings=Wclr.inv,
             Center = mean(x),
             InvLoadings = cdtInv(Wclr.inv, orig=x),
             InvDownLoadings = cdtInv(-Wclr.inv, orig=x),
             call = cl, type="jointDiag::uwedge")
  kkk = paste("princomp", class(x), sep=".")
  class(out)= c("uwedge","genDiag", kkk,  "princomp")
  return(out)
}  

#' @describeIn Maf.data.frame  Generalised diagonalisations
#' @method UWEDGE rcomp
#' @export
UWEDGE.rcomp <- UWEDGE.acomp




### RJD calculation -----------------------

#' @describeIn Maf.data.frame  Generalised diagonalisations
#' @export
RJD = function(x,...){
  if(!requireNamespace("JADE", quietly=FALSE)) stop("RJD requires package 'JADE' installed")
  UseMethod("RJD",x)
}

#' @describeIn Maf.data.frame  Generalised diagonalisations
#' @method RJD default
#' @export
RJD.default <- function(x, ...){
  JADE::rjd(X=x, ...)
} 


#' @describeIn Maf.data.frame  Generalised diagonalisations
#' @method RJD acomp
#' @export
RJD.acomp <- function(x, vg, i=NULL,  ...){
  requireNamespace("compositions", quietly=TRUE)
  cl <- match.call()
  vg = as.logratioVariogram(vg)
  if(is.null(i))
    i = c(1, floor(c(0.5,1)*dim(vg$vg)[1]))
  d = ncol(x)-1
  i = sort(i,decreasing=TRUE)
  V = loadings(princomp(x))
  M = -0.5*apply(vg$vg[i ,,], 1, clrvar2ilr, V=V )
  dim(M)=c(d,d,length(i))
  res = JADE::rjd(M)
  res$B = t(res$V) 
  
  ord = order(diag(res$B %*% M[,,2] %*% t(res$B)))
  res$B = res$B[ord,]
  W = t(res$B)
  # do not normalize
  # W = unclass(t(normalize(rmult(t(W))))) 
  W = unclass(W)
  ilrx = ilr(x, V=V)
  mns = mean(ilrx)
  ilrxc = ilrx-mns
  facscores = unclass(ilrxc) %*% W
  Wclr = V %*% W
  rownames(Wclr) = colnames(x)
  Wclr.inv = t(Wclr)
  colnames(Wclr.inv) = colnames(x)
  sdevs = sqrt(gsi.computeExplainedVariance(facscores, Wclr.inv))
  o = order(sdevs, decreasing = TRUE)
  Wclr = Wclr[,o]
  facscores = facscores[,o]
  Wclr.inv = Wclr.inv[o,]
  colnames(Wclr) <- rownames(Wclr.inv) <- paste("rjd", 1:d, sep="")
  colnames(facscores) <- paste("rjd", 1:d, sep="")
  out = list(sdev=sdevs[o], loadings = Wclr, center=mean(cdt(x)), 
             scale=rep(1, ncol(x)),
             n.obs=nrow(x), scores = facscores, 
             diagonalized = res$D[o,o,], invLoadings=Wclr.inv,
             Center = mean(x),
             InvLoadings = cdtInv(Wclr.inv, orig=x),
             InvDownLoadings = cdtInv(-Wclr.inv, orig=x),
             call = cl, type="JADE::rjd")
  kkk = paste("princomp", class(x), sep=".")
  class(out)= c("rjd","genDiag", kkk,  "princomp")
  return(out)
}  

#' @describeIn Maf.data.frame  Generalised diagonalisations
#' @method RJD rcomp
#' @export
RJD.rcomp <- RJD.acomp


### genDiag methods ----

#' Predict method for generalised diagonalisation objects
#'
#' @param object a generalized diagonalisation object, as obtained from a call to
#' \code{\link{Maf}}, and on the same page, information on the other diagonalisation
#' methods \code{UWEDGE} or \code{RJD}
#' @param newdata a matrix or data.frame of factor scores to convert back to the original
#' scale (default: the scores element from `object`)
#' @param ... not used, kept for generic compatibility
#'
#' @return A data set or compositional object of the nature of the original data
#' used for creating the genDiag object.
#' @export
#' @family generalised Diagonalisations
#' @examples
#' data("jura", package="gstat")
#' juracomp = compositions::acomp(jura.pred[, -(1:6)]) 
#' lrvg = logratioVariogram(data=juracomp, loc=jura.pred[,1:2])
#' mf = Maf(juracomp, vg=lrvg)
#' mf
#' biplot(mf)
#' predict(mf) 
#' unclass(predict(mf)) - unclass(juracomp) # predict recovers the original composition
predict.genDiag = function (object, newdata=NULL, ...) {
  requireNamespace("compositions", quietly=TRUE)
  if(is.null(newdata))
    newdata = object$scores
  if("data.frame" %in% class(newdata))
    newdata = as.matrix(newdata)
  Z = newdata %*% unclass(object$invLoadings)
  if("Center" %in% names(object)){
    Z = cdtInv(Z, orig = object$Center) + object$Center
  }else{
    Z = Z + outer(rep(1, nrow(Z)), object$center)
    Z = data.frame(Z)
    colnames(Z) = names(object$center)
  }
  return(Z)
}



#' Colored biplot for gemeralised diagonalisations
#' Colored biplot method for objects of class genDiag
#' @param x a generalized diagonalisation object, as obtained from a call to
#' \code{\link{Maf}} (or to \code{UWEDGE} or \code{RJD}, on the help page of \code{\link{Maf}}).
#' @param choices which factors should be represented? vector of 2 indices; defaults to
#' c(1,2)
#' @param scale deprecated, kept for coherence with \code{link{biplot.princomp}}
#' @param pc.biplot same as the homonimous argument from \code{link{biplot.princomp}}:
#'  boolean, to scale variables down by sqrt(n) and observations up by the same factor. 
#' @param ... further arguments to \code{\link{coloredBiplot}}
#'
#' @return nothing. Function is called exclusively to produce the plot
#' @export
#' @family generalised Diagonalisations
#' @importFrom compositions coloredBiplot
#' @method coloredBiplot genDiag
#'
#' @examples
#' data("jura", package="gstat")
#' juracomp = compositions::acomp(jura.pred[, -(1:6)]) 
#' lrvg = logratioVariogram(data=juracomp, loc=jura.pred[,1:2])
#' mf = Maf(juracomp, vg=lrvg)
#' mf
#' compositions::coloredBiplot(mf, xlabs.col=as.integer(jura.pred$Rock)+2)
coloredBiplot.genDiag <- function(x, choices = 1:2, scale=0, pc.biplot=FALSE, ...){
  if(length(choices) != 2) stop("length of choices must be 2")
  if(!length(scores <- x$scores))
    stop(gettextf("object '%s' has no scores", deparse(substitute(x))),
         domain = NA)
  lam <- rep(1, along.with=choices)
  if(is.null(n <- x$n.obs)) n <- 1
  lam <- lam * sqrt(n)
  if(scale < 0 || scale > 1) warning("'scale' is outside [0, 1]")
  if(scale != 0) lam <- lam^scale else lam <- 1
  if(pc.biplot) lam <- lam / sqrt(n)
  coloredBiplot.default(t(t(scores[,choices]) / lam),
                         t(x$invLoadings[choices,] * lam), ...)
  invisible()
}