#Author: Tara Gianoulis
#Goal: Metrics to to evaluate CCA.
#Assumes CCA object is called ccobject (later turn into functions)
#and follows structure from regCCA output
#Variance extracted
#Purpose: amount of variance accounted for in each variable
#Pseudocode: first take canonical factor loadings (correlation between caninonical variate and original variable) (hereto referred to as C)and square them
varExtracted_Environment<-ccobject$scores$corr.X.xscores^2
varExtracted_Sequence<-ccobject$scores$corr.Y.xscores^2
#To get a sense across all variables, average the above squares of canonical factor loadings (C^2)
avg_varExtracted_Environment<-apply(varExtracted_Environment,2,FUN=mean)
avg_varExtracted_Sequence<-apply(varExtracted_Sequence,2,FUN=mean)
#Redundancy analysis
#Purpose: how much does one set of variables explain in another
#Not symmetric, needs to be performed for both matrices
#Pseudocode: sum(C^2)/(number of variables)*Rc^2
#where Rc^2 is simply the canonical correlation coefficients (square roof eigen value) squared
#Hack way to get number of variables can't find R syntax for matrix dimensions
redundancy_Environment<-colSums(ccobject$scores$corr.X.xscores^2/length(ccobject$xcoef[,1]))*ccobject$cor^2
redundancy_Sequence<-colSums(ccobject$scores$corr.Y.xscores^2/length(ccobject$ycoef[,1]))*ccobject$cor^2
#Can compute the average redundancy by simply averaging redundancy across all (or only first significant) roots for single index of redundancy
avg_redundancy_dim1=(redundancy_Environment[1]+redundancy_Sequence[1])/2
avg_redundancy_dim12=(redundancy_Environment[1]+redundancy_Environment[2]+redundancy_Sequence[1]+redundancy_Sequence[2])/4