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
## Warning: package 'vcfR' was built under R version 4.2.2
##
## ***** *** 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-2
library(ggplot2)
library(ggpubr)
Set the working directory
setwd("C:/Users/saman/Desktop/Comp Bio")
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: Scale the data using scale(). When scaling the data this means the SD becomes 1 and the mean become 0.
SNPs_scaled <- scale(SNPs_cleaned)
TODO: Use prcomp() to run PCA on the scaled data. PCA is principal component analysis to reduce the dimensions of the data.
pca_scaled <- prcomp(SNPs_scaled)
TODO: Use screeplot() to visualize the data to see the variance in the different dimensions
TODO: UPDATE PLOT WITH TITLE
screeplot(pca_scaled,
ylab = "variance",
main = "Scree plot of PCA SNPs")
TODO: INTERPRET SCREEPLOT - There is little variance within PC2-PC10 but a lot of variance within PC1
TODO: Use summary() to get data summary and save in object called “summary_out_scaled”
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 - again there is little variance between PC2-PC10 but a lot of variance within PC1 with a percent variance of over 15%.
TODO: Use biplot() to create a biplot of the data
biplot(pca_scaled)
TODO: EXPLAIN WHY THIS IS A BAD IDEA - this is a bad idea because there is so much data that the viewer is unable to see the relationship between the data
TODO: Use the vegan package scores() to score the data
pca_scores <- vegan::scores(pca_scaled)
TODO: Create a vector of the population IDs
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: Create a dataframe with data.frame() of the population IDs with the PCA scores
pca_scores2 <- data.frame(pop_id,
pca_scores)
TODO: Use ggscatter to create a scatter plot of the data to view relations between PC1, PC2, and population IDs
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 = "Amount of varitation explained",
ylab = "Amount of varitaion explained",
main = "PC1 Vs PC2 based on population ID")
TODO: INTERPRET PLOT - There is one clear cluster which shows that there is a similarity in the amount of variance in PC1 and PC2 within the Cau and Div populations. There is a potential cluster between Alt and Nel while Sub appears to be in its own cluster.