Program to perform Principal component analysis (PCA) on the dataset in R PROGRAM:
a. Implement PCA on IRIS Dataset. Also compare our results by calculating eigenvectors and eigenvalues separately
# Taking the numeric part of the IRIS data data_iris <- iris[1:4]
# Calculating the covariance matrix Cov_data <- cov(data_iris )
# Find out the eigenvectors and eigenvalues using the covariance matrix Eigen_data <- eigen(Cov_data)
# Using the inbuilt function
PCA_data <- princomp(data_iris ,cor="False")
# Let’s now compare the output variances Eigen_data$values
PCA_data$sdev^2
# Compare the eigenvectors of both models. PCA_data$loadings[,1:4]
Eigen_data$vectors summary(PCA_data) biplot (PCA_data)
screeplot(PCA_data, type="lines")
# Fit two naive Bayes models. one over the entire data. The second on the first principal component. We will calculate the difference in accuracy between these two models. Building two models.
#Select the first principal component for the second model model2 = PCA_data$loadings[,1]
#For the second model, we need to calculate scores by multiplying our loadings with the data
model2_scores <- as.matrix(data_iris) %*% model2
#Loading libraries for naiveBayes model library(class)
library(e1071)
#Fitting the first model over the entire data mod1<-naiveBayes(iris[,1:4], iris[,5])
#Fitting the second model using the first principal component mod2<-naiveBayes(model2_scores, iris[,5])
# Accuracy for the first model table(predict(mod1, iris[,1:4]), iris[,5])
# Accuracy for the second model table(predict(mod2, model2_scores), iris[,5])
b. Download wine dataset from https://archive.ics.uci.edu/ml/index.php and perform PCA on it
wine <- read.table("http://archive.ics.uci.edu/ml/machine-learning- databases/wine/wine.data", sep=",")
# Name the variables
colnames(wine) <- c("Cvs","Alcohol","Malic acid","Ash","Alcalinity of ash", "Magnesium", "Total phenols", "Flavanoids", "Nonflavanoid phenols", "Proanthocyanins", "Color intensity", "Hue", "OD280/OD315 of diluted wines", "Proline")
# The first column corresponds to the classes wineClasses <- factor(wine$Cvs)
# Use pairs
pairs(wine[,-1], col = wineClasses, upper.panel = NULL, pch = 16, cex = 0.5) legend("topright", bty = "n", legend = c("Cv1","Cv2","Cv3"), pch = 16, col = c("black","red","green"),xpd = T, cex = 2, y.intersp = 0.5)
# pairwise interactions in a set of 13 variables, dev.off() # clear the format from the previous plot winePCA <- prcomp(scale(wine[,-1])) plot(winePCA$x[,1:2], col = wineClasses)
# repeat the procedure after introducing an outlier in place of the 10th observation.
wineOutlier <- wine
wineOutlier[10,] <- wineOutlier[10,]*10
# change the 10th obs. into an extreme one by multiplying its profile by 10 outlierPCA <- prcomp(scale(wineOutlier[,-1]))
plot(outlierPCA$x[,1:2], col = wineClasses)
source("https://bioconductor.org/biocLite.R") biocLite("pcaMethods")
library(pcaMethods)
winePCAmethods <- pca(wine[,-1], scale = "uv", center = T, nPcs = 2, method = "svd")
slplot(winePCAmethods, scoresLoadings = c(T,T), scol = wineClasses) Output:
To perform Principal component analysis (PCA) on the dataset using R is executed successfully.
POST LAB Task:
1. Perform the following tasks on tumor dataset
(i) Standardize the data (Center and scale).
(ii) Calculate the Eigenvectors and Eigenvalues from the covariance matrix or correlation matrix.
(iii) Sort the Eigenvalues in descending order and choose the K largest Eigenvectors (Where K is the desired number of dimensions of the new feature subspace k ≤ d).
CS 340 Milestone One Guidelines and Rubric Overview: For this assignment, you will implement the fundamental operations of create, read, update,
Retail Transaction Programming Project Project Requirements: Develop a program to emulate a purchase transaction at a retail store. This
7COM1028 Secure Systems Programming Referral Coursework: Secure
Create a GUI program that:Accepts the following from a user:Item NameItem QuantityItem PriceAllows the user to create a file to store the sales receip
CS 340 Final Project Guidelines and Rubric Overview The final project will encompass developing a web service using a software stack and impleme