### Install PCAmixdata from GitHub Source: https://github.com/chavent/pcamixdata/blob/master/README.md Installs the development version of the PCAmixdata package from GitHub. Requires the devtools package to be installed first. ```r devtools::install_github("chavent/PCAmixdata") ``` -------------------------------- ### Install devtools Package Source: https://github.com/chavent/pcamixdata/blob/master/README.md Installs the devtools package, which is a prerequisite for installing other packages from GitHub. ```r install.packages("devtools") ``` -------------------------------- ### Load PCAmixdata Library Source: https://github.com/chavent/pcamixdata/blob/master/docs/supplementary.html Loads the PCAmixdata library. Ensure the package is installed before running. ```r library(PCAmixdata) ``` -------------------------------- ### MFAmix Analysis and Supplementary Variables Source: https://github.com/chavent/pcamixdata/blob/master/docs/supplementary.html This example demonstrates how to split a dataset into quantitative and qualitative parts, perform an MFAmix analysis with defined groups, and add supplementary variables to the analysis. ```APIDOC ## MFAmix Analysis ### Description Performs Multiple Factor Analysis on mixed data types (quantitative and qualitative) and allows for the projection of supplementary variables. ### Method Function Call (R) ### Parameters - **data** (data.frame) - Required - The dataset containing both quantitative and qualitative variables. - **groups** (vector) - Required - A vector indicating the group membership of each variable. - **name.groups** (vector) - Required - Names for the variable groups. - **ndim** (integer) - Optional - Number of dimensions to keep. - **rename.level** (boolean) - Optional - Whether to rename levels. - **graph** (boolean) - Optional - Whether to plot the graph. ### Request Example ```R data(wine) X.quanti <- splitmix(wine)$X.quanti[,1:5] X.quali <- splitmix(wine)$X.quali[,1,drop=FALSE] data <- cbind(X.quanti,X.quali) groups <-c(1,2,2,3,3,1) name.groups <- c("G1","G2","G3") mfa <- MFAmix(data,groups,name.groups,ndim=4,rename.level=TRUE,graph=FALSE) ``` ### Response - **mfa** (object) - An object containing the results of the MFAmix analysis. ``` -------------------------------- ### Perform PCA on Mixed Data Source: https://github.com/chavent/pcamixdata/blob/master/docs/supplementary.html Example usage of the primary PCA function for mixed data types. ```R library(PCAmixdata) data(wine) X.quanti <- splitmix(wine$tab.num)$X.quanti X.quali <- splitmix(wine$tab.quali)$X.quali pca <- PCAmix(X.quanti, X.quali, rename.level=TRUE, graph=FALSE) summary(pca) ``` -------------------------------- ### Run MFA with Supplementary Variables in R Source: https://github.com/chavent/pcamixdata/blob/master/docs/supplementary.html This code snippet demonstrates how to perform a Multiple Factor Analysis (MFA) using the 'FactoMineR' package in R. It includes the setup for supplementary variables and plotting their contributions. ```r groups.sup <- c(1,1,2) name.groups.sup <- c("Gsup1","Gsup2") mfa.sup <- supvar(mfa,data.sup,groups.sup,name.groups.sup,rename.level=TRUE) plot(mfa.sup,choice="sqload",coloring.var="groups") ``` -------------------------------- ### Get Numerical Variable Coordinates (DUDIMIX) Source: https://github.com/chavent/pcamixdata/blob/master/docs/PCAmixcompare.html Extract the coordinates for numerical variables from a DUDIMIX analysis. The output shows correlations with the principal components. ```R dudimix$co[c(1,2,5),] ``` -------------------------------- ### Get Numerical Variable Coordinates (PCA Mix) Source: https://github.com/chavent/pcamixdata/blob/master/docs/PCAmixcompare.html Access the coordinates of numerical variables from a PCA Mix analysis. These coordinates are interpreted as correlations with the principal components. ```R pcamix$quanti$coord ``` -------------------------------- ### Get Level Coordinates Source: https://github.com/chavent/pcamixdata/blob/master/docs/PCAmixcompare.html Level coordinates represent the projection of categorical variable levels onto the factor maps. `PCAmix` and `dudi.mix` provide direct results, while `FAMD` requires scaling by the square root of eigenvalues. ```R pcamix$levels$coord ``` ```R dudimix$co[-c(1,2,5),] ``` ```R famd$quali.var$coord %*%diag(1/sqrt(pcamix$eig[1:2,1])) ``` -------------------------------- ### Load and Inspect Included Datasets Source: https://context7.com/chavent/pcamixdata/llms.txt Demonstrates loading and inspecting the structure of built-in datasets like wine, gironde, and decathlon. ```r # Wine dataset - 21 wines with sensory descriptors data(wine) str(wine) # Gironde dataset - 542 cities with grouped variables data(gironde) names(gironde) str(gironde$employment) str(gironde$services) # Decathlon dataset - athletic performance data(decathlon) dim(decathlon) ``` -------------------------------- ### Accessing supvar documentation Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Displays the help documentation for the supvar method in PCAmix. ```R ?supvar.PCAmix ``` -------------------------------- ### Initialize PCAMixData Analysis Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Initializes the data processing environment for mixed data analysis. ```R library(PCAmixdata) data(wine) X.quanti <- wine[,1:27] X.quali <- wine[,28:31] res.pcamix <- PCAmix(X.quanti=X.quanti, X.quali=X.quali, rename.level=TRUE, graph=FALSE) ``` -------------------------------- ### Get Numerical Variable Coordinates (FAMD) Source: https://github.com/chavent/pcamixdata/blob/master/docs/PCAmixcompare.html Retrieve the coordinates of numerical variables from a FAMD (Factor Analysis of Mixed Data) analysis. These values indicate the correlation with the principal dimensions. ```R famd$quanti.var$coord ``` -------------------------------- ### Prepare Data for MFA Source: https://github.com/chavent/pcamixdata/blob/master/docs/supplementary.html This snippet prepares quantitative and qualitative data from the wine dataset for MFA. Ensure necessary libraries are loaded and data is split appropriately. ```R #devtools::install_github("chavent/PCAmixdata") library(PCAmixdata) data(wine) X.quanti <- splitmix(wine)$X.quanti[,1:5] X.quali <- splitmix(wine)$X.quali[,1,drop=FALSE] X.quali.sup <- splitmix(wine)$X.quali[,2,drop=FALSE] data <- cbind(X.quanti,X.quali) ``` -------------------------------- ### PCA with Mixed Data Types using PCAmix Source: https://github.com/chavent/pcamixdata/blob/master/docs/supplementary.html Demonstrates how to perform PCA on a dataset with both quantitative and qualitative variables using the PCAmix function. Requires the 'wine' dataset for demonstration. The function is called with quantitative and qualitative data subsets, specifying the number of dimensions and disabling graph generation. ```R data(wine) X.quanti <- splitmix(wine)$X.quanti[,1:5] X.quali <- splitmix(wine)$X.quali n <- nrow(wine) sub <- sample(1:n,10) pca<-PCAmix(X.quanti[sub,],X.quali[sub,],ndim=4,graph=FALSE) ``` -------------------------------- ### Prepare Data for PCAmix Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Loads the 'gironde' dataset and prepares the 'housing' data by removing the first 10 observations and splitting into quantitative and qualitative variables for PCAmix. ```R library(PCAmixdata) data(gironde) #Create the datatable housing without the ten first observations train <- gironde$housing[-c(1:10), ] #Split the datatable split <- splitmix(train) X1 <- split$X.quanti X2 <- split$X.quali res.pcamix <- PCAmix(X.quanti=X1, X.quali=X2,rename.level=TRUE, graph=FALSE) ``` -------------------------------- ### Prepare and Run MFAmix Model Source: https://github.com/chavent/pcamixdata/blob/master/docs/supplementary.html Prepares data by defining groups and names, then fits an MFAmix model. Use this for initial model exploration and analysis. Ensure 'gironde' data is loaded and 'class.var' and 'names' are defined for prediction. ```R data(gironde) groups <- c(rep(1,9),rep(2,5),rep(3,9),rep(4,4)) name.groups <- c("employment","housing","services","environment") dat<-cbind(gironde$employment,gironde$housing, gironde$services,gironde$environment) n <- nrow(dat) set.seed(10) sub <- sample(1:n,520) res<-MFAmix(data=dat[sub,],groups=groups,name.groups=name.groups, rename.level=TRUE, ndim=3,graph=FALSE) ``` -------------------------------- ### Display PCAmix plot documentation Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Access the help documentation for the plot method of PCAmix objects. ```R ?plot.PCAmix ``` -------------------------------- ### Perform MFAmix Analysis and Visualization Source: https://github.com/chavent/pcamixdata/blob/master/docs/supplementary.html This snippet demonstrates how to split a mixed dataset, perform MFA, add supplementary variables, and plot the results. ```R data(wine) X.quanti <- splitmix(wine)$X.quanti[,1:5] X.quali <- splitmix(wine)$X.quali[,1,drop=FALSE] X.quanti.sup <- splitmix(wine)$X.quanti[,28:29] X.quali.sup <- splitmix(wine)$X.quali[,2,drop=FALSE] data <- cbind(X.quanti,X.quali) data.sup <- cbind(X.quanti.sup,X.quali.sup) groups <-c(1,2,2,3,3,1) name.groups <- c("G1","G2","G3") mfa <- MFAmix(data,groups,name.groups,ndim=4,rename.level=TRUE,graph=FALSE) groups.sup <- c(1,1,2) name.groups.sup <- c("Gsup1","Gsup2") mfa.sup <- supvar(mfa,data.sup,groups.sup,name.groups.sup,rename.level=TRUE) plot(mfa.sup,choice="sqload",coloring.var="groups") ``` -------------------------------- ### PCAMixData API Overview Source: https://github.com/chavent/pcamixdata/blob/master/docs/supplementary.html General documentation for the PCAMixData project endpoints. ```APIDOC ## PCAMixData API ### Description This project provides data processing capabilities for PCAMixData. Please refer to specific endpoints for detailed usage. ### Method GET ### Endpoint /chavent/pcamixdata ``` -------------------------------- ### Load PCA packages Source: https://github.com/chavent/pcamixdata/blob/master/docs/PCAmixcompare.html Load the necessary libraries for performing PCA on mixed data. ```R library(PCAmixdata) library(FactoMineR) library(ade4) ``` -------------------------------- ### Compare Squared Loadings Before Rotation Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Presents the squared loadings for the first 3 dimensions of the original PCAmix result, rounded to 2 decimal places. ```R round(res.pcamix$sqload[,1:3],digit=2) ``` -------------------------------- ### Load and inspect the gironde dataset Source: https://github.com/chavent/pcamixdata/blob/master/docs/PCAmixcompare.html Load the PCAmixdata package and inspect the housing subset of the gironde dataset. ```R library(PCAmixdata) data(gironde) housing <- gironde$housing head(housing) ``` -------------------------------- ### Prepare Training and Test Data for PCAmix Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Randomly selects 100 observations for a test sample and uses the remaining data to train the PCAmix model. ```R set.seed(10) test <- sample(1:nrow(gironde$housing),100) train.pcamix <- PCAmix(X1[-test,],X2[-test,],ndim=2,graph=FALSE) ``` -------------------------------- ### Load and Inspect gironde Dataset Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Load the PCAmixdata library and inspect the structure of the gironde dataset, which contains four distinct datatables. ```R library(PCAmixdata) data(gironde) names(gironde) ``` -------------------------------- ### Perform MFAmix Analysis and Plot Results Source: https://context7.com/chavent/pcamixdata/llms.txt Demonstrates how to perform an MFAmix analysis on mixed data and visualize the results, including individual scores, correlation circles, partial axes, group contributions, squared loadings, and qualitative levels. Ensure data is preprocessed and group variables are correctly defined. ```r data(gironde) class.var <- c(rep(1, 9), rep(2, 5), rep(3, 9), rep(4, 4)) names <- c("employment", "housing", "services", "environment") dat <- cbind(gironde$employment[1:50, ], gironde$housing[1:50, ], gironde$services[1:50, ], gironde$environment[1:50, ]) res <- MFAmix(data = dat, groups = class.var, name.groups = names, rename.level = TRUE, ndim = 3, graph = FALSE) # Plot individuals plot(res, choice = "ind", cex = 0.6, main = "Individual Scores") # Plot quantitative variables colored by group plot(res, choice = "cor", coloring.var = "groups", cex = 0.6, col.groups = c("red", "blue", "green", "orange"), main = "Correlation Circle by Group") # Plot partial axes (group-specific components) plot(res, choice = "axes", coloring.var = "groups", cex = 0.6, nb.partial.axes = 2, main = "Partial Axes") # Plot group contributions plot(res, choice = "groups", coloring.var = "groups", cex = 0.8, main = "Group Contributions") # Squared loadings colored by group membership plot(res, choice = "sqload", coloring.var = "groups", posleg = "topright", cex = 0.8, main = "Squared Loadings") # Squared loadings colored by variable type plot(res, choice = "sqload", coloring.var = "type", cex = 0.8, main = "Squared Loadings by Type") # Plot levels of qualitative variables plot(res, choice = "levels", coloring.var = "groups", cex = 0.8, main = "Qualitative Levels by Group") # Plot partial individuals (trajectories across groups) plot(res, choice = "ind", partial = c("AMBES", "ARCACHON"), cex = 0.6, posleg = "bottomright", main = "Partial Individual Trajectories") ``` -------------------------------- ### plot.PCAmix: Visualizing Supplementary Variables Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html This endpoint demonstrates how to automatically plot supplementary variables on graphical outputs using the plot S3 method. ```APIDOC ## plot.PCAmix ### Description The plot method automatically includes supplementary variables in the graphical outputs of a PCAmix analysis. ### Method S3 Method: plot ### Parameters - **x** (PCAmix object) - Required - The object containing supplementary variables. - **choice** (string) - Required - The type of plot (e.g., "cor" for correlation circle). - **main** (string) - Optional - Title of the plot. ### Request Example ```R plot(res.sup, choice="cor", main="Numerical variables") ``` ``` -------------------------------- ### Display PCArot Object Summary Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Prints a summary of the PCArot object, detailing the method used, number of iterations, and available components for analysis. ```R res.pcarot ``` -------------------------------- ### Display PCAmix Object Summary Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Prints the summary of the PCAmix object to view available components and results. ```R res.pcamix ``` -------------------------------- ### Load and Inspect Housing Data Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Loads the gironde dataset and displays the first few rows of the housing table. ```R library(PCAmixdata) data(gironde) head(gironde$housing) ``` -------------------------------- ### Generate PCAmix graphical outputs Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Create a 2x2 grid of plots for a PCAmix object, visualizing observations, levels, numerical variables, and all variables. ```R par(mfrow=c(2,2)) plot(res.pcamix,choice="ind",coloring.ind=X2$houses,label=FALSE, posleg="bottomright", main="Observations") plot(res.pcamix,choice="levels",xlim=c(-1.5,2.5), main="Levels") plot(res.pcamix,choice="cor",main="Numerical variables") plot(res.pcamix,choice="sqload",coloring.var=T, leg=TRUE, posleg="topright", main="All variables") ``` -------------------------------- ### Plot MFA Levels and Groups Source: https://github.com/chavent/pcamixdata/blob/master/docs/supplementary.html Visualizes the levels and group variables of an MFA object. Ensure the mfa.sup object is properly defined before execution. ```R plot(mfa.sup,choice="levels",coloring.var="groups") ``` -------------------------------- ### Visualizing PCA Results with Predicted Points Source: https://github.com/chavent/pcamixdata/blob/master/docs/supplementary.html Illustrates how to visualize the results of a PCAmix analysis. It plots the principal components and then overlays points predicted by the PCAmix model, colored red and with specific markers, to highlight the new data's position relative to the original components. ```R plot(pca,axes=c(1,2)) points(pred[,c(1,2)],col=2,pch=16) text(pred[,c(1,2)], labels = rownames(X.quanti[-sub,1:5]), col=2,pos=3) ``` -------------------------------- ### Perform PCA of mixed data Source: https://github.com/chavent/pcamixdata/blob/master/docs/PCAmixcompare.html Execute PCA on the housing dataset using PCAmix, FAMD, and dudi.mix functions. ```R # PCAmix (PCAmixdata) split <- splitmix(housing) pcamix <- PCAmix(X.quanti=split$X.quanti, X.quali=split$X.quali, rename.level=TRUE, graph=FALSE, ndim=2) # FAMD (FactoMineR) famd <- FAMD(housing, graph = FALSE, ncp = 2) # dudi.mix (ade4) dudimix <- dudi.mix(housing, scannf = FALSE, nf = 2) ``` -------------------------------- ### Compare Eigenvalues Across PCA Methods Source: https://github.com/chavent/pcamixdata/blob/master/docs/PCAmixcompare.html Extracts and compares the first two eigenvalues from PCAmix, dudi.mix, FADM, and MCA implementations. ```R mca.pcamix$eig[1:2,1] # PCAmix, dudi.mix, FADM ``` ```R mca.dudi$eig[1:2] # MCA with ade4 ``` ```R mca$eig[1:2,1] # MCA with FactoMineR ``` -------------------------------- ### Plotting MFAmix Graphical Outputs Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Demonstrates various plotting options for MFAmix objects, including numerical variables, observations, all variables, and group-specific visualizations. ```APIDOC ## Plotting MFAmix Graphical Outputs ### Description This section provides examples of how to generate graphical outputs from an MFAmix object using the `plot` method. These plots are similar to those from PCAmix but incorporate group information. ### Method `plot(object, choice, ...)` ### Endpoint N/A (This is a function call within an R environment) ### Parameters - **object** (MFAmix object) - The MFAmix object to plot. - **choice** (character) - The type of plot to generate. Options include "cor", "ind", "sqload", "groups". - **coloring.var** (character, optional) - Variable to use for coloring the plot elements (e.g., "groups"). - **leg** (logical, optional) - Whether to display a legend. - **partial** (character vector, optional) - For "ind" plots, specifies observations to highlight. - **label** (logical, optional) - Whether to label observations. - **posleg** (character, optional) - Position of the legend. - **main** (character, optional) - Main title for the plot. ### Request Example ```R # Assuming 'res.mfamix' is a fitted MFAmix object # Plotting numerical variables with group coloring plot(res.mfamix, choice="cor", coloring.var="groups", leg=TRUE, main="(a) Numerical variables") # Plotting observations, highlighting specific ones plot(res.mfamix, choice="ind", partial=c("SAINTE-FOY-LA-GRANDE"), label=TRUE, posleg="topright", main="(b) Observations") # Plotting squared loadings for all variables plot(res.mfamix, choice="sqload", coloring.var="groups", posleg="topright", main="(c) All variables") # Plotting group contributions plot(res.mfamix, choice="groups", coloring.var="groups", main="(c) Groups") ``` ### Response Graphical outputs displayed directly in the R plotting device. The specific fields depend on the `choice` parameter. #### Success Response (200) - **Graphical Plot**: Visual representation of the MFAmix results. #### Response Example (Visual output, no direct JSON example) ### Error Handling - If the object is not of class MFAmix, an error will occur. - Invalid `choice` parameters will result in an error. - Incorrectly specified optional parameters may lead to plotting issues or errors. ``` -------------------------------- ### Compare Levels Coordinates Source: https://github.com/chavent/pcamixdata/blob/master/docs/PCAmixcompare.html Displays the first few rows of levels coordinates from different multivariate analysis functions to compare output structures. ```R head(mca.famd$quali.var$coord) ``` ```R head(mca.pcamix$levels$coord) ``` ```R head(mca$var$coord) ``` ```R head(mca.dudi$co) ``` -------------------------------- ### Compare Squared Loadings After Rotation Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Shows the squared loadings for the rotated principal components, rounded to 2 decimal places, to observe changes after rotation. ```R round(res.pcarot$sqload,digit=2) ``` -------------------------------- ### Summarize PCAmix and PCArot Results Source: https://context7.com/chavent/pcamixdata/llms.txt Generates summary statistics including squared loadings for PCAmix results and rotated components. ```r # Perform analysis data(wine) X.quanti <- wine[, 3:29] X.quali <- wine[, 1:2] pca <- PCAmix(X.quanti, X.quali, ndim = 4, graph = FALSE) # Display summary summary(pca) # Summary after rotation rot <- PCArot(pca, dim = 3, graph = FALSE) summary(rot) ``` -------------------------------- ### Predicting PCA Components with PCAmix Source: https://github.com/chavent/pcamixdata/blob/master/docs/supplementary.html Shows how to use the predict method with a PCAmix object to predict principal components for new data. This is useful for applying a trained PCA model to unseen data. The prediction is performed on a subset of the data not used for training. ```R pred <- predict(pca,X.quanti[-sub,],X.quali[-sub,]) ``` -------------------------------- ### MCA using PCAmix (PCAmixdata package) Source: https://github.com/chavent/pcamixdata/blob/master/docs/PCAmixcompare.html Performs MCA on the categorical 'services' dataset using the PCAmix function from the PCAmixdata package. Sets rename.level to TRUE and disables graphs for cleaner output. ```r mca.pcamix <- PCAmix(X.quali=services, rename.level=TRUE, graph=FALSE, ndim=2) ``` -------------------------------- ### Load and view categorical data Source: https://github.com/chavent/pcamixdata/blob/master/docs/PCAmixcompare.html Loads the 'gironde' dataset and extracts the 'services' variable, then displays the first few rows. This is a prerequisite for performing MCA. ```r data(gironde) services <- gironde$services head(services) ``` -------------------------------- ### supvar.PCAmix: Calculating Supplementary Variable Coordinates Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html This endpoint demonstrates how to use the supvar method to obtain coordinates for supplementary numerical and categorical variables within a PCAmix analysis. ```APIDOC ## supvar.PCAmix ### Description The supvar method calculates the coordinates of supplementary numerical and categorical variables on the correlation circle of a PCAmix object. ### Method S3 Method: supvar ### Parameters - **res.pcamix** (PCAmix object) - Required - The PCAmix object to which supplementary variables are added. - **X1sup** (data.frame) - Required - Supplementary numerical variables. - **X2sup** (data.frame) - Required - Supplementary categorical variables. - **rename.level** (boolean) - Optional - If TRUE, renames levels of categorical variables. ### Request Example ```R X1sup <- gironde$environment[,1,drop=FALSE] X2sup <- gironde$services[,7,drop=FALSE] res.sup <- supvar(res.pcamix,X1sup,X2sup,rename.level=TRUE) ``` ### Response - **quanti.sup** (list) - Coordinates of supplementary numerical variables. - **levels.sup** (list) - Coordinates of levels of supplementary categorical variables. ``` -------------------------------- ### Plot PCAmix maps before and after rotation Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Visualizes observations and variables in dimensions 1 and 3 using the PCAmix and PCARot results. ```R par(mfrow=c(2,2)) plot(res.pcamix,choice="ind",label=FALSE,axes=c(1,3), main="Observations before rotation") plot(res.pcarot,choice="ind",label=FALSE,axes=c(1,3), main="Observations after rotation") plot(res.pcamix,choice="sqload", coloring.var=TRUE, leg=TRUE,axes=c(1,3), posleg="topleft", main="Variables before rotation", xlim=c(0,1), ylim=c(0,1)) plot(res.pcarot,choice="sqload", coloring.var=TRUE, leg=TRUE,axes=c(1,3), posleg="topright", main="Variables after rotation", xlim=c(0,1), ylim=c(0,1)) ``` -------------------------------- ### Verify Quasi-Barycentric Property Source: https://github.com/chavent/pcamixdata/blob/master/docs/PCAmixcompare.html This code demonstrates the quasi-barycentric property by comparing the calculated mean scores of observations belonging to a specific level with the scaled coordinates of that level from `PCAmix`. ```R barycenter <- apply(pcamix$scores[which(housing$houses==" inf 90%."),],2,mean) quasi_barycenter <- barycenter/sqrt(pcamix$eig[1:2,1]) # PCAmix coordinates of the level 'inf 90%' pcamix$levels$coord[1,, drop=FALSE] ``` ```R quasi_barycenter ``` -------------------------------- ### Verify Unchanged Explained Inertia Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Calculates and displays the sum of the proportions of variance explained by the rotated principal components, confirming it remains unchanged. ```R sum(res.pcarot$eig[,2]) ``` -------------------------------- ### Apply PCAmix to Mixed Data Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Splits the dataset into quantitative and qualitative variables and performs PCAmix analysis. ```R split <- splitmix(gironde$housing) X1 <- split$X.quanti X2 <- split$X.quali res.pcamix <- PCAmix(X.quanti=X1, X.quali=X2,rename.level=TRUE, graph=FALSE) ``` -------------------------------- ### Plot PCAmix Levels for Categorical Variables Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Visualizes the levels of categorical variables in PCAmix, colored by their group. Useful for understanding how these variables interpret the first principal component. Ensure 'res.mfamix' object is available and 'services' group is defined. ```R plot(res.mfamix, choice="levels", coloring.var="groups", posleg="bottomleft", main="(c) Levels",xlim=c(-2,4)) ``` -------------------------------- ### plot.PCAmix - Visualization Source: https://context7.com/chavent/pcamixdata/llms.txt Generates various plots for exploring PCAmix results including individual scores, correlation circles, and squared loadings. ```APIDOC ## plot.PCAmix ### Description Visualizes PCAmix results through different plot types. ### Parameters - **x** (PCAmix object) - Required - The analysis result. - **choice** (string) - Required - Type of plot: 'ind' (individuals), 'cor' (correlation circle), 'levels' (qualitative levels), or 'sqload' (squared loadings). - **axes** (vector) - Optional - The two axes to plot. - **coloring.ind** (vector) - Optional - Qualitative variable to color individuals by. - **lim.cos2.plot** (numeric) - Optional - Threshold for filtering individuals by squared cosine quality. ``` -------------------------------- ### Apply MFAmix with Supplementary Groups Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Configures MFAmix with three active groups and adds a fourth group as supplementary data. ```R dat <- cbind(gironde$employment,gironde$services,gironde$environment) names <- c("employment","services","environment") mfa <-MFAmix(data=dat,groups=c(rep(1,9), rep(2,9),rep(3,4)), name.groups=names, rename.level=TRUE,graph=FALSE) mfa.sup <- supvar(mfa,data.sup=gironde$housing, groups.sup=rep(1,5), name.groups.sup="housing.sup",rename.level=TRUE) ``` -------------------------------- ### Check Class of MFAmix Object Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Use the class() function to verify that the result is an MFAmix object. ```r class(res.mfamix) ``` -------------------------------- ### Perform MFA with Groups Source: https://github.com/chavent/pcamixdata/blob/master/docs/supplementary.html This snippet performs Mixed Factor Analysis (MFA) on prepared data, defining groups and their names. Set 'graph=FALSE' to prevent plot generation during execution. ```R groups <-c(1,1,1,2,2,2) name.groups <- c("G1","G2") mfa <- MFAmix(data,groups,name.groups,ndim=4,rename.level=TRUE,graph=FALSE) ``` -------------------------------- ### Plot PCA Results Source: https://github.com/chavent/pcamixdata/blob/master/docs/supplementary.html Generates a plot of PCA results, specifically focusing on 'sqload' (squared loadings). Ensure the 'pca2' object is available in your environment. ```R plot(pca2,choice="sqload") ``` -------------------------------- ### MFAmix Object Structure Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Details the components and attributes available within the MFAmix object class. ```APIDOC ## MFAmix Object Structure ### Description The MFAmix object contains the comprehensive results of a Multiple Factor Analysis for mixed data, including eigenvalues, individual results, and variable loadings. ### Object Components - **$eig** (numeric) - Eigenvalues of the analysis. - **$eig.separate** (numeric) - Eigenvalues of the separate analyses. - **$separate.analyses** (list) - Results of separate analyses for each group of variables. - **$groups** (list) - Results for all variable groups. - **$partial.axes** (list) - Results for the partial axes. - **$ind** (list) - Results for the individuals. - **$ind.partial** (list) - Results for the partial individuals. - **$quanti** (list) - Results for the quantitative variables. - **$levels** (list) - Results for the levels of the qualitative variables. - **$quali** (list) - Results for the qualitative variables. - **$sqload** (list) - Squared loadings. - **$listvar.group** (list) - List of variables in each group. - **$global.pca** (list) - Results for the global PCA. ``` -------------------------------- ### Perform PCA on Mixed Data with PCAmix Source: https://context7.com/chavent/pcamixdata/llms.txt Use PCAmix for datasets with both quantitative and qualitative variables. Handles missing values by imputation. Suppress plotting with graph = FALSE. ```r library(PCAmixdata) data(wine) X.quanti <- splitmix(wine)$X.quanti X.quali <- splitmix(wine)$X.quali pca <- PCAmix( X.quanti = X.quanti[, 1:27], X.quali = X.quali, ndim = 4, rename.level = TRUE, graph = FALSE ) print(pca$eig) head(pca$ind$coord) head(pca$quanti$coord) print(pca$sqload) print(pca$levels$coord) ``` -------------------------------- ### Visualize PCAMixData Groups Source: https://github.com/chavent/pcamixdata/blob/master/docs/supplementary.html Use the plot function to visualize groups within a PCAMixData object. Ensure the object is properly initialized before calling this function. ```R plot(mfa.sup,choice="groups",coloring.var="groups") ``` -------------------------------- ### Plot Observations on Principal Component Map Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Visualizes the training data and overlays the predicted test observations on the principal component map. ```R plot(train.pcamix,axes=c(1,2),label=FALSE,main="Observations map") points(pred,col=2,pch=16) legend("bottomright",legend = c("train","test"),fill=1:2,col=1:2) ``` -------------------------------- ### Add Supplementary Variables to PCAmix Source: https://github.com/chavent/pcamixdata/blob/master/docs/supplementary.html Performs a PCAmix analysis and then adds supplementary quantitative and qualitative variables. Use this when you have a primary PCA analysis and want to project additional variables onto it. ```r data(wine) X.quanti <- splitmix(wine)$X.quanti[,1:5] X.quali <- splitmix(wine)$X.quali[,1,drop=FALSE] X.quanti.sup <-splitmix(wine)$X.quanti[,28:29] X.quali.sup <-splitmix(wine)$X.quali[,2,drop=FALSE] pca<-PCAmix(X.quanti,X.quali,ndim=4,graph=FALSE) pca2 <- supvar(pca,X.quanti.sup,X.quali.sup) plot(pca2,choice="levels") ``` -------------------------------- ### Visualize PCAmix Results Source: https://context7.com/chavent/pcamixdata/llms.txt Generates various plots for PCAmix results, including individual scores, correlation circles for quantitative variables, level maps for qualitative variables, and squared loading plots. Customize plots using arguments like 'choice', 'axes', 'coloring.ind', and 'lim.cos2.plot'. ```R # Perform analysis data(gironde) base <- gironde$housing[1:50, ] X.quanti <- splitmix(base)$X.quanti X.quali <- splitmix(base)$X.quali res <- PCAmix(X.quanti, X.quali, rename.level = TRUE, ndim = 3, graph = FALSE) # Plot individuals (scores) plot(res, choice = "ind", axes = c(1, 2), cex = 0.8, main = "Individual Scores") # Plot individuals colored by a qualitative variable houses <- X.quali$houses plot(res, choice = "ind", coloring.ind = houses, cex = 0.6, posleg = "bottomright", main = "Scores by Housing Type") # Filter individuals by squared cosine quality plot(res, choice = "ind", lim.cos2.plot = 0.5, cex = 0.8, main = "Individuals with cos2 > 0.5") # Correlation circle for quantitative variables plot(res, choice = "cor", axes = c(1, 2), cex = 0.8, main = "Correlation Circle") # Component map for qualitative variable levels plot(res, choice = "levels", axes = c(1, 2), cex = 0.8, main = "Qualitative Levels") # Squared loadings plot (shows all variables together) plot(res, choice = "sqload", coloring.var = TRUE, cex = 0.8, main = "Squared Loadings by Variable Type") ``` -------------------------------- ### Visualize MFAmix objects Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Generates various plots for an MFAmix object, including correlations, observations, variable loadings, and group contributions. ```R ?plot.MFAmix ## Rendering development documentation for 'plot.MFAmix' par(mfrow=c(2,2)) plot(res.mfamix, choice="cor",coloring.var="groups",leg=TRUE, main="(a) Numerical variables") plot(res.mfamix,choice="ind", partial=c("SAINTE-FOY-LA-GRANDE"), label=TRUE, posleg="topright", main="(b) Observations") plot(res.mfamix,choice="sqload",coloring.var="groups", posleg="topright",main="(c) All variables") plot(res.mfamix, choice="groups", coloring.var="groups", main="(c) Groups") ``` -------------------------------- ### Plot PCAMixData Levels Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Visualizes the levels of the PCAMixData result object. ```R plot(res.sup,choice="levels",main="Levels",xlim=c(-2,2.5)) ``` -------------------------------- ### Visualize MFA Results Source: https://github.com/chavent/pcamixdata/blob/master/docs/supplementary.html Generates a plot for MFA results, allowing for axis selection and group-based coloring. ```R plot(mfa.sup,choice="axes",coloring.var="groups") ``` -------------------------------- ### Plotting supplementary variables Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Visualizes supplementary variables on graphical outputs using the plot method. ```R #par(mfrow=c(2,1)) plot(res.sup,choice="cor",main="Numerical variables") ``` -------------------------------- ### Print MFAmix Object Results Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html The print method for MFAmix objects provides a summary of the analysis, including the number of individuals and variables, and lists the available result components. ```r res.mfamix ``` -------------------------------- ### Display Principal Component Scores Source: https://github.com/chavent/pcamixdata/blob/master/docs/PCAmixcompare.html Use `head()` to display the first few rows of principal component scores. These represent the coordinates of observations projected onto the factor maps. ```R head(pcamix$scores) ``` ```R head(famd$ind$coord) ``` ```R head(dudimix$li) ``` -------------------------------- ### PCArot - Apply Varimax Rotation Source: https://context7.com/chavent/pcamixdata/llms.txt Applies varimax rotation to the results of a PCAmix analysis to improve interpretability of components. ```APIDOC ## PCArot ### Description Applies varimax rotation to the first dimensions of a PCAmix object. ### Parameters - **obj** (PCAmix object) - Required - The result of a PCAmix analysis. - **dim** (integer) - Required - Number of dimensions to rotate. - **itermax** (integer) - Optional - Maximum number of iterations for the rotation algorithm. - **graph** (boolean) - Optional - Whether to display the rotation plot. ### Response - **eig** (matrix) - Variance explained after rotation. - **ind$coord** (matrix) - Rotated individual coordinates. - **quanti$coord** (matrix) - Rotated loadings for quantitative variables. - **levels$coord** (matrix) - Rotated coordinates for qualitative variable levels. - **T** (matrix) - The rotation matrix. ``` -------------------------------- ### Plot MFA Levels Source: https://github.com/chavent/pcamixdata/blob/master/docs/supplementary.html Visualizes the levels of an MFA object using the plot function. ```R plot(mfa.sup,choice="levels") ``` -------------------------------- ### Perform PCAmix Analysis and Rotation Source: https://context7.com/chavent/pcamixdata/llms.txt Performs PCAmix analysis on quantitative and qualitative variables, then applies varimax rotation to the principal components. Use this to explore the structure of mixed-type data and improve component interpretability. ```R data(wine) pca <- PCAmix( X.quanti = wine[, 3:29], X.quali = wine[, 1:2], ndim = 4, graph = FALSE ) rot <- PCArot( obj = pca, # PCAmix object dim = 3, # Number of dimensions to rotate itermax = 100, # Maximum iterations graph = FALSE # Suppress automatic plotting ) print("Before rotation:") print(pca$eig[1:3, ]) print("After rotation:") print(rot$eig) # Get rotated individual coordinates head(rot$ind$coord) # Rotated loadings for quantitative variables print(rot$quanti$coord) # Rotated coordinates for qualitative variable levels print(rot$levels$coord) # Access rotation matrix print(rot$T) # For 2D rotation, get rotation angle in radians if (!is.null(rot$theta)) { print(paste("Rotation angle:", rot$theta, "radians")) } ``` -------------------------------- ### Split Mixed Data into Quantitative and Qualitative Source: https://context7.com/chavent/pcamixdata/llms.txt Utility function to separate a mixed data frame into quantitative and qualitative components. Integer columns are treated as quantitative; use factors for categorical variables. This is a preparatory step for PCAmix. ```r # Load mixed dataset data(decathlon) # View structure of original data str(decathlon) #> 'data.frame': 41 obs. of 13 variables: #> $ X100m : num 11 10.8 11.2 ... #> $ Long.jump : num 7.58 7.45 7.34 ... #> ... #> $ Competition: Factor w/ 2 levels "Decastar","OlympicG": 1 1 1 ... # Split into quantitative and qualitative data_split <- splitmix(decathlon) # Access quantitative variables print(head(data_split$X.quanti)) #> X100m Long.jump Shot.put High.jump X400m #> SEBRLE 11.04 7.58 14.83 2.07 49.81 #> CLAY 10.76 7.40 14.26 1.86 49.37 # Access qualitative variables print(data_split$X.quali) #> Competition #> SEBRLE Decastar #> CLAY Decastar # Use with PCAmix pca <- PCAmix( X.quanti = data_split$X.quanti, X.quali = data_split$X.quali, graph = FALSE ) ``` -------------------------------- ### Add Supplementary Variables to PCAmix/MFAmix Source: https://context7.com/chavent/pcamixdata/llms.txt Projects supplementary variables onto existing PCAmix or MFAmix components without affecting the analysis. Useful for visualizing how additional variables relate to the principal components. Ensure the initial PCA object is fitted with active variables. ```r # Perform initial PCAmix with active variables data(wine) X.quanti <- splitmix(wine)$X.quanti[, 1:5] X.quali <- splitmix(wine)$X.quali[, 1, drop = FALSE] # Supplementary variables (not used in PCA computation) X.quanti.sup <- splitmix(wine)$X.quanti[, 28:29] X.quali.sup <- splitmix(wine)$X.quali[, 2, drop = FALSE] # Fit PCAmix on active variables only pca <- PCAmix(X.quanti, X.quali, ndim = 4, graph = FALSE) # Add supplementary variables pca_sup <- supvar(pca, X.quanti.sup, X.quali.sup, rename.level = TRUE) # Access supplementary variable results print(pca_sup$quanti.sup) # Supplementary quantitative variable coordinates print(pca_sup$levels.sup) # Supplementary qualitative level coordinates print(pca_sup$sqload.sup) # Supplementary squared loadings # Visualize with supplementary variables plot(pca_sup, choice = "cor", main = "Correlation Circle with Supplementary Variables") plot(pca_sup, choice = "levels", main = "Levels with Supplementary Variables") plot(pca_sup, choice = "sqload", main = "Squared Loadings with Supplementary") ``` -------------------------------- ### Plotting levels with pcamixdata Source: https://github.com/chavent/pcamixdata/blob/master/docs/PCAmixcompare.html Generates a plot of levels for a pcamix object. The xlim parameter is used to define the range of the x-axis. ```R plot(pcamix, choice = "levels", xlim=c(-1.5,2.4)) ``` -------------------------------- ### Plotting PCAMIX Correlation Circles and Levels Source: https://github.com/chavent/pcamixdata/blob/master/docs/index.html Use this code to visualize numerical variables on a correlation circle and categorical variables by their levels. It shows plots before and after rotation, useful for interpreting principal components. ```R par(mfrow=c(2,2)) plot(res.pcamix,choice="cor", coloring.var=TRUE, leg=TRUE,axes=c(1,3), posleg="topright", main="Before rotation") plot(res.pcarot,choice="cor", coloring.var=TRUE, leg=TRUE,axes=c(1,3), posleg="topright", main="After rotation") plot(res.pcamix,choice="levels", leg=TRUE,axes=c(1,3), posleg="topright", main="Before rotation",xlim=c(-1.5,2.5)) plot(res.pcarot,choice="levels", leg=TRUE,axes=c(1,3), posleg="topright", main="After rotation",xlim=c(-1.5,2.5)) ``` -------------------------------- ### Plot MFA with Supplementary Variables Source: https://github.com/chavent/pcamixdata/blob/master/docs/supplementary.html This snippet generates a plot for MFA results including supplementary variables, visualizing variable groups. Use 'choice="levels"' for level-based plots and 'coloring.var = "groups"' for group coloring. ```R plot(mfa.sup,choice="levels",coloring.var = "groups") ``` -------------------------------- ### View Squared Loadings Source: https://github.com/chavent/pcamixdata/blob/master/docs/PCAmixcompare.html Squared loadings indicate the proportion of variance in each variable explained by the principal components. They are calculated as squared correlations for numerical variables and correlation ratios for categorical variables. ```R pcamix$sqload ``` ```R famd$var$coord ``` ```R dudimix$cr ``` -------------------------------- ### Perform FAMD Analysis and Plotting Source: https://github.com/chavent/pcamixdata/blob/master/docs/PCAmixcompare.html Executes a Factor Analysis of Mixed Data (FAMD) and generates a plot for quantitative variables. ```R plot(famd, choix = "quanti") ``` -------------------------------- ### Recode Data for PCAmix Algorithm Source: https://context7.com/chavent/pcamixdata/llms.txt Internal utility function that recodes quantitative and qualitative data matrices for PCAmix. It performs standardization for quantitative data and creates indicator matrices for qualitative data. The output includes standardized quantitative data (W), original merged data (X), and the indicator matrix (G). ```r # Manual recoding example data(wine) X.quanti <- splitmix(wine)$X.quanti[, 1:5] X.quali <- splitmix(wine)$X.quali # Recode the data rec <- recod(X.quanti, X.quali, rename.level = TRUE) # Standardized quantitative + centered indicator matrix print(dim(rec$W)) #> [1] 21 15 # n observations x (p1 quanti + m levels) # Original merged data print(dim(rec$X)) #> [1] 21 7 # n observations x p total variables # Indicator matrix for qualitative variables print(dim(rec$G)) #> [1] 21 10 # n observations x m levels ``` -------------------------------- ### Display Data Dimensions Source: https://context7.com/chavent/pcamixdata/llms.txt Prints the number of observations and variables from a PCAmix object. ```r print(paste("Observations:", rec$n)) print(paste("Total variables:", rec$p)) print(paste("Quantitative variables:", rec$p1)) ``` -------------------------------- ### MCA using dudi.mix (ade4 package) Source: https://github.com/chavent/pcamixdata/blob/master/docs/PCAmixcompare.html Performs MCA on the categorical 'services' dataset using the dudi.mix function from the ade4 package. Disables the interactive selection of axes and specifies the number of retained dimensions. ```r mca.dudimix <- dudi.mix(services, scannf = FALSE, nf = 2) ``` -------------------------------- ### Add Supplementary Variables to MFA Source: https://github.com/chavent/pcamixdata/blob/master/docs/supplementary.html This snippet adds supplementary qualitative variables to an existing MFA object. Ensure the supplementary variables are correctly formatted and grouped. ```R groups.sup <- c(1) name.groups.sup <- "Gsup1" mfa.sup <- supvar(mfa,X.quali.sup,groups.sup,name.groups.sup,rename.level=TRUE) ```