-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMCAcode.R
105 lines (89 loc) · 4.75 KB
/
MCAcode.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#load libraries
library(readr)
library(tidyverse)
library(dplyr)
library(ggplot2)
library(FactoMineR)
library(factoextra)
#load data
data <- read_csv("data.csv")
summary(data)
str(data)
names(data)<-str_replace_all(names(data), c(" " = "." , "," = "" ))
#data manipulation
data$BMI.Category <- gsub("Normal Weight", "Normal", data$BMI.Category)
data$BMI.Category <- factor(data$BMI.Category, levels = c("Normal","Overweight","Obese"))
data$sleep.hours <- as.factor(ifelse(data$Sleep.Duration < 7, '< 7 hours',
ifelse(data$Sleep.Duration > 7, '7 - 9 hours', '7 hours')))
data <- data %>%
mutate(Stress.Level = as.character(Stress.Level)) %>%
mutate(Quality.of.Sleep = as.character(Quality.of.Sleep))
data$Stress.Level <- factor(data$Stress.Level,levels = c("1","2","3","4","5","6","7","8","9","10"))
data$Quality.of.Sleep <- factor(data$Quality.of.Sleep,levels = c("1","2","3","4","5","6","7","8","9","10"))
data$agecat <- as.factor(ifelse(data$Age < 31, '20 - 30',
ifelse(data$Age < 41, '30 - 40',
ifelse(data$Age < 51, '40 - 50', '50 - 60'))))
data$bloodpressure <- as.factor(ifelse(data$Blood.Pressure %in% c("117/76","118/76","115/75","115/78","119/77","118/75"), 'normal',
ifelse(data$Blood.Pressure %in% c("121/79"), 'elevated',
ifelse(data$Blood.Pressure %in% c("120/80","125/80","122/80","126/83","132/87","130/86","128/85","131/86","128/84","135/88","129/84","130/85","125/82"), 'high bp stage 1', 'high bp stage 2'))))
data$bloodpressure <- factor(data$bloodpressure, levels = c("normal", "elevated", "high bp stage 1", "high bp stage 2"))
data$dailysteps <- as.factor(ifelse(data$Daily.Steps < 5000, 'sedentary',
ifelse(data$Daily.Steps > 4999 & data$Daily.Steps < 7500, 'low active',
ifelse(data$Daily.Steps > 7499 & data$Daily.Steps < 10000, 'somewhat active', 'active'))))
data$dailysteps <- factor(data$dailysteps, levels = c("sedentary","low active","somewhat active","active"))
data$heart.rate <- case_when(
data$Gender == "Male" & data$Heart.Rate > 70 & data$Heart.Rate < 76 ~ "average",
data$Gender == "Male" & data$Heart.Rate > 65 & data$Heart.Rate < 72 ~ "above average",
data$Gender == "Male" & data$Heart.Rate > 74 & data$Heart.Rate < 81 ~ "below average",
data$Gender == "Male" & data$Heart.Rate > 54 & data$Heart.Rate < 62 ~ "excellent",
data$Gender == "Male" & data$Heart.Rate > 61 & data$Heart.Rate < 67 ~ "good",
data$Gender == "Male" & data$Heart.Rate > 81 ~ "poor",
data$Gender == "Female" & data$Heart.Rate > 72 & data$Heart.Rate < 77 ~ "average",
data$Gender == "Female" & data$Heart.Rate > 68 & data$Heart.Rate < 74 ~ "above average",
data$Gender == "Female" & data$Heart.Rate > 76 & data$Heart.Rate < 84 ~ "below average",
data$Gender == "Female" & data$Heart.Rate > 59 & data$Heart.Rate < 65 ~ "excellent",
data$Gender == "Female" & data$Heart.Rate > 64 & data$Heart.Rate < 69 ~ "good",
data$Gender == "Female" & data$Heart.Rate > 83 ~ "poor")
data$heart.rate <- factor(data$heart.rate, levels = c("poor", "below average", "average", "above average","good","excellent"))
data$Sleep.Disorder <- factor(data$Sleep.Disorder, levels = c("None", "Insomnia", "Sleep Apnea"))
str(data)
#subset
dataMCA <- subset(data, select = c(2,4,6,8,9,13:18))
str(dataMCA)
#mca
res.mca <- MCA(dataMCA, quali.sup = c(1,2,8), graph = FALSE)
summary(res.mca)
head(res.mca$eig, 10)
fviz_screeplot(res.mca, addlabels = TRUE, ylim = c(0, 45))
#dimdesc
res.desc <- dimdesc(res.mca, axes = c(1,2))
# Description of dimension 1
res.desc[[1]]
# Description of dimension 2
res.desc[[2]]
#categories variables
var <- get_mca_var(res.mca)
var
fviz_mca_var(res.mca, choice = "mca.cor",
repel = TRUE, ggtheme = theme_minimal(),
geom = c("arrow", "text"), arrow.size = 0.02, arrow.length = unit(0.1, "inches"))
#coord var
var$coord
fviz_mca_var(res.mca, col.var="black", shape.var = 15,
repel = TRUE,ggtheme = theme_minimal(),linecolor = "black")
head(round(var$coord, 2),15)
#cos2
head(round(var$cos2, 2),15)
fviz_mca_var(res.mca, col.var = "cos2", alpha.var="cos2",invisible=c("quali.sup","ind"),
gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
repel = TRUE, # Avoid text overlapping
ggtheme = theme_minimal())
library("corrplot")
corrplot(var$cos2, is.corr=FALSE,method = "number",
tl.col = "brown",tl.cex = 0.8, tl.srt=45, number.cex = 0.5)
#contrib
head(round(var$contrib, 2),15)
fviz_mca_var(res.mca, col.var = "contrib", invisible=c("quali.sup","ind"),
gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
repel = TRUE, # avoid text overlapping (slow)
ggtheme = theme_minimal())