[Update] Now I got a plot with both active and supplementary variables with the following syntax:
#download & install packages
install.packages("tidyverse")
install.packages("FactoMineR")
install.packages("ggplot2")
install.packages("factoextra")
library(tidyverse)
library(FactoMineR)
library(ggplot2)
library(factoextra)
#import excel file named "Datatest"
recode intercultural competency variables with 5 categories into a yes/no variable
Datatest$hc_proud_gr <- ifelse(Datatest$hc_proud <= 3, 0,1)
Datatest$hc_unique_gr <- ifelse(Datatest$hc_unique <= 3, 0,1)
Datatest$hc_fc_culture_gr <- ifelse(Datatest$hc_fc_culture <= 3, 0,1)
Datatest$fp_interact_fun_gr <- ifelse(Datatest$fp_interact_fun <= 3, 0,1)
Datatest$fc_know_gr <- ifelse(Datatest$fc_know <= 3, 0,1)
Datatest$fc_want_know_gr <- ifelse(Datatest$fc_want_know <= 3, 0,1)
Datatest$fc_respect_gr <- ifelse(Datatest$fc_respect <= 3, 0,1)
Datatest$fp_problem_gr<- ifelse(Datatest$fp_problem <= 3, 0,1)
Datatest$fp_gap_fun_gr<- ifelse(Datatest$fp_gap_fun <= 3, 0,1)
Datatest$fp_interactc_active_gr<- ifelse(Datatest$fp_interactc_active <= 3, 0,1)
#Label variables
Datatest$future_country <- factor(Datatest$future_country,
levels = c(1,2,3),
labels = c("Wish to live abroad +", "Wish to live abroad -", "Wish to live abroad DN"))
Datatest$fc_learn <- factor(Datatest$fc_learn,
levels = c(1,2,3),
labels = c("Learn about other cultures +", "Learn about other cultures -", "Learn about other cultures DN"))
Datatest$fc_media <- factor(Datatest$fc_media,
levels = c(1,2,3,4,5),
labels = c("Media other cultures --", "Media other cultures -", "Media other cultures -+", "Media other cultures +", "Media other cultures ++"))
Datatest$friends_gr <- factor(Datatest$friends_gr,
levels = c(1,2,3),
labels = c("Friends multicultural +", "Friends multicultural -", "Friends multicultural DN"))
Datatest$lg_interest <- factor(Datatest$lg_interest,
levels = c(1,2,3,4),
labels = c("Foreign languages for work", "Foreign languages for communication", "Dont need to learn foreign languages", "Foreign languages Other"))
Datatest$hc_proud_gr <- factor(Datatest$hc_proud_gr,
levels = c(0,1),
labels = c("Country proud -", "Country proud +"))
Datatest$hc_unique_gr <- factor(Datatest$hc_unique_gr,
levels = c(0,1),
labels = c("Country unique -", "Country unique +"))
Datatest$hc_fc_culture_gr <- factor(Datatest$hc_fc_culture_gr,
levels = c(0,1),
labels = c("Cultural difference -", "Cultural difference +"))
Datatest$fp_interact_fun_gr <- factor(Datatest$fp_interact_fun_gr,
levels = c(0,1),
labels = c("Interaction fun -", "Interaction fun +"))
Datatest$fc_know_gr <- factor(Datatest$fc_know_gr,
levels = c(0,1),
labels = c("Knowledge -", "Knowledge +"))
Datatest$fc_want_know_gr <- factor(Datatest$fc_want_know_gr,
levels = c(0,1),
labels = c("Interest -", "Interest +"))
Datatest$fc_respect_gr <- factor(Datatest$fc_respect_gr,
levels = c(0,1),
labels = c("Respect -", "Respect +"))
Datatest$fp_problem_gr <- factor(Datatest$fp_problem_gr,
levels = c(0,1),
labels = c("Insecurity -", "Insecurity +"))
Datatest$fp_gap_fun_gr <- factor(Datatest$fp_gap_fun_gr,
levels = c(0,1),
labels = c("Enjoy difference -", "Enjoy difference +"))
Datatest$fp_interactc_active_gr <- factor(Datatest$fp_interactc_active_gr,
levels = c(0,1),
labels = c("Interaction active -", "Interaction active +"))
#Select active and supplementary variables for MCA
selected_vars <- c("hc_proud_gr", "hc_unique_gr", "hc_fc_culture_gr", "fp_interact_fun_gr",
"fc_know_gr", "fc_want_know_gr", "fc_respect_gr", "fp_problem_gr",
"fp_gap_fun_gr", "fp_interactc_active_gr")
selected_data <- Datatest[selected_vars]
selected_sup <- c("fc_media", "future_country", "fc_learn",
"friends_gr", "lg_interest")
selected_data_sup <- Datatest[selected_sup]
#check selected variables if they are factor variables
selected_data$hc_proud_gr <-as.factor(selected_data$hc_proud_gr)
selected_data$hc_unique_gr <-as.factor(selected_data$hc_unique_gr)
selected_data$hc_fc_culture_gr <-as.factor(selected_data$hc_fc_culture_gr)
selected_data$fp_interact_fun_gr <-as.factor(selected_data$fp_interact_fun_gr)
selected_data$fc_know_gr <-as.factor(selected_data$fc_know_gr)
selected_data$fc_want_know_gr <-as.factor(selected_data$fc_want_know_gr)
selected_data$fc_respect_gr <-as.factor(selected_data$fc_respect_gr)
selected_data$fp_problem_gr <-as.factor(selected_data$fp_problem_gr)
selected_data$fp_gap_fun_gr <-as.factor(selected_data$fp_gap_fun_gr)
selected_data$fp_interactc_active_gr <-as.factor(selected_data$fp_interactc_active_gr)
selected_data_sup$fc_media <-as.factor(selected_data_sup$fc_media)
selected_data_sup$future_country <-as.factor(selected_data_sup$future_country)
selected_data_sup$fc_learn <-as.factor(selected_data_sup$fc_learn)
selected_data_sup$friends_gr <-as.factor(selected_data_sup$friends_gr)
selected_data_sup$lg_interest <-as.factor(selected_data_sup$lg_interest)
run mca for active variables
cats = apply(selected_data, 2, function(x) nlevels(as.factor(x)))
mca1 = MCA(selected_data, graph = FALSE)
make plot
mca1_vars_df = data.frame(mca1$var$coord, Variable = rep(names(cats), cats))
mca1_obs_df = data.frame(mca1$ind$coord)
ggplot(data=mca1_vars_df,
aes(x = Dim.1, y = Dim.2, label = rownames(mca1_vars_df))) +
geom_hline(yintercept = 0, colour = "gray70") +
geom_vline(xintercept = 0, colour = "gray70") +
geom_text(aes(colour=Variable)) +
ggtitle("MCA plot of Intercultural competency variables")
#show available stats information
mca1
#percentage of dimensions
mca1$eig
#percentage of contribution of variables
mca1$var$contrib
add supplementary variables to the plot
mca1_sup = MCA(selected_data_sup, graph = FALSE)
supp_vars_df = data.frame(mca1_sup$ind$coord)
use fviz_mca_var() to add supplementary variable categories to the plot
fviz_mca_var(mca1_sup, col.var = "blue", ggtheme = theme_minimal()) +
geom_hline(yintercept = 0, colour = "gray70") +
geom_vline(xintercept = 0, colour = "gray70") +
geom_text(data = mca1_vars_df, aes(x = Dim.1, y = Dim.2, label = rownames(mca1_vars_df), colour = Variable)) +
ggtitle("MCA plot with Supplementary Variable Categories")+
theme(text = element_text(size = 6), # adjust overall text size
axis.title = element_text(size = 6), # adjust axis title size
axis.text = element_text(size = 4), # adjust axis text size
legend.text = element_text(size = 4), # adjust legend text size
plot.title = element_text(size = 10))
#stats info on the coordinates of the supplementary variables
Extract coordinates of all modalities of supplementary variables for Dimension 1 and Dimension 2
supp_vars_coords <- as.data.frame(mca1_sup$var$coord)
Display coordinates for all modalities of supplementary variables on Dimension 1 and Dimension 2
print("Supplementary Variables Coordinates (Dimension 1 and Dimension 2):")
print(supp_vars_coords)
Can anyone please tell me if I am doing this correctly?
The values I am getting differ from those of an MCA conducted with the same data in a different program.
Thank you very very very much.
Hi everyone. I am new here and trying to become friends with R studio.
I want to add supplementary variables to my MCA in R.
I use packages : FactoMineR, ggplot2, factoextra, and tidyverse.
I first made two sets of selected variables, "selected_data" for active variables, and "selected_sup", for the supplementary variables I want to add after running MCA on the active variables.
The active variables are 10 variables (yes/no) about intercultural competency.
I made a plot using the following syntax, which worked and gave me a nice plot.
cats = apply(selected_data, 2, function(x) nlevels(as.factor(x)))
mca1 = MCA(selected_data, graph = FALSE)
mca1_vars_df = data.frame(mca1$var$coord, Variable = rep(names(cats), cats))
mca1_obs_df = data.frame(mca1$ind$coord)
ggplot(data=mca1_vars_df,
aes(x = Dim.1, y = Dim.2, label = rownames(mca1_vars_df))) +
geom_hline(yintercept = 0, colour = "gray70") +
geom_vline(xintercept = 0, colour = "gray70") +
geom_text(aes(colour=Variable)) +
ggtitle("MCA plot of Intercultural competency variables")
Now I want to add the supplementary variables to see if those are involved in the dimensions.
The supplementary variables are about experience abroad, media exposure, knowledge on foreign languages etc, and those are categorical with 3-5 levels.
I tried several things but I can't get any results.
Is anyone willing to help me?
Thank you very much in advance.