-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFAMD_user_specific.R
executable file
·66 lines (42 loc) · 1.88 KB
/
FAMD_user_specific.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
54
55
56
57
58
59
60
61
62
63
64
library(FactoMineR)
library(factoextra)
library(jsonlite)
myArgs <- commandArgs(trailingOnly = TRUE)
data <- read.csv(myArgs)
########################################################################
#run FAMD
run_FAMD <- function(data_frame) {
data_types <- sapply(data_frame, class)
run_type <- 'MCA'
if ((any(data_types=="integer") || any(data_types == "numeric")) && (any(data_types == "factor") || any(data_types == "character"))) {
run_type <- 'famd'
} else if ((any(data_types=="integer") || any(data_types == "numeric")) && (!any(data_types == "factor") || !any(data_types == "character"))) {
run_type <- 'PCA'
}
if(run_type == 'famd' || run_type == 'MCA') {
################################
# do FAMD in case of qualitative and quantitative data
res.famd <- FAMD(data_frame, graph = FALSE)
var <- get_famd_var(res.famd)
# print(res.famd$eig)
var_contrib <- res.famd[["var"]][["contrib"]]
# Contribution to the first dimension
# print(fviz_contrib(res.famd, "var", axes = 1))
# print(fviz_contrib(res.famd, "var", axes = 2))
data_frame_var_contribution <- as.data.frame(t(var_contrib))
return(list(data_frame_var_contribution, res.famd$eig, res.famd$ind$coord, res.famd$var$coord, -1))
} else if (run_type == 'PCA') {
##########################
# in case of only numerical values use PCA
res.famd = PCA(data_frame, scale.unit=TRUE, ncp=5, graph=FALSE)
var <- res.famd$var
# print(res.famd$eig)
# print(fviz_contrib(res.famd, "var", axes = 1))
# print(fviz_contrib(res.famd, "var", axes = 2))
var_contrib <- var$contrib
data_frame_var_contribution <- as.data.frame(t(var_contrib))
return(list(data_frame_var_contribution, res.famd$eig, res.famd$ind$coord, res.famd$var$coord, -1))
}
}
list_final_contributing <- run_FAMD(data)
cat(jsonlite::toJSON(list_final_contributing, pretty=TRUE))