-
Notifications
You must be signed in to change notification settings - Fork 0
/
PCA_short
73 lines (73 loc) · 1.34 KB
/
PCA_short
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
## PCA - short script ####
#
#
# Useful packages
#
library(phia)
library(dplyr)
library(vegan)
library(ggplot2)
library(tidyverse)
library(ggfortify)
library(gridExtra)
#
#
data <-read.table(file.choose(),header=TRUE, fill = FALSE)
attach(data)
names(data)
#
select.data <- data[2:16] # only use columns 2-16 from data
#
#
# By default, the prcomp function centers the variables to have mean zero.
# By using the option scale = TRUE, we scale the variables to have standard deviation one.
# The output from prcomp contains a number of useful quantities.
#
pca <- prcomp(select.data, scale = TRUE)
names(pca)
#
#
# The "center" and "scale" components correspond to the means and SD of the variables
# that were used for scaling prior to implementing PCA
#
# means
pca$center
#
# SD
pca$scale
#
#
# The "rotation" matrix provides the PC loadings;
# each column of pca1$rotation contains the corresponding PC loading vector
#
pca$rotation
#
#
# Obtain the PC scores from the results
# If we project the n data points x1,...,xn onto the first eigenvector,
# the projected values are called the principal component scores for each observation.
#
pca$x
#
#
# Variance Explained by each PC
#
pca$sdev
#
(VE <- pca$sdev^2)
#
# The Proportion of Variance explained by each PC
#
PVE <- VE / sum(VE)
round(PVE, 2)
#
#
screeplot(pca)
#
#
pca
screeplot(pca)
biplot(pca)
#
#
#