-
Notifications
You must be signed in to change notification settings - Fork 0
/
_079_pca.R
54 lines (39 loc) · 1.12 KB
/
_079_pca.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Filename: _079_pca.R
# Title: Principal Component Analysis (PCA) in R
# Author: Raghava | GitHub: @raghavtwenty
# Date Created: July 7, 2024 | Last Updated: July 8, 2024
# Language: R | Version: 4.4.0
# Load necessary libraries
# install.packages('dplyr')
library(stats)
library(dplyr)
# View the iris dataset
View(iris)
# Select the first four columns of the iris dataset (excluding the species column)
mydata <- select(iris, c(1, 2, 3, 4))
# Calculate the correlation matrix
cor_matrix <- cor(mydata)
print(cor_matrix)
# Calculate the mean of the correlation coefficients
mean_cor <- mean(cor_matrix)
print(mean_cor)
# Perform Principal Component Analysis (PCA)
PCA <- princomp(mydata)
# Display the loadings of the PCA
print(PCA$loadings)
# Summary of PCA results
summary(PCA)
# Extract the PCA scores
PC <- PCA$scores
# View the first few PCA scores
View(PC)
# Calculate the correlation matrix of the PCA scores
cor_PC <- cor(PC)
print(cor_PC)
# Plot the PCA results
plot(PCA)
screeplot(PCA, type = "line", main = "Screeplot")
# Create a biplot of the PCA
biplot(PCA)
# View the first 10 PCA scores
print(PCA$scores[1:10,])