The example is split into 2 Parts:
Part 1 must be completed first to create a file,
SNPs_cleaned.csv, that has been completely prepared for
analysis.
Now in Part 2, you will analyze the data with PCA. The steps here will be:
scale())prcomp())screeplot())vegan::scores())In the code below all code is provided. Your tasks will be to do 4 things:
Load the vcfR package with library()
library(vcfR) # KEY
##
## ***** *** vcfR *** *****
## This is vcfR 1.13.0
## browseVignettes('vcfR') # Documentation
## citation('vcfR') # Citation
## ***** ***** ***** *****
library(vegan)
## Loading required package: permute
## Loading required package: lattice
## This is vegan 2.6-4
library(ggplot2)
library(ggpubr)
Set the working directory
Load the data
SNPs_cleaned <- read.csv(file = "SNPs_cleaned.csv")
warning("If this didn't work, its may be because you didn't set your working directory.")
## Warning: If this didn't work, its may be because you didn't set your working
## directory.
TODO: we want to scale the column data that has character data to be able to work with it more easily
SNPs_scaled <- scale(SNPs_cleaned)
TODO: Run PCA using prcomp()
pca_scaled <- prcomp(SNPs_scaled)
TODO: using screeplot(), we decide how many of the new, re-engineered dimensions should be retained.
TODO: UPDATE PLOT WITH TITLE
screeplot(pca_scaled,
ylab = "Relative importance",
main = "Screeplot of PCA data")
TODO: INTERPRET SCREEPLOT the large jump from PC1 to PC2 and the very small difference between PC2 and PC3 means that there isn’t a big deal to retain PC3
TODO: create a function that calculates a percent of variation, rounds it and then plots it on a barplot. extract the information on explained variation (importance) we want. var_out using PCA variation specifies how many PCs2 we want and the code wants 10. y-axis on plot is the percent of variation captured by each PC.
summary_out_scaled <- summary(pca_scaled)
PCA_variation <- function(pca_summary, PCs = 2){
var_explained <- pca_summary$importance[2,1:PCs]*100
var_explained <- round(var_explained,1)
return(var_explained)
}
var_out <- PCA_variation(summary_out_scaled,PCs = 10)
N_columns <- ncol(SNPs_scaled)
barplot(var_out,
main = "Percent variation Scree plot",
ylab = "Percent variation explained")
abline(h = 1/N_columns*100, col = 2, lwd = 2)
TODO: INTERPRET VARIANCE EXPLAINED high variation in PC1 and then a hard drop in the percent variation after that.
TODO: using the biplot function, create a biplot using the scaled pca data
biplot(pca_scaled)
TODO: EXPLAIN WHY THIS IS A BAD IDEA its a mess, cant interpret relationship between 1000+ variables
TODO: using scores function from vegan package, get scored data
pca_scores <- vegan::scores(pca_scaled)
TODO: EXPLAIN
pop_id <- c("Nel","Nel","Nel","Nel","Nel","Nel","Nel","Nel",
"Nel", "Nel", "Nel", "Nel", "Nel", "Nel", "Nel", "Alt",
"Alt", "Alt", "Alt", "Alt", "Alt", "Alt", "Alt", "Alt",
"Alt", "Alt", "Alt", "Alt", "Alt", "Alt", "Sub", "Sub",
"Sub", "Sub", "Sub", "Sub", "Sub", "Sub", "Sub", "Sub",
"Sub", "Cau", "Cau", "Cau", "Cau", "Cau", "Cau", "Cau",
"Cau", "Cau", "Cau", "Cau", "Cau", "Div", "Div", "Div",
"Div", "Div", "Div", "Div", "Div", "Div", "Div", "Div",
"Div", "Div", "Div", "Div")
TODO: EXPLAIN
pca_scores2 <- data.frame(pop_id,
pca_scores)
TODO: create scatter plot that plots the scores with color coded species based off pop_id
TODO: UPDATE PLOT WITH TITLE TODO: UPDATE X and Y AXES WITH AMOUNT OF VARIATION EXPLAINED
ggpubr::ggscatter(data = pca_scores2,
y = "PC2",
x = "PC1",
color = "pop_id",
shape = "pop_id",
xlab = "PC1 (19.9% variation)",
ylab = "PC2 (2.3% variation)",
main = "PCA scatter plot")
TODO: INTERPRET PLOT
different pop_id grouped together indicating correlation to pc1 and PC2 and each other. ID’s that are close together are highly correlated to each other such as Alt and Nel.