```{asis, echo = {{espace_pca_knit}}, eval = {{espace_pca_knit}}, include = {{espace_pca_knit}}}
### Environmental space 
Performing and plotting principal component analysis to reduce dimensionality of environmental space for  *`r "{{spName1}}"`* & *`r "{{spName2}}"`*. PCA done for {{pcaPlotSel_rmd}}.
```

```{r, echo = {{espace_pca_knit}}, include = {{espace_pca_knit}}}
# Determine the variables to use
pcaSel_{{multAbr}} <- {{pcaSel_rmd}}
# Run the pca
espace_pca_{{multAbr}} <- espace_pca(
  sp.name1 = "{{spName1}}",
  sp.name2 = "{{spName2}}", 
  occs.z1 = occs_{{spAbr1}}[,pcaSel_{{multAbr}}],
  occs.z2 = occs_{{spAbr2}}[,pcaSel_{{multAbr}}],
  bgPts.z1 = bgEnvsVals_{{spAbr1}}[,pcaSel_{{multAbr}}],
  bgPts.z2 = bgEnvsVals_{{spAbr2}}[,pcaSel_{{multAbr}}])

## Generate plots
# PCA Scatter Plot
if ("{{pcaPlotSel_rmd}}" == "occs") {
  x <- espace_pca_{{multAbr}}$scores[espace_pca_{{multAbr}}$scores$bg == 'sp', ]
  x.f <- factor(x$sp)
} else if ("{{pcaPlotSel_rmd}}" == "occsBg") {
  x <- espace_pca_{{multAbr}}$scores[espace_pca_{{multAbr}}$scores$sp == 'bg', ]
  x.f <- factor(x$bg)
}
ade4::s.class(x, x.f, xax = {{pc1_rmd}}, yax = {{pc2_rmd}},
              col = c("red", "blue"), cstar = 0, cpoint = 0.1)
title(xlab = paste0("PC", {{pc1_rmd}}), ylab = paste0("PC", {{pc2_rmd}}))
# PCA Correlation circle
ade4::s.corcircle(espace_pca_{{multAbr}}$co, xax = {{pc1_rmd}}, yax = {{pc2_rmd}},
                  lab = pcaSel_{{multAbr}}, full = FALSE, box = TRUE)
      title(xlab = paste0("PC", {{pc1_rmd}}),
            ylab = paste0("PC", {{pc2_rmd}}))
# PCA screeplot
screeplot(espace_pca_{{multAbr}}, main = NULL)
# Print PCA summary of results
summary(espace_pca_{{multAbr}})
```
