### Install and Load Packages Source: http://factominer.free.fr/more/Murcie_husson.R Installs and loads necessary R packages for data analysis. ```R if (!(require(FactoMineR))) install.packages("FactoMineR") if (!(require(Factoshiny))) install.packages("Factoshiny") if (!(require(FactoInvestigate))) install.packages("FactoInvestigate") if (!(require(missMDA))) install.packages("missMDA") ``` -------------------------------- ### Install RExcel via R Console Source: http://factominer.free.fr/more/excel-fr.html Instructions to install RExcel and its dependencies using R commands. ```R install.packages("RExcelInstaller") library(RExcelInstaller) installRExcel() library(rcom) installstatconnDCOM() ``` -------------------------------- ### Basic PCA Example Source: http://factominer.free.fr/more/code_pca.r A simple example of creating a matrix and performing PCA. ```R mat=matrix(rnorm(7*200,0,1),ncol=200) PCA(mat) ``` -------------------------------- ### Install Companion Packages Source: http://factominer.free.fr/install.html Commands to install companion packages for FactoMineR. ```r install.packages(c("Factoshiny","missMDA","FactoInvestigate")) ``` -------------------------------- ### PCA with missing values example Source: http://factominer.free.fr/course/missing.html Example code for performing PCA with missing values using missMDA. ```R library(missMDA) data(orange) nb = estim_ncpPCA(orange,ncp.max=5) res.comp = imputePCA(orange,ncp=2) res.pca = PCA(res.comp$completeObs) ``` -------------------------------- ### Multiple imputation for continuous variables example Source: http://factominer.free.fr/course/missing.html Example code for generating multiple imputed data sets for continuous variables using missMDA. ```R library(missMDA) data(orange) nb = estim_ncpPCA(orange,ncp.max=5) resMI = MIPCA(orange,ncp=2) plot(resMI) ``` -------------------------------- ### Load Companion Packages Source: http://factominer.free.fr/install.html Commands to load companion packages into an R session. ```r library(Factoshiny) library(missMDA) library(FactoInvestigate) ``` -------------------------------- ### Data Loading and Preparation Source: http://factominer.free.fr/libra/gastos.csv Loads the 'gastos' dataset from a CSV file and calculates basic statistics. ```R library(FactoMineR) gastos <- read.table("http://factominer.free.fr/libra/gastos.csv",header=TRUE, sep=";", row.names=1) apply(gastos[1:7,],2,mean) apply(gastos[1:7,],2,sd)*sqrt(6/7) apply(gastos[1:7,],2,sd)*sqrt(6/7)/apply(gastos[1:7,],2,mean) ``` -------------------------------- ### write.infile function example Source: http://factominer.free.fr/factomethods/tools-functions.html Example of using the write.infile function to save a result or object to a file, with options to specify the file name and the separator character. ```R library(FactoMineR) data(decathlon) res.pca = PCA(decathlon, quanti.sup=11:12, quali.sup=13, graph=FALSE) write.infile(decathlon, file="decathlon.xls", sep="\t") write.infile(res.pca$eig, file="eigenvalues.txt", sep="\t") ``` -------------------------------- ### MCA with missing values example Source: http://factominer.free.fr/course/missing.html Example code for performing MCA with missing values using missMDA. ```R library(missMDA) data(vnf) nb = estim_ncpMCA(vnf,ncp.max=5) tab.disj = imputeMCA(vnf, ncp=4)$tab.disj res.mca = MCA(vnf,tab.disj=tab.disj) ``` -------------------------------- ### Scaling vs. Non-Scaling in PCA Source: http://factominer.free.fr/more/exercise.r This example compares the results of PCA when scaling is applied versus when it is not applied, highlighting the effect of scaling on the analysis. ```R library(FactoMineR) mat <- matrix(rnorm(20*3,0,1), ncol=3) X1 <- mat[,1] X2 <- X1 + 0.2*mat[,2] X3 <- 100*mat[,3] X <- cbind.data.frame(X1,X2,X3) res <- PCA(X,scale.unit=FALSE) res_sc <- PCA(X,scale.unit=TRUE) ``` -------------------------------- ### Assessing Data Structure with PCA Source: http://factominer.free.fr/more/exercise.r This example demonstrates how to assess the structure of data by calculating the 95th quantile of the second eigenvalue's percentage of variance over 1000 random matrices. ```R library(FactoMineR) nr <- 50 nc <- 8 iner=rep(0,1000) for (i in 1:1000) { mat <- matrix(rnorm(nr*nc,0,1), ncol=nc) iner[i]=PCA(mat,graph=F)$eig[2,3] } quantile(iner,0.95) ``` -------------------------------- ### PCA Example Source: http://factominer.free.fr/libra/naranja.csv This code snippet shows a complete example of performing PCA on a dataset, including data loading, PCA calculation, and result summarization. ```R library(FactoMineR) naranja <- read.table("http://factominer.free.fr/libra/naranja.csv",header=TRUE, sep=";", dec=".", row.names=1) summary(naranja) res.pca <- PCA(naranja,quanti.sup=8:15,quali.sup=16:17) plot(res.pca, invisible="quali") round(res.pca$var$coord[,1:2],2) round(res.pca$eig,2) round(res.pca$ind$dist,2) round(res.pca$ind$contrib[,1:2],2) round(res.pca$var$contrib[,1:2],2) lapply(dimdesc(res.pca),lapply,round,2) ``` -------------------------------- ### Install FactoMineR Package Source: http://factominer.free.fr/install.html Command to install the FactoMineR package from CRAN. ```r install.packages("FactoMineR") ``` -------------------------------- ### Visualizing Missing Data Patterns Source: http://factominer.free.fr/missMDA/ecological.csv Uses the VIM package to visualize missing data patterns and summarizes the missing data combinations. ```R library(VIM) aggr(Ecolo) aggr(Ecolo,only.miss=TRUE,numbers=TRUE,sortVar=TRUE) res <- summary(aggr(Ecolo,prop=TRUE,combined=TRUE))$combinations res[rev(order(res[,2])),] ``` -------------------------------- ### Poulet PCA Analysis Source: http://factominer.free.fr/livreV2/poulet.csv This code snippet loads the 'poulet' dataset, performs PCA, and generates various plots. ```R library(FactoMineR) poulet <- read.table("http://factominer.free.fr/livre/poulet.csv",header=TRUE,sep=";",dec=".",row.names=1) poulet <- as.data.frame(t(poulet)) regime <- as.factor(c(rep("N",6),rep("J16",5),rep("J16R5",8),rep("J16R16",9),rep("J48",6),rep("J48R24",9))) poulet <- cbind.data.frame(regime,poulet) colnames(poulet)[1] <- "Régime" res.pca <- PCA(poulet,quali.sup=1) plot(res.pca, habillage=1, cex=0.7) plot(res.pca, choix="var", invisible="var") points(res.pca$var$coord[,1:2], cex=0.5) dimdesc(res.pca,proba=1e-5) plot(res.pca, habillage=1, axes=3:4) plot(res.pca, choix="var", invisible="var", axes=3:4) points(res.pca$var$coord[,3:4], cex=0.5) plotellipses(res.pca) plotellipses(res.pca,axes=3:4) ``` -------------------------------- ### reconst function example Source: http://factominer.free.fr/factomethods/tools-functions.html Example of using the reconst function to reconstruct a data set from the result of a PCA or MFA, specifying the number of dimensions to use. ```R library(FactoMineR) data(decathlon) res.pca = PCA(decathlon, quanti.sup = 11:12, quali.sup=13, graph=FALSE) rec = reconst(res.pca,ncp=5) ``` -------------------------------- ### Basic MCA and plotting Source: http://factominer.free.fr/libra/te.csv Demonstrates loading data, performing MCA, and generating various plots. ```R library(FactoMineR) te <- read.table("http://factominer.free.fr/libra/te.csv",header=TRUE,sep=";") summary(te) res.mca<-MCA(te, quanti.sup=22, quali.sup=c(19:21,23:36)) plot(res.mca,invisible=c("var","quali.sup"),cex=0.7) plot(res.mca,invisible=c("ind","quali.sup")) plot(res.mca,invisible="quali.sup") plot(res.mca,invisible="ind") plot(res.mca,invisible=c("ind","var")) ``` -------------------------------- ### Tea Dataset Example Source: http://factominer.free.fr/factomethods/datasets/tea.txt A snippet showing a portion of the tea dataset with various attributes. ```text "299" "breakfast" "tea time" "Not.evening" "Not.lunch" "Not.dinner" "always" "home" "Not.work" "Not.tearoom" "friends" "Not.resto" "Not.pub" "black" "lemon" "sugar" "tea bag" "tea shop" "p_upscale" 73 "F" "non-worker" "Not.sportsman" "+60" "+2/day" "Not.escape-exoticism" "Not.spirituality" "Not.healthy" "diuretic" "friendliness" "Not.iron absorption" "feminine" "sophisticated" "slimming" "No.exciting" "relaxing" "effect on health" "300" "breakfast" "Not.tea time" "Not.evening" "Not.lunch" "Not.dinner" "Not.always" "home" "Not.work" "Not.tearoom" "Not.friends" "Not.resto" "Not.pub" "black" "milk" "sugar" "tea bag" "chain store" "p_private label" 41 "M" "employee" "sportsman" "35-44" "1/day" "Not.escape-exoticism" "Not.spirituality" "healthy" "diuretic" "friendliness" "Not.iron absorption" "Not.feminine" "sophisticated" "No.slimming" "No.exciting" "relaxing" "No.effect on health" ``` -------------------------------- ### Import Datasets for MFA Source: http://factominer.free.fr/more/Murcie_husson.R Imports multiple datasets for performing Multiple Factor Analysis (MFA). ```R Expert <- read.table("http://factominer.free.fr/more/Expert_wine.csv",header=TRUE, sep=";", row.names=1) Consu <- read.table("http://factominer.free.fr/more/Consumer_wine.csv",header=T,sep=";",row.names=1) Stud <- read.table("http://factominer.free.fr/more/Student_wine.csv",header=T,sep=";",row.names=1) Pref <- read.table("http://factominer.free.fr/more/Preference_wine.csv",header=T,sep=";",row.names=1) Expert <- read.table("Expert_wine.csv",header=TRUE, sep=";", row.names=1) Consu <- read.table("Consumer_wine.csv",header=T,sep=";",row.names=1) Stud <- read.table("Student_wine.csv",header=T,sep=";",row.names=1) Pref <- read.table("Preference_wine.csv",header=T,sep=";",row.names=1) complet <- cbind.data.frame(Expert[,1:28],Consu[,2:16],Stud[,2:16],Pref) palette(c("black","red","blue","orange","darkgreen","maroon","darkviolet")) ``` -------------------------------- ### Principal Component Analysis (PCA) Source: http://factominer.free.fr/more/code_tutorial.r Example of performing PCA on expert wine data, including supplementary variables and plotting. ```R library(FactoMineR) ###### PCA Expert <- read.table("http://factominer.free.fr/more/Expert_wine.csv",header=TRUE, sep=";", row.names=1) res.pca <- PCA(Expert,quanti.sup=29:30,quali.sup=1) plot.PCA(res.pca,habillage=1) res.pca x11() barplot(res.pca$eig[,1],main="Eigenvalues",names.arg=1:nrow(res.pca$eig)) plot.PCA(res.pca,habillage=1) res.pca$ind$coord res.pca$ind$cos2 res.pca$ind$contrib dimdesc(res.pca) ``` -------------------------------- ### Hierarchical Clustering on Principal Components (HCPC) Source: http://factominer.free.fr/more/code_tutorial.r Example of performing HCPC on the results of an MFA. ```R ##### Hierarchical clustering res.hcpc <- HCPC(res.mfa) plot(res.hcpc,choice="map",draw.tree=F) plot(res.hcpc,palette=palette()) ``` -------------------------------- ### Data Loading Source: http://factominer.free.fr/more/souris.csv Reads the mouse dataset from a CSV file, specifying the separator, header, and row names. ```R mouse=read.table("http://factominer.free.fr/more/souris.csv",header=T,sep=";",row.names=1) ## setwd("D:/Documents and Settings/Husson/Bureau/ecole doctorale") ## mouse=read.table("souris.csv",header=T,sep=";",row.names=1) ``` -------------------------------- ### Using Factoshiny with PCA Source: http://factominer.free.fr/graphs/factoshiny-fr.html Example of how to use Factoshiny for Principal Component Analysis (PCA) by directly providing the dataset. ```r library(Factoshiny) PCAshiny(Mydata) ``` -------------------------------- ### Pairs Plot Source: http://factominer.free.fr/libra/gastos.csv Generates a pairs plot for the first 7 individuals and first 4 variables. ```R pairs(gastos[1:7,1:4]) ``` -------------------------------- ### Use Factoshiny for Interactive Graphs Source: http://factominer.free.fr/more/Murcie_husson.R Generates interactive graphs from PCA results using Factoshiny. ```R library(Factoshiny) res.shiny <- PCAshiny(Expert) ``` -------------------------------- ### Import Dataset and Perform PCA Source: http://factominer.free.fr/more/Murcie_husson.R Imports a dataset and performs Principal Component Analysis (PCA) using FactoMineR. ```R setwd("C:/Users/husson/AOBox/Travail/huss/Divers/site_Facto/more/") Expert <- read.table("http://factominer.free.fr/more/Expert_wine.csv", header=TRUE, sep=";", row.names=1) Expert <- read.table("Expert_wine.csv", header=TRUE, sep=";", row.names=1) library(FactoMineR) res.pca <- PCA(Expert,quanti.sup=29:30,quali.sup=1) summary(res.pca) plot(res.pca,hab=1,cex=.8) ``` -------------------------------- ### Individual Data Plotting Source: http://factominer.free.fr/libra/gastos.csv Plots individual data points with custom axes and labels. ```R par(las=2) plot(gastos[1:7,21],type="b",axes=F,ylab="Communicationes (en Euros)",xlab="",bty="o") axis(2) axis(1,1:7,rownames(gastos)[1:7]) par(las=0) ``` -------------------------------- ### Principal Component Analysis (PCA) Source: http://factominer.free.fr/libra/gastos.csv Performs PCA on the 'gastos' dataset, including supplementary individuals and variables, and displays results. ```R res.pca <- PCA(gastos,ind.sup=8:18,quanti.sup=27:ncol(gastos)) res.pca round(res.pca$eig,2) barplot(res.pca$eig[,1], main="Valores propios",names.arg=paste("dim",1:nrow(res.pca$eig))) plot.PCA(res.pca, choix="ind", invisible="ind.sup") round(cbind(res.pca$ind$coord[,1:3],res.pca$ind$cos2[,1:3],res.pca$ind$contrib[,1:3]),2) plot.PCA(res.pca, choix="var", invisible="quanti.sup") round(cbind(res.pca$var$coord[,1:3],res.pca$var$cos2[,1:3],res.pca$var$contrib[,1:3]),2) round(cor(gastos[,c(1,4,17)]),2) plot.PCA(res.pca, choix="var", invisible="var") round(cbind(res.pca$quanti.sup$coord[,1:3],res.pca$quanti.sup$cos2[,1:3]),2) dimdesc(res.pca) round(cbind(res.pca$ind.sup$coord[,1:4],res.pca$ind.sup$cos2[,1:4],2) plot(res.pca, choix="ind", axes=2:3) plot(res.pca, choix="var", axes=2:3) res.pca$call$centre res.pca$call$ecart.type round(scale(gastos[1:7,c(5,6,15,17,19,21)]),2)*sqrt(6/7) ``` -------------------------------- ### Load Data and Perform MCA Source: http://factominer.free.fr/bookV2/gmo.csv This snippet loads the GMO dataset, performs an MCA analysis, and displays the results. It includes data preprocessing steps like adjusting factor levels. ```R library(FactoMineR) gmo <- read.table("http://factominer.free.fr/book/gmo.csv",header=TRUE,sep=";") summary(gmo[,1:16]) levels(gmo$Position.Al.H)[4] <- levels(gmo$Position.Al.H)[1] levels(gmo$Position.Culture) <- c("Favourable","Somewhat Against","Totally opposed","Favourable") summary(gmo[,1:16]) summary(gmo[,17:21], maxsum=Inf) res <- MCA(gmo, ncp=5, quali.sup=17:21, graph = FALSE) res ``` -------------------------------- ### MFA Function Usage Example Source: http://factominer.free.fr/course/doc/script_MFA_WinesSenso.R Demonstrates how to use the MFA function with the wine dataset, specifying groups, types, number of components, group names, and supplementary groups. ```R library(FactoMineR) ?MFA data(wine) res <- MFA(wine, group=c(2,5,3,10,9,2), type=c("n",rep("s",5)), ncp=5, name.group=c("orig","smell","vis","smellAf","tasting","pref"), num.group.sup=c(1,6)) ``` -------------------------------- ### Install FactoMineR Rcmdr plugin via R Console Source: http://factominer.free.fr/more/excel-fr.html Command to install the FactoMineR Rcmdr plugin from a URL. ```R source("http://factominer.free.fr/install-facto.r") ``` -------------------------------- ### MFA Function Example Source: http://factominer.free.fr/more/gene.csv An example demonstrating how to use the MFA function for multivariate analysis with different variable types. ```R library(FactoMineR) comp <- read.table("http://factominer.free.fr/more/gene.csv",sep=";",header=T,row.names=1) res.mfa <- MFA(comp,group=c(68,356,1),type=c(rep("s",2),"n"),name.group=c("CGH","expr","WHO"),num.group.sup=3,graph=FALSE) plot.MFA(res.mfa,habillage="type",lab.ind.moy=F) plot.MFA(res.mfa,invisible="quali.sup",partial="all",habillage="group") plot.MFA(res.mfa,invisible="ind",partial="all",habillage="group") plot.MFA(res.mfa,choix="var",habillage="group") plot.MFA(res.mfa,choix="var",habillage="group",lab.var=F) plot.MFA(res.mfa,choix="group") plotellipses(res.mfa,keepvar="type") dimdesc(res.mfa) ``` -------------------------------- ### Multiple Imputation and Visualization Example Source: http://factominer.free.fr/missMDA/trustMI.html Example of performing multiple imputation using MIPCA and visualizing the uncertainty with plot.MIPCA. ```R library(missMDA) data(orange) nbdim = estim_ncpPCA(orange) # estimate the number of dimensions to impute plot(res.comp) ``` -------------------------------- ### coeffRV function example Source: http://factominer.free.fr/factomethods/tools-functions.html Example of using the coeffRV function to calculate the RV coefficient between two matrices and test its significance. ```R library(FactoMineR) data(wine) X = wine[,3:7] Y = wine[,11:20] coeffRV(X,Y) ``` -------------------------------- ### Loading Package and Data, Launching catdes Function Source: http://factominer.free.fr/factomethods/categories-description.html This snippet shows how to load the FactoMineR package, load the 'tea' dataset, and then launch the catdes function to analyze the 'age_Q' variable. ```r library(FactoMineR) data(tea) res = catdes(tea, num.var=23, proba=0.05) #tea: the data set used #num.var: the indice of the variable to characterize #proba: the significance threshold considered to characterize the category (by default 0.05) ``` -------------------------------- ### textual function example Source: http://factominer.free.fr/factomethods/tools-functions.html Example of using the textual function to calculate the number of occurrences of each word and return a contingency table. ```R library(FactoMineR) data(poison.text) res.text = textual(poison.text, num.text=3, contingence.by=c(1,2)) ``` -------------------------------- ### Data Import and Preparation Source: http://factominer.free.fr/course/doc/script_clustering_Temperature.R Sets the working directory, reads temperature data from a CSV file, and displays a summary of the data. ```R setwd("C:/Users/houee/Downloads") temperature <- read.table("data_clustering_Temperature.csv", header=TRUE, sep=";", row.names=1) summary(temperature) ``` -------------------------------- ### Clustering with MCA results Source: http://factominer.free.fr/more/code_mca.r Example of performing hierarchical clustering on MCA results and visualizing the dendrogram and cluster assignments. ```R res.mca=MCA(tea,quanti.sup=19,quali.sup=20:36,ncp=20,graph=F) library(cluster) classif = agnes(res.mca$ind$coord,method="ward") plot(classif,main="Dendrogram",ask=F,which.plots=2,labels=FALSE) clust = cutree(classif,k=3) tea.comp = cbind.data.frame(tea,res.mca$ind$coord[,1:3],factor(clust)) res.aux=MCA(tea.comp,quanti.sup=c(19,37:39),quali.sup=c(20:36,40),graph=F) plot(res.aux,invisible=c("quali.sup","var","quanti.sup"),habillage=40) catdes(tea.comp,ncol(tea.comp)) ``` -------------------------------- ### plotellipses function example Source: http://factominer.free.fr/factomethods/tools-functions.html Example of using the plotellipses function to draw confidence ellipses around the categories of a supplementary categorical variable. ```R library(FactoMineR) data(decathlon) res.pca = PCA(decathlon, quanti.sup=11:12, quali.sup=13, graph=FALSE) plotellipses(res.pca) ``` -------------------------------- ### Clustering with FactoMineR and R code Source: http://factominer.free.fr/course/MOOC.html R code examples for Clustering using the FactoMineR package, provided for the Temperature dataset. ```R Temperature dataset (course) and R code ``` -------------------------------- ### Correspondence Analysis on perfume data Source: http://factominer.free.fr/more/code_mca.r Example of performing Correspondence Analysis on the 'perfume.txt' dataset, including loading data, performing CA, and plotting results. ```R library(FactoMineR) perfume = read.table("perfume.txt",header=T,sep="\t",row.names=1) res.ca = CA(perfume,col.sup=16:39) plot(res.ca,invisible="row") plot(res.ca,invisible=c("col","col.sup")) ``` -------------------------------- ### Data Preparation and HCPC Example Source: http://factominer.free.fr/bookV2/decoupage.txt This snippet shows how to load data, prepare a quantitative variable for HCPC, and perform the HCPC analysis. ```R library(FactoMineR) tea <- read.table("http://factominer.free.fr/book/tea.csv",header=TRUE,sep=";") nb.clusters <- 4 coupure <- quantile(tea[,22], seq(0,1,1/nb.clusters)) Xqual <- cut(tea[,22],coupure, include.lowest=TRUE) summary(Xqual) hist(tea$age,col="grey",main="Histogram of the variable age",freq=FALSE, xlab="age", nclass=15) vari <- tea[,22] res.hcpc <- HCPC(vari, iter.max=10) ``` -------------------------------- ### Basic Usage of Investigate Function Source: http://factominer.free.fr/reporting/index.html Demonstrates how to load the FactoInvestigate package, perform a PCA using FactoMineR, and then use the Investigate function to generate a report. ```R library(FactoMineR) data(decathlon) res = PCA(decathlon, quanti.sup = 11:12, quali.sup=13, graph=FALSE) Investigate(res) ``` -------------------------------- ### Correlation Circle with 200 Variables and 7 Individuals Source: http://factominer.free.fr/more/exercise.r This example shows how to generate a correlation circle for a dataset with a large number of variables and a small number of individuals using FactoMineR. ```R library(FactoMineR) mat <- matrix(rnorm(7*200,0,1), ncol=200) PCA(mat) ``` -------------------------------- ### Multiple Correspondence Analysis on tea data Source: http://factominer.free.fr/more/code_mca.r Example of performing Multiple Correspondence Analysis on the 'tea' dataset, including data loading, summary, plotting, and dimension description. ```R library(FactoMineR) data(tea) summary(tea) par(ask=T) for (i in 1:6) barplot(table(tea[,i])) res.mca=MCA(tea,quanti.sup=19,quali.sup=20:36) plot(res.mca,invisible=c("var","quali.sup","quanti.sup"),cex=0.7) plot(res.mca,invisible=c("ind","quali.sup","quanti.sup"),cex=0.8) plot(res.mca,invisible=c("quali.sup","quanti.sup"),cex=0.8) ######### plot(res.mca$ind$coord[,1:2],col=as.numeric(tea[,17]),pch=20) legend("topright",legend=levels(tea[,17]),text.col=1:3,col=1:3) aa=by(res.mca$ind$coord,tea[,17],FUN=mean)[[3]][1:2] points(aa[1],aa[2],col=3,pch=15,cex=1.5) x11() plot(res.mca$ind$coord[,1:2],col=as.numeric(tea[,18]),pch=20) legend("topright",legend=levels(tea[,18]),text.col=1:6,col=1:6) bb=by(res.mca$ind$coord,tea[,18],FUN=mean)[[5]][1:2] points(bb[1],bb[2],col=5,pch=15,cex=1.5) ########## plot(res.mca,invisible=c("var","ind","quanti.sup"),cex=0.8) dimdesc(res.mca) res.mca$eig ################# aa=as.factor(rep(1:10,each=100)) bb=cbind.data.frame(aa,aa,aa,aa,aa,aa,aa,aa,aa,aa) colnames(bb)=paste("a",1:10,sep="") res=MCA(bb,graph=FALSE) res$eig[1:10,] ################# ########### burt=t(tab.disjonctif(tea[,1:18]))%*%tab.disjonctif(tea[,1:18]) res.burt=CA(burt) res.burt$eig[1:10,] ############ res.mca$ind$contrib res.mca$ind$cos2 res.mca$var$contrib res.mca$var$cos2 ``` -------------------------------- ### graph.var function example Source: http://factominer.free.fr/factomethods/tools-functions.html Example of using the graph.var function to plot variables after a principal components analysis, with options to specify which variables and labels to draw, a limit for cosine squared, and whether to create a new plot. ```R library(FactoMineR) data(decathlon) res.pca = PCA(decathlon, quanti.sup=11:12, quali.sup=13, graph=FALSE) graph.var (res.pca, draw=c("var","Points"), label=c("Long.jump","Points"), lim.cos2.var=0.5, new.plot=TRUE) ``` -------------------------------- ### PCA with FactoMineR and R code Source: http://factominer.free.fr/course/MOOC.html R code examples for Principal Component Analysis (PCA) using the FactoMineR package, provided for the Wine dataset. ```R Wine dataset (course) and R code ``` -------------------------------- ### Perform MFA Source: http://factominer.free.fr/more/Murcie_husson.R Performs Multiple Factor Analysis (MFA) on combined datasets. ```R res.mfa <- MFA(complet,group=c(1,27,15,15,60),type=c("n",rep("s",4))) summary(res.mfa) resMFA.shiny <- MFAshiny(res.mfa) ``` -------------------------------- ### Plotting ellipses Source: http://factominer.free.fr/libra/te.csv Illustrates plotting ellipses for specific variables in the MCA. ```R plotellipses(res.mca,keepvar=c("restaurante","lugar.de.compra","relajante","categoria.profesional")) ``` -------------------------------- ### MFA with FactoMineR and R code Source: http://factominer.free.fr/course/MOOC.html R code examples for Multiple Factor Analysis (MFA) using the FactoMineR package, provided for the Sensory dataset. ```R Sensory dataset (available in FactoMineR) and R code ``` -------------------------------- ### Dimension Description Source: http://factominer.free.fr/reporting/Investigate_PCA.html R code to get the description of dimensions from a PCA result. ```r dimdesc(res, axes = 1:3) ``` -------------------------------- ### Multiple Factor Analysis (MFA) - Wine Data Source: http://factominer.free.fr/more/code_tutorial.r Example of performing MFA on combined wine datasets (Expert, Consumer, Student, Preference), including plotting. ```R ###### MFA Consumer <- read.table("http://factominer.free.fr/more/Consumer_wine.csv",header=TRUE, sep=";", row.names=1) Student <- read.table("http://factominer.free.fr/more/Student_wine.csv", header=TRUE, sep=";", row.names=1) Preference <- read.table("http://factominer.free.fr/more/Preference_wine.csv", header=TRUE, sep=";", row.names=1) palette(c("black","red","blue","orange","darkgreen","maroon","darkviolet")) complet <- cbind.data.frame(Expert[,1:28],Consumer[,2:16],Student[,2:16],Preference) res.mfa <- MFA(complet,group=c(1,27,15,15,60),type=c("n",rep("s",4)),num.group.sup=c(1,5),name.group=c("Label","Expert","Consumer","Student","Preference"),graph=FALSE) plot(res.mfa,choix="group",cex=1.4) plot(res.mfa,choix="var",invisible="sup",habillage="group",cex=1.2) plot(res.mfa,choix="var",invisible="actif",habillage="none",lab.var=FALSE,cex=1.2) plot(res.mfa,choix="ind",partial="all",habillage="group",palette=palette(),cex=1.4) plot(res.mfa,choix="axes",habillage="group",palette=palette(),cex=1.4) ``` -------------------------------- ### Launch condes() function Source: http://factominer.free.fr/factomethods/continuous-variables-description.html Launches the condes() function to characterize the 'Overall quality' variable. ```R res = condes(wine, num.var=30, proba=0.05) #wine: the data set used #num.var: the indice of the variable to characterize #proba: the significance threshold considered to characterize the variable (by default 0.05) ``` -------------------------------- ### Automatic Interpretation with FactoInvestigate Source: http://factominer.free.fr/more/Murcie_husson.R Performs automatic interpretation of PCA results and saves to an HTML file. ```R library(FactoInvestigate) Investigate(res.pca,"Automatic.html") ``` -------------------------------- ### Help for PCA function Source: http://factominer.free.fr/course/doc/script_PCA_Decathlon.R Displays the help documentation for the PCA function. ```R ?PCA ``` -------------------------------- ### Multiple imputation with MCA Source: http://factominer.free.fr/missMDA/MI.html Example of generating imputed datasets using MCA with missMDA. ```R library(missMDA) data(vnf) nbdim = estim_ncpMCA(vnf,ncp.max=5) ## Time-consuming, nb = 4 res = MIMCA(vnf, ncp=4,nboot=10) plot(res) ```