-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpca_analysis.r
More file actions
29 lines (23 loc) 路 974 Bytes
/
Copy pathpca_analysis.r
File metadata and controls
29 lines (23 loc) 路 974 Bytes
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
pca_analysis <- function(df, var) {
# dependencies
source("load_pckgs.r")
dependencies <- c("factoextra", "dplyr", "tidyr", "ggplot2")
invisible(lapply(dependencies, load_pkg))
# data transformation
data <- df %>%
dplyr::select(dplyr::all_of(var), dplyr::where(is.numeric)) %>%
dplyr::select(-m.body_width, -m.height, -g.conf, -g.srr, -g.aapac18e_h) %>%
tidyr::drop_na()
message("馃搳 The data for PCA:")
dplyr::glimpse(data)
numeric_data <- data %>% dplyr::select(-dplyr::all_of(var))
# analysis
pca_result <- prcomp(numeric_data, center = TRUE, scale. = TRUE)
# plot
factoextra::fviz_pca_ind(pca_result,
label = "none",
habillage = data[[var]],
addEllipses = TRUE,
ellipse.level = 0.95) +
ggtitle("PCA: Individuals Colored by Combined Group")
}