R/envMarker.R

Defines functions envMarker

Documented in envMarker

#'@title  Covariable matrices as environmental markers
#'
#' @description Builds covariables matrices using different biological criteria
#' @author Germano Martins F Costa Neto <germano.cneto@usp.br>
#' @param .dfcov Kernel of environmental covariates (see envK function)
#' @param .digits output digits
#' @param .format Criteria for elaboration of the environmental mark: 101 format (same as molecular marker); rel-01 (scaled to mean 0 and variance 1) and PCA (PCA strcuturec)
#' @param .ncp defines the number of principal components to be used when .param = PCA
#' @importFrom FactoMineR PCA
#' @seealso envK
#' @seealso envKernel

envMarker =function(.dfcov,
                   .digits,
                   .format=c("101","rel-01","PCA"),
                   .ncp){
  #' df.cov : dataframe of environmental covariates
  #' digits: number of output digits
  #' format: environmental marker (012),
  #'         centering and scaled to mean 0 and variance 1 (rel-01)
  #'         principal component analysis (PCA)
  #' ncp   : number of principal components used (valid only for format="PCA")
  if(is.null(.digits)){.digits=0}
  if(is.null(.dfcov)) {stop(".dfcov is missing")}
  if(is.null(.ncp))   {.ncp = 2}

  COVARIATES <- .df.cov

  if(.format == "rel-01"){
    cat("Environmental Marker rel-01/n")
    COVARIATES <- as.matrix(round(scale(COVARIATES,scale = T,center = T),.digits))
    return(W=COVARIATES)
  }

  if(.format == "101"){
    cat("Environmental Marker 101/n")
    COVARIATES<-round(apply(COVARIATES,2,function(X) scales::rescale(x=X,to=c(-1,1))),.digits)
    return(W=COVARIATES)}

  if(.format == "PCA"){
    cat("Environmental Marker PCA/n")
    res.pca <- FactoMiner::PCA(COVARIATES,graph=FALSE)
    COVARIATESp <- res.pca$svd$U[,1:.ncp]
    rownames(COVARIATESp) <- row.names(COVARIATES)
    return(list(W=COVARIATESp,eig=res.pca$eig))}
}
gcostaneto/envirotype documentation built on Feb. 19, 2020, 10:36 p.m.