logo Hurry, Grab up to 30% discount on the entire course
Order Now logo

Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Doug BeaucheminAccounting
(5/5)

580 Answers

Hire Me
expert
Clinton Kibaki MuneneNursing
(/5)

779 Answers

Hire Me
expert
Earle BirdselllData mining
(5/5)

855 Answers

Hire Me
expert
Basudev RoyMathematics
(/5)

604 Answers

Hire Me
R Programming
(5/5)

Program to perform Principal component analysis (PCA) on the dataset in R PROGRAM

INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS

Problem Description:

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:

 RESULT:

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).

 

(5/5)
Attachments:

Related Questions

. The fundamental operations of create, read, update, and delete (CRUD) in either Python or Java

CS 340 Milestone One Guidelines and Rubric  Overview: For this assignment, you will implement the fundamental operations of create, read, update,

. Develop a program to emulate a purchase transaction at a retail store. This  program will have two classes, a LineItem class and a Transaction class

Retail Transaction Programming Project  Project Requirements:  Develop a program to emulate a purchase transaction at a retail store. This

. The following program contains five errors. Identify the errors and fix them

7COM1028   Secure Systems Programming   Referral Coursework: Secure

. Accepts the following from a user: Item Name Item Quantity Item Price Allows the user to create a file to store the sales receipt contents

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

. The final project will encompass developing a web service using a software stack and implementing an industry-standard interface. Regardless of whether you choose to pursue application development goals as a pure developer or as a software engineer

CS 340 Final Project Guidelines and Rubric  Overview The final project will encompass developing a web service using a software stack and impleme