install.packages(c("FactoMineR", "factoextra"))
## Installing packages into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
library("FactoMineR")
library("factoextra")
## Loading required package: ggplot2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
##
## Attaching package: 'ade4'
## The following object is masked from 'package:FactoMineR':
##
## reconst
res.ca <- dudi.coa (housetasks, scannf = FALSE, nf = 5)
library (ca)
res.ca <- ca (housetasks)
library (MASS)
res.ca <- corresp(housetasks, nf = 3)
## Loading required package: prettyGraphs
res.ca <- epCA (housetasks, graph = FALSE)
fviz_eig(res.ca) # Scree plot

fviz_ca_biplot (res.ca) # Biplot of rows and columns

data (housetasks)
# head (house tasks)
##
## Attaching package: 'gplots'
## The following object is masked from 'package:stats':
##
## lowess
#1. convert the data as a table
dt <- as.table(as.matrix (housetasks))
#2. Graph
balloonplot(t(dt), main = "housetasks", xlab = "", ylab="",
label = FALSE, show.margins = FALSE)

chisq <- chisq.test(housetasks)
chisq
##
## Pearson's Chi-squared test
##
## data: housetasks
## X-squared = 1944.5, df = 36, p-value < 2.2e-16
library("FactoMineR")
res.ca <- CA (housetasks, graph = FALSE)
## **Results of the Correspondence Analysis (CA)**
## The row variable has 13 categories; the column variable has 4 categories
## The chi square of independence between the two variables is equal to 1944.456 (p-value = 0 ).
## *The results are available in the following objects:
##
## name description
## 1 "$eig" "eigenvalues"
## 2 "$col" "results for the columns"
## 3 "$col$coord" "coord. for the columns"
## 4 "$col$cos2" "cos2 for the columns"
## 5 "$col$contrib" "contributions of the columns"
## 6 "$row" "results for the rows"
## 7 "$row$coord" "coord. for the rows"
## 8 "$row$cos2" "cos2 for the rows"
## 9 "$row$contrib" "contributions of the rows"
## 10 "$call" "summary called parameters"
## 11 "$call$marge.col" "weights of the columns"
## 12 "$call$marge.row" "weights of the rows"
#Chi-square statistics
chi2 <- 1944.456
#Degree of freedom
df <- (nrow (housetasks) - 1) * (ncol (housetasks) - 1)
#P-value
pval <- pchisq (chi2, df = df, lower.tail = FALSE)
pval
## [1] 0
library("factoextra")
eig.val <- get_eigenvalue(res.ca)
eig.val
## eigenvalue variance.percent cumulative.variance.percent
## Dim.1 0.5428893 48.69222 48.69222
## Dim.2 0.4450028 39.91269 88.60491
## Dim.3 0.1270484 11.39509 100.00000
fviz_screeplot (res.ca, addlabels = TRUE, ylim = c(0, 50))

fviz_screeplot(res.ca) +
geom_hline (yintercept=33.33, linetype=2, color="red")

# repel= TRUE to avoid text overlapping (slow if many point)
fviz_ca_biplot (res.ca, repel = TRUE)

row <- get_ca_row(res.ca)
row
## Correspondence Analysis - Results for rows
## ===================================================
## Name Description
## 1 "$coord" "Coordinates for the rows"
## 2 "$cos2" "Cos2 for the rows"
## 3 "$contrib" "contributions of the rows"
## 4 "$inertia" "Inertia of the rows"
# Coordinates
head (row$coord)
## Dim 1 Dim 2 Dim 3
## Laundry -0.9918368 0.4953220 -0.31672897
## Main_meal -0.8755855 0.4901092 -0.16406487
## Dinner -0.6925740 0.3081043 -0.20741377
## Breakfeast -0.5086002 0.4528038 0.22040453
## Tidying -0.3938084 -0.4343444 -0.09421375
## Dishes -0.1889641 -0.4419662 0.26694926
# Cos2: quality on the factore map
head(row$cos2)
## Dim 1 Dim 2 Dim 3
## Laundry 0.7399874 0.1845521 0.07546047
## Main_meal 0.7416028 0.2323593 0.02603787
## Dinner 0.7766401 0.1537032 0.06965666
## Breakfeast 0.5049433 0.4002300 0.09482670
## Tidying 0.4398124 0.5350151 0.02517249
## Dishes 0.1181178 0.6461525 0.23572969
# Contributions to the principal components
head(row$contrib)
## Dim 1 Dim 2 Dim 3
## Laundry 18.2867003 5.563891 7.968424
## Main_meal 12.3888433 4.735523 1.858689
## Dinner 5.4713982 1.321022 2.096926
## Breakfeast 3.8249284 3.698613 3.069399
## Tidying 1.9983518 2.965644 0.488734
## Dishes 0.4261663 2.844117 3.634294
## Dim 1 Dim 2 Dim 3
## Laundry -0.9918368 0.4953220 -0.31672897
## Main_meal -0.8755855 0.4901092 -0.16406487
## Dinner -0.6925740 0.3081043 -0.20741377
## Breakfeast -0.5086002 0.4528038 0.22040453
## Tidying -0.3938084 -0.4343444 -0.09421375
## Dishes -0.1889641 -0.4419662 0.26694926
fviz_ca_row(res.ca, repel = TRUE)

fviz_ca_row(res.ca, col.row="steelblue", shape.row = 15)

## Dim 1 Dim 2 Dim 3
## Laundry 0.7399874 0.1845521 0.07546047
## Main_meal 0.7416028 0.2323593 0.02603787
## Dinner 0.7766401 0.1537032 0.06965666
## Breakfeast 0.5049433 0.4002300 0.09482670
# Color by cos2 values: quality on the factor map
fviz_ca_row(res.ca, col.row = "cos2",
gradient.cols= c("#00AFBB", "#E7B800", "#FC4E07"),
repel = TRUE)

# Change the transparency by cos2 values
fviz_ca_row(res.ca, alpha.row="cos2")

## corrplot 0.92 loaded
corrplot (row$cos2, is.corr=FALSE)

# Cos2 of rows on Dim. 1 and Dim.2
fviz_cos2 (res.ca, choice = "row", axes = 1:2)

## Dim 1 Dim 2 Dim 3
## Laundry 18.2867003 5.563891 7.968424
## Main_meal 12.3888433 4.735523 1.858689
## Dinner 5.4713982 1.321022 2.096926
## Breakfeast 3.8249284 3.698613 3.069399
## Tidying 1.9983518 2.965644 0.488734
## Dishes 0.4261663 2.844117 3.634294
library("corrplot")
corrplot (row$contrib, is.corr=FALSE)

# Contributions of rows to dimension 1
fviz_contrib (res.ca, choice = "row", axes = 1, top = 10)

# Contributions of rows to dimension 2
fviz_contrib(res.ca, choice = "row", axes = 2, top = 10)

#Total contribution to dimension 1 and 2
fviz_contrib(res.ca, choice = "row", axes = 1:2, top = 10)

fviz_ca_row(res.ca, col.row = "contrib",
gradient.cols = c("#00AFBB", "#E7B800","#FC4E07"),
repel = TRUE)

# Change the transparency by contrib values
fviz_ca_row(res.ca, alpha.row="contrib",
repel = TRUE)

col <- get_ca_col(res.ca)
col
## Correspondence Analysis - Results for columns
## ===================================================
## Name Description
## 1 "$coord" "Coordinates for the columns"
## 2 "$cos2" "Cos2 for the columns"
## 3 "$contrib" "contributions of the columns"
## 4 "$inertia" "Inertia of the columns"
# Coordinates of column points
head(col$coord)
## Dim 1 Dim 2 Dim 3
## Wife -0.83762154 0.3652207 -0.19991139
## Alternating -0.06218462 0.2915938 0.84858939
## Husband 1.16091847 0.6019199 -0.18885924
## Jointly 0.14942609 -1.0265791 -0.04644302
#Quality of representation
head (col$cos2)
## Dim 1 Dim 2 Dim 3
## Wife 0.801875947 0.1524482 0.045675847
## Alternating 0.004779897 0.1051016 0.890118521
## Husband 0.772026244 0.2075420 0.020431728
## Jointly 0.020705858 0.9772939 0.002000236
# Contributions
head(col$contrib)
## Dim 1 Dim 2 Dim 3
## Wife 44.462018 10.312237 10.8220753
## Alternating 0.103739 2.782794 82.5492464
## Husband 54.233879 17.786612 6.1331792
## Jointly 1.200364 69.118357 0.4954991

fviz_ca_col(res.ca, col.col = "cos2",
gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
repel = TRUE)

fviz_cos2(res.ca, choice = "col", axes = 1:2)

fviz_contrib(res.ca, choice = "col", axes = 1:2)

fviz_ca_biplot (res.ca, repel = TRUE)

fviz_ca_biplot(res.ca,
map="rowprincipal", arrow = c(TRUE, TRUE),
repel = TRUE)

fviz_ca_biplot(res.ca, map ="colgreen", arrow = c(TRUE, FALSE),
repel = TRUE)

#Dimension description
res.desc <- dimdesc (res.ca, axes = c(1,2))
# Description of dimension 1 by row points
head (res.desc[[1]]$row, 4)
## coord
## Laundry -0.9918368
## Main_meal -0.8755855
## Dinner -0.6925740
## Breakfeast -0.5086002
# Description of dimension 1 by colum points
head (res.desc[[1]]$col, 4)
## coord
## Wife -0.83762154
## Alternating -0.06218462
## Jointly 0.14942609
## Husband 1.16091847
# Description of dimension 2 by row points
res.desc[[2]]$row
## coord
## Holidays -1.4350066
## Finances -0.6178684
## Insurance -0.4737832
## Dishes -0.4419662
## Tidying -0.4343444
## Shopping -0.4033171
## Official 0.2536132
## Dinner 0.3081043
## Breakfeast 0.4528038
## Main_meal 0.4901092
## Laundry 0.4953220
## Driving 0.6534143
## Repairs 0.8642647
# Description of dimension 1 by column points
res.desc[[2]]$col
## coord
## Jointly -1.0265791
## Alternating 0.2915938
## Wife 0.3652207
## Husband 0.6019199
data(children)
#head (children)
res.ca <- CA (children, row.sup = 15:18, col.sup = 6:8,
graph = FALSE)
fviz_ca_biplot (res.ca, repel = TRUE)

fviz_ca_biplot(res.ca, repel = TRUE,
invisible = c("row.sup", "col.sup"))

## $coord
## Dim 1 Dim 2 Dim 3 Dim 4
## comfort 0.2096705 0.7031677 0.07111168 0.3071354
## disagreement 0.1462777 0.1190106 0.17108916 -0.3132169
## world 0.5233045 0.1429707 0.08399269 -0.1063597
## to_live 0.3083067 0.5020193 0.52093397 0.2557357
##
## $cos2
## Dim 1 Dim 2 Dim 3 Dim 4
## comfort 0.06892759 0.77524032 0.007928672 0.14790342
## disagreement 0.13132177 0.08692632 0.179649183 0.60210272
## world 0.87587685 0.06537746 0.022564054 0.03618163
## to_live 0.13899699 0.36853645 0.396830367 0.09563620
fviz_ca_row(res.ca, repel = TRUE)

## $coord
## Dim 1 Dim 2 Dim 3 Dim 4
## thirty 0.10541339 -0.05969594 -0.10322613 0.06977996
## fifty -0.01706444 0.04907657 -0.01568923 -0.01306117
## more_fifty -0.17706810 -0.04813788 0.10077299 -0.08517528
##
## $cos2
## Dim 1 Dim 2 Dim 3 Dim 4
## thirty 0.1375601 0.04411543 0.131910759 0.060278490
## fifty 0.0108695 0.08990298 0.009188167 0.006367804
## more_fifty 0.2860989 0.02114509 0.092666735 0.066200714
fviz_ca_col (res.ca, repel = TRUE)

#Visualize rows with cos2 >= 0.8
fviz_ca_row(res.ca, select.row = list (cos2 = 0.8))

# Top 5 active rous and 5 suppl. rous with the highest cos2
fviz_ca_row(res.ca, select.row = list (cos2 = 5))

# Select by names
name <-list (name = c("employment", "fear", "future"))
fviz_ca_row(res.ca, select.row = name)

#Top 5 contributing rows and columns
fviz_ca_biplot(res.ca, select.row = list (contrib=5),
select.col= list(contrib= 5)) +
theme_minimal()

#Scree plot
scree.plot <- fviz_eig(res.ca)
#Biplot of row and column variables
biplot.ca <- fviz_ca_biplot (res.ca)
library(ggpubr)
ggexport (plotlist = list (scree.plot, biplot.ca),
filename = "CA.pdf")
## file saved to CA.pdf
# Export into a TXT file
write.infile(res.ca, "ca.txt", sep = "\t")
#Export into a CSV file
write.infile(res.ca, "ca.csv", sep = ";")
library("FactoMineR")
library("factoextra")
data (poison)
head (poison[, 1:7], 3)
## Age Time Sick Sex Nausea Vomiting Abdominals
## 1 9 22 Sick_y F Nausea_y Vomit_n Abdo_y
## 2 5 0 Sick_n F Nausea_n Vomit_n Abdo_n
## 3 6 16 Sick_y F Nausea_n Vomit_y Abdo_y
poison.active <- poison [1:55, 5:15]
head (poison.active[, 1:6], 3)
## Nausea Vomiting Abdominals Fever Diarrhae Potato
## 1 Nausea_y Vomit_n Abdo_y Fever_y Diarrhea_y Potato_y
## 2 Nausea_n Vomit_n Abdo_n Fever_n Diarrhea_n Potato_y
## 3 Nausea_n Vomit_y Abdo_y Fever_y Diarrhea_y Potato_y
#Summary of the 4 first variables
summary (poison.active) [, 1:4]
## Nausea Vomiting Abdominals Fever
## Nausea_n:43 Vomit_n:33 Abdo_n:18 Fever_n:20
## Nausea_y:12 Vomit_y:22 Abdo_y:37 Fever_y:35
for (i in 1:4) {
plot (poison.active[,1], main=colnames(poison.active) [i],
ylab = "Count", col="steelblue", las = 2)
}




res.mca <- MCA (poison.active, graph = FALSE)
## **Results of the Multiple Correspondence Analysis (MCA)**
## The analysis was performed on 55 individuals, described by 11 variables
## *The results are available in the following objects:
##
## name description
## 1 "$eig" "eigenvalues"
## 2 "$var" "results for the variables"
## 3 "$var$coord" "coord. of the categories"
## 4 "$var$cos2" "cos2 for the categories"
## 5 "$var$contrib" "contributions of the categories"
## 6 "$var$v.test" "v-test for the categories"
## 7 "$ind" "results for the individuals"
## 8 "$ind$coord" "coord. for the individuals"
## 9 "$ind$cos2" "cos2 for the individuals"
## 10 "$ind$contrib" "contributions of the individuals"
## 11 "$call" "intermediate results"
## 12 "$call$marge.col" "weights of columns"
## 13 "$call$marge.li" "weights of rows"
library("factoextra")
eig.val <-get_eigenvalue (res.mca)
# head (eig. val)
fviz_screeplot (res.mca, addlabels = TRUE, ylim = c(0, 45))

fviz_mca_biplot(res.mca,
repel = TRUE, # Avoid text overlapping (slow if many point)
ggtheme = theme_minimal())

var <- get_mca_var(res.mca)
var
## Multiple Correspondence Analysis Results for variables
## ===================================================
## Name Description
## 1 "$coord" "Coordinates for categories"
## 2 "$cos2" "Cos2 for categories"
## 3 "$contrib" "contributions of categories"
# Coordinates
head (var$coord)
## Dim 1 Dim 2 Dim 3 Dim 4 Dim 5
## Nausea_n 0.2673909 0.12139029 -0.265583253 0.03376130 0.07370500
## Nausea_y -0.9581506 -0.43498187 0.951673323 -0.12097801 -0.26410958
## Vomit_n 0.4790279 -0.40919465 0.084492799 0.27361142 0.05245250
## Vomit_y -0.7185419 0.61379197 -0.126739198 -0.41041713 -0.07867876
## Abdo_n 1.3180221 -0.03574501 -0.005094243 -0.15360951 -0.06986987
## Abdo_y -0.6411999 0.01738946 0.002478280 0.07472895 0.03399075
# Cos2: quality on the factore nap
head (var$cos2)
## Dim 1 Dim 2 Dim 3 Dim 4 Dim 5
## Nausea_n 0.2562007 0.0528025759 2.527485e-01 0.004084375 0.019466197
## Nausea_y 0.2562007 0.0528025759 2.527485e-01 0.004084375 0.019466197
## Vomit_n 0.3442016 0.2511603912 1.070855e-02 0.112294813 0.004126898
## Vomit_y 0.3442016 0.2511603912 1.070855e-02 0.112294813 0.004126898
## Abdo_n 0.8451157 0.0006215864 1.262496e-05 0.011479077 0.002374929
## Abdo_y 0.8451157 0.0006215864 1.262496e-05 0.011479077 0.002374929
# Contributions to the principal components
head(var$contrib)
## Dim 1 Dim 2 Dim 3 Dim 4 Dim 5
## Nausea_n 1.515869 0.81100008 4.670018e+00 0.08449397 0.48977906
## Nausea_y 5.431862 2.90608363 1.673423e+01 0.30277007 1.75504164
## Vomit_n 3.733667 7.07226253 3.627455e-01 4.25893721 0.19036376
## Vomit_y 5.600500 10.60839380 5.441183e-01 6.38840581 0.28554563
## Abdo_n 15.417637 0.02943661 7.192511e-04 0.73219636 0.18424268
## Abdo_y 7.500472 0.01432051 3.499060e-04 0.35620363 0.08963157
fviz_mca_var(res.mca, choice = "mca.cor",
repel = TRUE, # Avoid text overlapping (slow)
ggtheme = theme_minimal())

head (round(var$coord, 2), 4)
## Dim 1 Dim 2 Dim 3 Dim 4 Dim 5
## Nausea_n 0.27 0.12 -0.27 0.03 0.07
## Nausea_y -0.96 -0.43 0.95 -0.12 -0.26
## Vomit_n 0.48 -0.41 0.08 0.27 0.05
## Vomit_y -0.72 0.61 -0.13 -0.41 -0.08
fviz_mca_var(res.mca,
repel = TRUE, # Avoid text overlapping (slow)
ggtheme = theme_minimal())

fviz_mca_var(res.mca, col.var="black", shape.var = 15,
repel = TRUE)

## Dim 1 Dim 2 Dim 3 Dim 4 Dim 5
## Nausea_n 0.2562007 0.05280258 0.25274850 0.004084375 0.019466197
## Nausea_y 0.2562007 0.05280258 0.25274850 0.004084375 0.019466197
## Vomit_n 0.3442016 0.25116039 0.01070855 0.112294813 0.004126898
## Vomit_y 0.3442016 0.25116039 0.01070855 0.112294813 0.004126898
# Color by cos2 values: quality on the factor map
fviz_mca_var(res.mca, col.var = "cos2",
gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
repel = TRUE, # Avoid text overlapping
ggtheme = theme_minimal())

# Change the transparency by cos2 values
fviz_mca_var(res.mca, alpha.var="cos2",
repel = TRUE,
ggtheme = theme_minimal())

library("corrplot")
corrplot (var$cos2, is.corr=FALSE)

# Cos2 of variable categories on Dim. 1 and Dim. 2
fviz_cos2(res.mca, choice = "var", axes = 1:2)

head (round (var$contrib, 2), 4)
## Dim 1 Dim 2 Dim 3 Dim 4 Dim 5
## Nausea_n 1.52 0.81 4.67 0.08 0.49
## Nausea_y 5.43 2.91 16.73 0.30 1.76
## Vomit_n 3.73 7.07 0.36 4.26 0.19
## Vomit_y 5.60 10.61 0.54 6.39 0.29
# Contributions of rows to dimension 1
fviz_contrib (res.mca, choice = "var", axes = 1, top = 15)

# Contributions of rows to dimension 2
fviz_contrib (res.mca, choice = "var", axes = 2, top = 15)

# Total contribution to dimension 1 and 2
fviz_contrib (res.mca, choice = "var", axes = 1:2, top = 15)

fviz_mca_var(res.mca, col.var = "contrib",
gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
repel = TRUE, # avoid text overlapping (slow)
ggtheme = theme_minimal()
)

# Change the transparency by contrib values
fviz_mca_var(res.mca, alpha.var="contrib",
repel = TRUE,
ggtheme = theme_minimal())

ind <- get_mca_ind(res.mca)
ind
## Multiple Correspondence Analysis Results for individuals
## ===================================================
## Name Description
## 1 "$coord" "Coordinates for the individuals"
## 2 "$cos2" "Cos2 for the individuals"
## 3 "$contrib" "contributions of the individuals"
#Coordinates of column points
head (ind$coord)
## Dim 1 Dim 2 Dim 3 Dim 4 Dim 5
## 1 -0.4525811 -0.26415072 0.17151614 0.01369348 -0.11696806
## 2 0.8361700 -0.03193457 -0.07208249 -0.08550351 0.51978710
## 3 -0.4481892 0.13538726 -0.22484048 -0.14170168 -0.05004753
## 4 0.8803694 -0.08536230 -0.02052044 -0.07275873 -0.22935022
## 5 -0.4481892 0.13538726 -0.22484048 -0.14170168 -0.05004753
## 6 -0.3594324 -0.43604390 -1.20932223 1.72464616 0.04348157
#Quality of representation
head (ind$cos2)
## Dim 1 Dim 2 Dim 3 Dim 4 Dim 5
## 1 0.34652591 0.1180447167 0.0497683175 0.0003172275 0.0231460846
## 2 0.55589562 0.0008108236 0.0041310808 0.0058126211 0.2148103098
## 3 0.54813888 0.0500176790 0.1379484860 0.0547920948 0.0068349171
## 4 0.74773962 0.0070299584 0.0004062504 0.0051072923 0.0507479873
## 5 0.54813888 0.0500176790 0.1379484860 0.0547920948 0.0068349171
## 6 0.02485357 0.0365775483 0.2813443706 0.5722083217 0.0003637178
# Contributions
head (ind$contrib)
## Dim 1 Dim 2 Dim 3 Dim 4 Dim 5
## 1 1.110927 0.98238297 0.498254685 0.003555817 0.31554778
## 2 3.792117 0.01435818 0.088003703 0.138637089 6.23134138
## 3 1.089470 0.25806722 0.856229950 0.380768961 0.05776914
## 4 4.203611 0.10259105 0.007132055 0.100387990 1.21319013
## 5 1.089470 0.25806722 0.856229950 0.380768961 0.05776914
## 6 0.700692 2.67693398 24.769968729 56.404214518 0.04360547
fviz_mca_ind(res.mca, col.ind = "cos2",
gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
repel = TRUE, # Avoid text overlapping (slow if many points)
ggtheme = theme_minimal())

# Cos2 of individuals
fviz_cos2 (res.mca, choice = "ind", axes = 1:2, top = 20)

# Contribution of individuals to the dimensions
fviz_contrib(res.mca, choice = "ind", axes = 1:2, top = 20)

fviz_mca_ind (res.mca,
label = "none", # hide individual labels
habillage = "Vomiting", # color by groups
palette = c("#00AFBB", "#E7B800"),
addEllipses = TRUE, ellipse.type = "confidence",
ggtheme = theme_minimal())

#habillage inder of the column to be used as grouping variable
fviz_mca_ind(res.mca, habillage = 2, addEllipses = TRUE)

#habillage external grouping variable
fviz_mca_ind(res.mca, habillage = poison$Vomiting, addEllipses = TRUE)

fviz_ellipses (res.mca, c("Vomiting", "Fever"),
geom = "point")
## Warning: `gather_()` was deprecated in tidyr 1.2.0.
## ℹ Please use `gather()` instead.
## ℹ The deprecated feature was likely used in the factoextra package.
## Please report the issue at <https://github.com/kassambara/factoextra/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

fviz_ellipses (res.mca, 1:4, geom = "point")

res.desc <- dimdesc(res.mca, axes = c(1,2))
# Description of dimension 1
res.desc[[1]]
##
## Link between the variable and the categorical variable (1-way anova)
## =============================================
## R2 p.value
## Abdominals 0.8451157 4.055640e-23
## Diarrhae 0.7994680 3.910776e-20
## Fever 0.7846788 2.600566e-19
## Mayo 0.3829749 4.756234e-07
## Vomiting 0.3442016 2.510738e-06
## Nausea 0.2562007 8.062777e-05
## Cheese 0.1944181 7.534834e-04
##
## Link between variable and the categories of the categorical variables
## ================================================================
## Estimate p.value
## Abdominals=Abdo_n 0.5671866 4.055640e-23
## Diarrhae=Diarrhea_n 0.5380920 3.910776e-20
## Fever=Fever_n 0.5330918 2.600566e-19
## Mayo=Mayo_n 0.4644981 4.756234e-07
## Vomiting=Vomit_n 0.3466915 2.510738e-06
## Nausea=Nausea_n 0.3547892 8.062777e-05
## Cheese=Cheese_n 0.3830043 7.534834e-04
## Cheese=Cheese_y -0.3830043 7.534834e-04
## Nausea=Nausea_y -0.3547892 8.062777e-05
## Vomiting=Vomit_y -0.3466915 2.510738e-06
## Mayo=Mayo_y -0.4644981 4.756234e-07
## Fever=Fever_y -0.5330918 2.600566e-19
## Diarrhae=Diarrhea_y -0.5380920 3.910776e-20
## Abdominals=Abdo_y -0.5671866 4.055640e-23
# Description of dimension 2
res.desc[[2]]
##
## Link between the variable and the categorical variable (1-way anova)
## =============================================
## R2 p.value
## Courgette 0.4464145 2.500166e-08
## Potato 0.3957543 2.690662e-07
## Vomiting 0.2511604 9.728027e-05
## Icecream 0.1409011 4.743927e-03
##
## Link between variable and the categories of the categorical variables
## ================================================================
## Estimate p.value
## Courgette=Courg_n 0.4176013 2.500166e-08
## Potato=Potato_y 0.4977523 2.690662e-07
## Vomiting=Vomit_y 0.1838104 9.728027e-05
## Icecream=Icecream_n 0.2597197 4.743927e-03
## Icecream=Icecream_y -0.2597197 4.743927e-03
## Vomiting=Vomit_n -0.1838104 9.728027e-05
## Potato=Potato_n -0.4977523 2.690662e-07
## Courgette=Courg_y -0.4176013 2.500166e-08
res.mca <- MCA (poison, ind.sup = 53:55,
quanti.sup = 1:2, quali.sup = 3:4, graph=FALSE)
#Supplementary qualitative variable categories
res.mca$quali.sup
## $coord
## Dim 1 Dim 2 Dim 3 Dim 4 Dim 5
## Sick_n 1.41809140 0.0020394048 0.13199139 -0.016036841 -0.08354663
## Sick_y -0.63026284 -0.0009064021 -0.05866284 0.007127485 0.03713184
## F -0.03108147 0.1123143957 0.05033124 -0.055927173 -0.06832928
## M 0.03356798 -0.1212995474 -0.05435774 0.060401347 0.07379562
##
## $cos2
## Dim 1 Dim 2 Dim 3 Dim 4 Dim 5
## Sick_n 0.893770319 1.848521e-06 0.007742990 0.0001143023 0.003102240
## Sick_y 0.893770319 1.848521e-06 0.007742990 0.0001143023 0.003102240
## F 0.001043342 1.362369e-02 0.002735892 0.0033780765 0.005042401
## M 0.001043342 1.362369e-02 0.002735892 0.0033780765 0.005042401
##
## $v.test
## Dim 1 Dim 2 Dim 3 Dim 4 Dim 5
## Sick_n 6.7514655 0.009709509 0.6284047 -0.07635063 -0.3977615
## Sick_y -6.7514655 -0.009709509 -0.6284047 0.07635063 0.3977615
## F -0.2306739 0.833551410 0.3735378 -0.41506855 -0.5071119
## M 0.2306739 -0.833551410 -0.3735378 0.41506855 0.5071119
##
## $eta2
## Dim 1 Dim 2 Dim 3 Dim 4 Dim 5
## Sick 0.893770319 1.848521e-06 0.007742990 0.0001143023 0.003102240
## Sex 0.001043342 1.362369e-02 0.002735892 0.0033780765 0.005042401
#Supplementary quantitative variables
res.mca$quanti
## $coord
## Dim 1 Dim 2 Dim 3 Dim 4 Dim 5
## Age 0.003934896 -0.00741340 -0.26494536 0.20015501 0.02928483
## Time -0.838158507 -0.08330586 -0.08718851 -0.08421599 -0.02316931
#Supplementary individuals
res.mca$ind.sup
## $coord
## Dim 1 Dim 2 Dim 3 Dim 4 Dim 5
## 53 1.0835684 0.5172478 0.5794063 0.5390903 0.4553650
## 54 -0.1249473 0.1417271 -0.1765234 -0.1526587 -0.2779565
## 55 -0.4315948 0.1270468 -0.2071580 -0.1186804 -0.1891760
##
## $cos2
## Dim 1 Dim 2 Dim 3 Dim 4 Dim 5
## 53 0.36304957 0.08272764 0.10380536 0.08986204 0.06411692
## 54 0.03157652 0.04062716 0.06302535 0.04713607 0.15626590
## 55 0.50232519 0.04352713 0.11572730 0.03798314 0.09650827
#Baplot of individuals and variable categories
fviz_mca_biplot(res.mca, repel = TRUE,
ggtheme = theme_minimal())

fviz_mca_var(res.mca, choice = "mca.cor",
repel = TRUE)

fviz_mca_var(res.mca, repel = TRUE,
ggtheme = theme_minimal())

fviz_mca_var(res.mca, choice = "quanti.sup",
ggtheme = theme_minimal())

fviz_mca_ind (res.mca,
label = "ind.sup", #Show the label of ind.sup only
ggtheme = theme_minimal())

# Visualize variable categories with cos2 >= 0.4
fviz_mca_var(res.mca, select.var = list(cos2 = 0.4))

# Top 10 active variables with the highest cos2
fviz_mca_var(res.mca, select.var = list(cos2 = 10))

# Select by names
name <- list(name= c("Fever_n", "Abdo_y", "Diarrhea_n",
"Fever_Y", "Vomit_y", "Vomit_n"))
fviz_mca_var(res.mca, select.var = name)

# top 5 contributing individuals and variable categories
fviz_mca_biplot(res.mca, select.ind = list(contrib = 5),
select.var = list (contrib = 5),
ggtheme = theme_minimal())

# Scree plot
scree.plot <- fviz_eig(res.mca)
#Biplot of row and column variables
biplot.mca <- fviz_mca_biplot(res.mca)
library(ggpubr)
ggexport (plotlist = list(scree.plot, biplot.mca),
filename = "MCA.pdf")
## file saved to MCA.pdf
#Export into a TXT file
write.infile(res.mca, "aca.txt", sep="\t")
#Export into a CSV file
write.infile(res.mca, "mca.csv", sep=";")
library("ade4")
res.mca <- dudi.acm(poison.active, scannf= FALSE, nf = 5)
library("ExPosition")
res.mca <- epMCA(poison.active, graph = FALSE, correction = "bg")
fviz_eig(res.mca) # Scree plot

fviz_mca_biplot (res.mca) # Biplot of rows and columns

library("FactoMineR")
data(wine)
df <- wine [,c(1,2, 16, 22, 29, 28, 30,31)]
head (df [, 1:7], 4)
## Label Soil Plante Acidity Harmony Intensity Overall.quality
## 2EL Saumur Env1 2.000 2.107 3.143 2.857 3.393
## 1CHA Saumur Env1 2.000 2.107 2.964 2.893 3.214
## 1FON Bourgueuil Env1 1.750 2.179 3.143 3.074 3.536
## 1VAU Chinon Env2 2.304 3.179 2.038 2.462 2.464
## 'data.frame': 21 obs. of 8 variables:
## $ Label : Factor w/ 3 levels "Saumur","Bourgueuil",..: 1 1 2 3 1 2 2 1 3 1 ...
## $ Soil : Factor w/ 4 levels "Reference","Env1",..: 2 2 2 3 1 1 1 2 2 3 ...
## $ Plante : num 2 2 1.75 2.3 1.76 ...
## $ Acidity : num 2.11 2.11 2.18 3.18 2.57 ...
## $ Harmony : num 3.14 2.96 3.14 2.04 3.64 ...
## $ Intensity : num 2.86 2.89 3.07 2.46 3.64 ...
## $ Overall.quality: num 3.39 3.21 3.54 2.46 3.74 ...
## $ Typical : num 3.25 3.04 3.18 2.25 3.44 ...
library (FactoMineR)
res.famd <- FAMD (df, graph = FALSE)
## *The results are available in the following objects:
##
## name description
## 1 "$eig" "eigenvalues and inertia"
## 2 "$var" "Results for the variables"
## 3 "$ind" "results for the individuals"
## 4 "$quali.var" "Results for the qualitative variables"
## 5 "$quanti.var" "Results for the quantitative variables"
library("factoextra")
eig.val <- get_eigenvalue(res.famd)
head(eig.val)
## eigenvalue variance.percent cumulative.variance.percent
## Dim.1 4.8315174 43.922886 43.92289
## Dim.2 1.8568797 16.880724 60.80361
## Dim.3 1.5824794 14.386176 75.18979
## Dim.4 1.1491200 10.446546 85.63633
## Dim.5 0.6518053 5.925503 91.56183

var <- get_famd_var(res.famd)
var
## FAMD results for variables
## ===================================================
## Name Description
## 1 "$coord" "Coordinates"
## 2 "$cos2" "Cos2, quality of representation"
## 3 "$contrib" "Contributions"
#Coordinates of variables
head (var$coord)
## Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
## Plante 0.7344160 0.060551966 0.105902048 0.004011299 0.0010340559
## Acidity 0.1732738 0.491118153 0.126394029 0.115376784 0.0045862935
## Harmony 0.8943968 0.023628146 0.040124469 0.003653813 0.0086624633
## Intensity 0.6991811 0.134639254 0.065382234 0.023214984 0.0064730431
## Overall.quality 0.9115699 0.005246728 0.009336677 0.005445276 0.0007961880
## Typical 0.7808611 0.027094327 0.001549791 0.083446627 0.0005912942
# Cos2: quality of representation on the factore map
head (var$cos2)
## Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
## Plante 0.53936692 3.666541e-03 1.121524e-02 1.609052e-05 1.069272e-06
## Acidity 0.03002381 2.411970e-01 1.597545e-02 1.331180e-02 2.103409e-05
## Harmony 0.79994566 5.582893e-04 1.609973e-03 1.335035e-05 7.503827e-05
## Intensity 0.48885427 1.812773e-02 4.274836e-03 5.389355e-04 4.190029e-05
## Overall.quality 0.83095973 2.752815e-05 8.717353e-05 2.965103e-05 6.339153e-07
## Typical 0.60974400 7.341026e-04 2.401853e-06 6.963340e-03 3.496288e-07
# Contributions to the dimensions
head (var$contrib)
## Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
## Plante 15.200526 3.2609526 6.69215972 0.3490757 0.15864490
## Acidity 3.586323 26.4485720 7.98708850 10.0404466 0.70362936
## Harmony 18.511716 1.2724651 2.53554453 0.3179662 1.32899551
## Intensity 14.471254 7.2508336 4.13163258 2.0202401 0.99309457
## Overall.quality 18.867156 0.2825562 0.59000304 0.4738648 0.12215119
## Typical 16.161818 1.4591321 0.09793437 7.2617850 0.09071638
# Plot of variables
fviz_famd_var(res.famd, repel = TRUE)

# Contribution to the first dimension
fviz_contrib(res.famd, "var", axes = 1)

# Contribution to the second dimension
fviz_contrib(res.famd, "var", axes = 2)

quanti.var <- get_famd_var(res.famd, "quanti.var")
quanti.var
## FAMD results for quantitative variables
## ===================================================
## Name Description
## 1 "$coord" "Coordinates"
## 2 "$cos2" "Cos2, quality of representation"
## 3 "$contrib" "Contributions"
fviz_famd_var(res.famd, "quanti.var", repel = TRUE,
col.var = "black")

fviz_famd_var(res.famd, "quanti.var", col.var = "contrib",
gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
repel = TRUE)

#Color by cos2 values: quality on the factor map
fviz_famd_var(res.famd, "quanti.var", col.var = "cos2",
gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
repel = TRUE)

quali.var <- get_famd_var (res.famd, "quali.var")
quali.var
## FAMD results for qualitative variable categories
## ===================================================
## Name Description
## 1 "$coord" "Coordinates"
## 2 "$cos2" "Cos2, quality of representation"
## 3 "$contrib" "Contributions"
fviz_famd_var(res.famd, "quali.var", col.var = "contrib",
gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07")
)

ind <- get_famd_ind(res.famd)
ind
## FAMD results for individuals
## ===================================================
## Name Description
## 1 "$coord" "Coordinates"
## 2 "$cos2" "Cos2, quality of representation"
## 3 "$contrib" "Contributions"
fviz_famd_ind (res.famd, col.ind = "cos2",
gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
repel = TRUE)

fviz_mfa_ind (res.famd,
habillage = "Label", # color by groups
palette = c("#00AFBB", "#E7B800", "#FC4E07"),
addEllipses = TRUE, ellipse.type = "confidence",
repel = TRUE # Avoid text overlapping
)

fviz_ellipses (res.famd, c("Label", "Soil"), repel = TRUE)

fviz_ellipses (res.famd, 1:2, geom = "point")
