### Install and Load ClustOfVar Source: https://context7.com/chavent/clustofvar/llms.txt Install the package from GitHub and load it into the R environment. ```r # Install from GitHub devtools::install_github("chavent/ClustOfVar") # Load the package library(ClustOfVar) ``` -------------------------------- ### Install ClustOfVar from GitHub Source: https://github.com/chavent/clustofvar/blob/master/README.md Use devtools to install the development version of the package from the GitHub repository. ```r devtools::install_github("chavent/ClustOfVar") # This needs the devtools package to be installed : # install.packages("devtools") ``` -------------------------------- ### Evaluate Partition Stability Source: https://context7.com/chavent/clustofvar/llms.txt Use bootstrap resampling to assess the stability of hierarchical clustering partitions. ```r library(ClustOfVar) data(decathlon) tree <- hclustvar(X.quanti = decathlon[, 1:10]) stab <- stability(tree, B = 60, graph = TRUE) stab$matCR # matrix of corrected Rand indices (B x (p-2)) stab$meanCR # vector of mean corrected Rand indices plot(stab, nmax = 7) boxplot(stab$matCR[, 1:7]) optimal_k <- which.max(stab$meanCR) + 1 cat("Suggested number of clusters:", optimal_k) ``` -------------------------------- ### Plot Variable Loadings by Cluster Source: https://context7.com/chavent/clustofvar/llms.txt Visualize variable contributions to clusters using dot charts. ```r library(ClustOfVar) data(wine) X.quanti <- PCAmixdata::splitmix(wine)$X.quanti X.quali <- PCAmixdata::splitmix(wine)$X.quali tree <- hclustvar(X.quanti, X.quali) tree_cut <- cutreevar(tree, k = 6) res_plot <- plot(tree_cut) res_plot$coord.quanti # coordinates of quantitative variables res_plot$coord.levels # coordinates of categorical variable levels ``` -------------------------------- ### Perform K-Means Clustering of Variables Source: https://context7.com/chavent/clustofvar/llms.txt Use kmeansvar to perform iterative k-means clustering, supporting random initialization or initialization from a hierarchical partition. ```r library(ClustOfVar) data(decathlon) # K-means with random initialization, 10 random starts part_random <- kmeansvar( X.quanti = decathlon[, 1:10], init = 5, # number of clusters nstart = 10, # number of random initializations iter.max = 150 # maximum iterations ) summary(part_random) # Output: # Call: # kmeansvar(X.quanti = decathlon[, 1:10], init = 5, nstart = 10) # # number of iterations: 3 # # Data: # number of observations: 41 # number of variables: 10 # number of clusters: 5 # # Cluster 1: # squared loading correlation # X100m 0.92 -0.96 # Long.jump 0.88 0.94 # ... # K-means initialized from hierarchical clustering tree <- hclustvar(X.quanti = decathlon[, 1:10]) part_init <- cutreevar(tree, 5)$cluster part_hier <- kmeansvar( X.quanti = decathlon[, 1:10], init = part_init, # use partition as initial clusters matsim = TRUE # calculate similarity matrices ) summary(part_hier) part_hier$sim # view similarity matrices # Access cluster assignments part_random$cluster # X100m Long.jump Shot.put High.jump X400m # 1 1 2 3 4 # ... # Access cluster scores (synthetic variables) head(part_random$scores) # cluster1 cluster2 cluster3 cluster4 cluster5 # SEBRLE 0.4872419 -0.2551023 -0.5813429 -0.8629382 0.5019456 # ... ``` -------------------------------- ### Plot Dendrogram and Aggregation Levels Source: https://context7.com/chavent/clustofvar/llms.txt Visualize hierarchical clustering results as a dendrogram or aggregation index plot. ```r library(ClustOfVar) data(wine) X.quanti <- PCAmixdata::splitmix(wine)$X.quanti X.quali <- PCAmixdata::splitmix(wine)$X.quali tree <- hclustvar(X.quanti, X.quali) plot(tree) plot(tree, type = "index") ``` -------------------------------- ### Access ClustOfVar Partition Results Source: https://context7.com/chavent/clustofvar/llms.txt Access cluster assignments, sizes, and homogeneity metrics from a clustering object. ```r summary(part) part$cluster # cluster assignments part$size # number of variables per cluster part$wss # within-cluster sum of squares part$E # percentage of homogeneity explained part_sim <- cutreevar(tree, k = 4, matsim = TRUE) part_sim$sim$cluster1 ``` -------------------------------- ### Cut Hierarchical Tree into Clusters Source: https://context7.com/chavent/clustofvar/llms.txt Use cutreevar to partition a hierarchical tree into a specific number of clusters and retrieve cluster details. ```r library(ClustOfVar) data(decathlon) # Build hierarchical tree tree <- hclustvar(decathlon[, 1:10]) plot(tree) # Determine optimal number of clusters using stability stab <- stability(tree, B = 40) # Cut tree into 4 clusters part <- cutreevar(tree, k = 4) print(part) # Output: # Call: # cutreevar(obj = tree, k = 4) # # name description # "$var" "list of variables in each cluster" # "$sim" "similarity matrix in each cluster" # "$cluster""cluster memberships" # "$wss" "within-cluster sum of squares" # "$E" "gain in cohesion (in %)" # "$size" "size of each cluster" # "$scores" "synthetic score of each cluster" ``` -------------------------------- ### plot.clustvar - Plot Variable Loadings by Cluster Source: https://context7.com/chavent/clustofvar/llms.txt Creates dot charts showing the loadings of variables within each cluster. For quantitative variables, loadings are correlations with the synthetic variable; for categorical variables, loadings are mean values of the synthetic variable for each level. ```APIDOC ## plot.clustvar - Plot Variable Loadings by Cluster ### Description Creates dot charts showing the loadings of variables within each cluster. For quantitative variables, loadings are correlations with the synthetic variable; for categorical variables, loadings are mean values of the synthetic variable for each level. ### Method `plot(x, ...)` ### Parameters #### Path Parameters None #### Query Parameters - **x** (clustvar object) - The result of a clustering function (e.g., `cutreevar`). ### Request Example ```r library(ClustOfVar) data(wine) # Prepare and cluster mixed data X.quanti <- PCAmixdata::splitmix(wine)$X.quanti X.quali <- PCAmixdata::splitmix(wine)$X.quali tree <- hclustvar(X.quanti, X.quali) tree_cut <- cutreevar(tree, k = 6) # Plot loadings for each cluster res_plot <- plot(tree_cut) ``` ### Response #### Success Response (200) - Generates a plot showing variable loadings by cluster. - **coord.quanti** (matrix) - Coordinates of quantitative variables. - **coord.levels** (matrix) - Coordinates of categorical variable levels. #### Response Example ```r # Plots are displayed directly. # Access coordinate data # res_plot$coord.quanti # res_plot$coord.levels ``` ``` -------------------------------- ### stability - Evaluate Partition Stability Source: https://context7.com/chavent/clustofvar/llms.txt Evaluates the stability of partitions obtained from hierarchical clustering using a bootstrap approach. The function compares partitions from bootstrap samples with the original partition using the adjusted Rand index, helping to determine an appropriate number of clusters. ```APIDOC ## stability - Evaluate Partition Stability ### Description Evaluates the stability of partitions obtained from hierarchical clustering using a bootstrap approach. The function compares partitions from bootstrap samples with the original partition using the adjusted Rand index, helping to determine an appropriate number of clusters. ### Method `stability(tree, B, graph, adj, method, ...)` ### Parameters #### Path Parameters None #### Query Parameters - **tree** (hclustvar object) - The hierarchical clustering result. - **B** (numeric) - The number of bootstrap samples. - **graph** (logical) - If TRUE, a plot is generated. - **adj** (logical) - If TRUE, uses adjusted Rand index. - **method** (character) - Method for calculating similarity (e.g., "average", "complete"). ### Request Example ```r library(ClustOfVar) data(decathlon) # Build hierarchical tree tree <- hclustvar(X.quanti = decathlon[, 1:10]) # Evaluate stability with 60 bootstrap samples stab <- stability(tree, B = 60, graph = TRUE) ``` ### Response #### Success Response (200) - **matCR** (matrix) - Matrix of corrected Rand indices (B x (p-2)). - **meanCR** (vector) - Vector of mean corrected Rand indices. #### Response Example ```r # Access stability results stab$matCR stab$meanCR # Custom plot with limited clusters plot(stab, nmax = 7) # Boxplot of Rand indices for first 7 partitions boxplot(stab$matCR[, 1:7]) # Use stability to choose optimal k optimal_k <- which.max(stab$meanCR) + 1 cat("Suggested number of clusters:", optimal_k) ``` ``` -------------------------------- ### plot.hclustvar - Plot Dendrogram and Aggregation Levels Source: https://context7.com/chavent/clustofvar/llms.txt Visualizes hierarchical clustering results as a dendrogram or as an aggregation levels plot showing the criterion values at each merge step. ```APIDOC ## plot.hclustvar - Plot Dendrogram and Aggregation Levels ### Description Visualizes hierarchical clustering results as a dendrogram or as an aggregation levels plot showing the criterion values at each merge step. ### Method `plot(x, type, ...)` ### Parameters #### Path Parameters None #### Query Parameters - **x** (hclustvar object) - The hierarchical clustering result. - **type** (character) - Type of plot to draw. "dendrogram" for standard dendrogram, "index" for aggregation levels. ### Request Example ```r library(ClustOfVar) data(wine) # Prepare mixed data X.quanti <- PCAmixdata::splitmix(wine)$X.quanti X.quali <- PCAmixdata::splitmix(wine)$X.quali tree <- hclustvar(X.quanti, X.quali) # Standard dendrogram plot(tree) # Aggregation levels plot plot(tree, type = "index") ``` ### Response #### Success Response (200) - Generates a plot. #### Response Example ```r # Plots are displayed directly. ``` ``` -------------------------------- ### Perform Hierarchical Clustering of Variables Source: https://context7.com/chavent/clustofvar/llms.txt Use hclustvar to perform hierarchical clustering on quantitative, qualitative, or mixed variable sets. ```r library(ClustOfVar) # Clustering quantitative variables only data(decathlon) tree_quant <- hclustvar(X.quanti = decathlon[, 1:10]) plot(tree_quant) # Output structure: # tree_quant$height - aggregation criterion values # tree_quant$merge - merge matrix showing clustering steps # tree_quant$clusmat - cluster membership matrix for all levels # Clustering qualitative variables with missing values data(vnf) tree_qual <- hclustvar(X.quali = vnf) plot(tree_qual) # Clustering mixed variables (quantitative + qualitative) data(wine) X.quanti <- PCAmixdata::splitmix(wine)$X.quanti X.quali <- PCAmixdata::splitmix(wine)$X.quali tree_mixed <- hclustvar(X.quanti, X.quali) plot(tree_mixed) # View aggregation levels to help choose number of clusters plot(tree_mixed, type = "index") ``` -------------------------------- ### Calculate Similarity Between Variables Source: https://context7.com/chavent/clustofvar/llms.txt Compute squared cosine similarity measures for quantitative, qualitative, or mixed variable pairs. ```r library(ClustOfVar) data(wine) X.quanti <- PCAmixdata::splitmix(wine)$X.quanti X.quali <- PCAmixdata::splitmix(wine)$X.quali sim_quant <- mixedVarSim(X.quanti[, 1], X.quanti[, 2]) cat("Quantitative-Quantitative similarity:", sim_quant, "\n") sim_qual <- mixedVarSim(X.quali[, 1], X.quali[, 2]) cat("Qualitative-Qualitative similarity:", sim_qual, "\n") sim_mixed <- mixedVarSim(X.quanti[, 1], X.quali[, 1]) cat("Mixed similarity:", sim_mixed, "\n") ``` -------------------------------- ### predict.clustvar - Predict Scores for New Data Source: https://context7.com/chavent/clustofvar/llms.txt Computes scores for new observations on the synthetic variables (cluster centers) of an existing partition. Uses the linear combination coefficients from the original clustering to project new data onto cluster-specific synthetic variables. ```APIDOC ## predict.clustvar - Predict Scores for New Data ### Description Computes scores for new observations on the synthetic variables (cluster centers) of an existing partition. Uses the linear combination coefficients from the original clustering to project new data onto cluster-specific synthetic variables. ### Method `predict(object, X.quanti, X.quali, ...)` ### Parameters #### Path Parameters None #### Query Parameters - **object** (clustvar object) - The result of a clustering function (e.g., `kmeansvar`). - **X.quanti** (data.frame) - Quantitative variables in the new data. - **X.quali** (data.frame) - Qualitative variables in the new data. ### Request Example ```r library(ClustOfVar) data(wine) # Create learning and test samples n <- nrow(wine) sub <- 10:20 X.quanti.train <- wine[sub, 3:29] X.quali.train <- wine[sub, 1:2] # Cluster variables using training data part <- kmeansvar(X.quanti.train, X.quali.train, init = 5) # Prepare test data X.quanti.test <- wine[-sub, 3:29] X.quali.test <- wine[-sub, 1:2] # Predict scores for new observations new_scores <- predict(part, X.quanti.test, X.quali.test) head(new_scores) ``` ### Response #### Success Response (200) - **scores** (matrix) - Scores for new observations on the synthetic variables. #### Response Example ```r # head(new_scores) # cluster1 cluster2 cluster3 cluster4 cluster5 # 2H2001 0.543210 -0.234567 0.876543 -0.432109 0.234567 # ... ``` ``` -------------------------------- ### Predict Scores for New Data Source: https://context7.com/chavent/clustofvar/llms.txt Project new observations onto existing cluster synthetic variables using learned coefficients. ```r library(ClustOfVar) data(wine) n <- nrow(wine) sub <- 10:20 X.quanti.train <- wine[sub, 3:29] X.quali.train <- wine[sub, 1:2] part <- kmeansvar(X.quanti.train, X.quali.train, init = 5) X.quanti.test <- wine[-sub, 3:29] X.quali.test <- wine[-sub, 1:2] new_scores <- predict(part, X.quanti.test, X.quali.test) head(new_scores) ``` -------------------------------- ### mixedVarSim - Calculate Similarity Between Variables Source: https://context7.com/chavent/clustofvar/llms.txt Computes the similarity between two variables of any type (quantitative-quantitative, qualitative-qualitative, or mixed). Similarity is defined as a squared cosine measure appropriate for each variable type combination. ```APIDOC ## mixedVarSim - Calculate Similarity Between Variables ### Description Computes the similarity between two variables of any type (quantitative-quantitative, qualitative-qualitative, or mixed). Similarity is defined as a squared cosine measure appropriate for each variable type combination. ### Method `mixedVarSim(x, y, ...)` ### Parameters #### Path Parameters None #### Query Parameters - **x** (vector) - The first variable. - **y** (vector) - The second variable. ### Request Example ```r library(ClustOfVar) data(wine) # Split mixed data X.quanti <- PCAmixdata::splitmix(wine)$X.quanti X.quali <- PCAmixdata::splitmix(wine)$X.quali # Similarity between two quantitative variables sim_quant <- mixedVarSim(X.quanti[, 1], X.quanti[, 2]) cat("Quantitative-Quantitative similarity:", sim_quant, "\n") # Similarity between two qualitative variables sim_qual <- mixedVarSim(X.quali[, 1], X.quali[, 2]) cat("Qualitative-Qualitative similarity:", sim_qual, "\n") # Similarity between quantitative and qualitative variable sim_mixed <- mixedVarSim(X.quanti[, 1], X.quali[, 1]) cat("Mixed similarity:", sim_mixed, "\n") ``` ### Response #### Success Response (200) - **similarity** (numeric) - The calculated similarity value. #### Response Example ```r # Output is printed to console ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.