## importing the file and specifying which columns are species and which are environmental

library(vegan)

AB_Climate_Trees=read.csv("AB_Climate_Trees.csv")
fix(AB_Climate_Trees)
row.names(AB_Climate_Trees)=AB_Climate_Trees$ECOSYS 
species=AB_Climate_Trees[,11:23]
environment=AB_Climate_Trees[,3:10]
fix(species) 
fix(environment)



## adding a small constant to all values since analysis cannot handle rows with all zeros

species001=(species + 0.001) 
fix(species001)



## one method for deciding what distance measure to use

rankindex(environment, species001, indices = c("euc", "man", "gow", "bra", "kul"),stepacross= FALSE, method = "spearman")



## running the analysis

dbRDA=capscale(species001 ~ MAT+MWMT+MCMT+TD+lnMAP+lnMSP+lnAHM+lnSHM, environment, dist="bray")
plot(dbRDA)
anova(dbRDA)


## testing for significance

anova(dbRDA)              ## overall test of the significance of the analysis
anova(dbRDA, by="axis", perm.max=500)           ## test axes for significance
anova(dbRDA, by="terms", permu=200)  ## test for sig. environmental variables


## getting the scores

scores.rda(dbRDA)             ## getting the scores from the analysis; notice species and sites are together
scores_dbRDA=scores.rda(dbRDA)     

site_scores=scores_dbRDA$sites     ## separating out the site scores; get CAP1 and CAP2 scores
fix(site_scores)
species_scores=scores_dbRDA$species   ## separating out the species scores
fix(species_scores)


## calculating loadings/environmental correlations with the axes

site_scores_environment=cbind(site_scores,environment) ## merge 
correlations=cor(site_scores_environment)    ## calculate correlations
fix(correlations)              
correlations2=correlations[3:10,1:2] ## the loadings we are interested in
fix(correlations2)

correlations3=correlations2[1:3,1:2]    ## these environmental variables are the only significant ones
fix(correlations3)


