vegan icon indicating copy to clipboard operation
vegan copied to clipboard

Function to return percent inertia explained for cca.objects

Open MikkoVihtakari opened this issue 6 years ago • 4 comments

First, I want to thank for making such a great package for multivariate and ecological community analyses! I use vegan a lot in my work. One thing I constantly miss is the possibility to quickly glance the inertia explained by axes in CA and CCA plots. Therefore, I normally have to modify the plots. Something that takes time, if you want to mass produce biplots to look at your data from several different angles. I thought of sharing my solution here in the hope that something similar would appear in the package in the future :)

#' @title Get percent of total inertia explained by principal or constrained axes
#' @param mod cca.object
#' @param axes A numeric vector indicating the axes for which percent explained inertia should be returned for
#' @return Returns a named vector containing explained inertia as percentages. Returns constrained inertia fo CCA and unconstrained for CA

axis.expl <- function(mod, axes = 1:2) {
  
  if(is.null(mod$CCA)) {
    sapply(axes, function(i) {
    100*mod$CA$eig[i]/mod$tot.chi
    })
  } else {
    sapply(axes, function(i) {
    100*mod$CCA$eig[i]/mod$tot.chi
    })
  }
    
}

## Lets try it:

data("dune")

x <- cca(dune)

(labs <- axis.expl(x))

#      CA1      CA2 
# 25.33987 18.91696 

## Add to plot

plot(x, xlab = "", ylab = "")

title(xlab = paste0(names(labs[1]), " (", sprintf("%.1f", labs[1]), "%)"))
title(ylab = paste0(names(labs[2]), " (", sprintf("%.1f", labs[2]), "%)"))

If there already is such a feature, please correct me :)

MikkoVihtakari avatar Feb 23 '18 12:02 MikkoVihtakari