R/calculateVarExp.R

Defines functions calculateVarExp

Documented in calculateVarExp

#' Calculate the variation explained by a PARAFAC model.
#'
#' @param Fac Fac object output from the [parafac()] function.
#' @param X Input data of the PARAFAC model.
#'
#' @return The variation explained by the model, expressed as a fraction (between 0-1).
#' @export
#'
#' @examples
#' X = Fujita2023$data
#' model = parafac(X, nfac=1, nstart=1, verbose=FALSE)
#' calculateVarExp(model$Fac, X)
calculateVarExp = function(Fac, X){
  Fac = lapply(Fac, as.matrix) # protection from the 1-component case

  Xhat = reinflateTensor(Fac[[1]], Fac[[2]], Fac[[3]])
  Xhat[is.na(X)] = 0 # Replace imputed values with 0 to avoid adding them to SSQ
  varExp = multiway::sumsq(Xhat, na.rm=TRUE) / multiway::sumsq(X, na.rm=TRUE)
  return(varExp)
}

Try the parafac4microbiome package in your browser

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

parafac4microbiome documentation built on June 8, 2025, 11:40 a.m.