Variable Clustering from scratch using SAS and Python

Urvish Shah
Analytics Vidhya
Published in
5 min readOct 1, 2020

--

Hello Everyone,

In this article, I am trying to showcase my understanding of the Variable Clustering algorithm (the most popular technique for dimension reduction).

Disclaimer: This article is aimed to share my knowledge of Variable Clustering. This does not include any project-related data, codes, and so on. SAS/Python codes used in this article are purely based on publically available datasets and not relate to any company project. The reader of this article must have a background in Statistics, SAS, and Python

The complexity of a dataset increases rapidly with increasing dimensionality. It increases the computation time, affects the ability to explore the model relationship, model scoring, increases the redundancy in the dataset, and so on.

One of the remedial measures can be Variable Clustering. It finds a group of variables that are as correlated as possible among themselves within a cluster and as uncorrelated as possible with variables in other clusters.

OK. Now, let’s understand how it is done.

PCA — Principal Component Analysis is the heart of the algorithm. Let us understand how it works.

Suppose, we have 10 variables and their correlation among themselves are as follow:

Table 1
Table 2

Now, we will create a coefficient for the first principal component or factor. The principal component is the weighted linear combination of predictor variables. It is calculated as follow:

Table 3

Let me expand the formulas. It is taking column total and row total and then First PC = column_total / sqrt(row_total)

Table 4
Table 5
Table 6
Table 7
Table 8

Now, obtain the matrix of residual coefficients (R1) by Table 7 values to Table 2 (R) values.

Table 9

Now, on this residual matrix, we will perform the same calculations to get the second PC2.

Table 10
Table 11

The same process will follow for all the factors/PCs. As we have 10 variables so there will be 10 PCs calculated. Till now, we get the coefficients of PCs as follow:

Table 12 (Component Marix)

Table 12 represents the Factor Loadings which are the correlation between each PCs and Variables. If we simply square the values, we will get R2 as follow:

Table 13 (R2)

We can calculate different measures by using Table 13.

Eigenvalue: Variance explained by each PC for all the variables (Column total of each PC).

Communalities: Variance explained by all the PC for a single variable (Row total). It will always sum to 1 as 100% variance explained by all the PCs for a single variable.

Table 14 (Communality and Eigenvalues)

Ideally, first few PCs (say first two PC in this example)will be retained as they explained most of the variations in the data. And this reduced set of PCs will be used to create score and that score will be used in place of original 10 variables.

Ok. Now, we are clear about PC, Eigenvalues, Communality and so on. Variable Clustering uses the same algorithm but instead of using the PC score, we will pick one variable from each Cluster. All the variables starts in one cluster. A principal components is done on the variables in the cluster. If the Second Eigenvalue of PC is greater than specified threshold, then the cluster is split.

The question is which variable to pick from each cluster? One should pick the variable which is having highest correlation with its own cluster and low with other clusters.

Hence, the variable having lowest 1 — R2 ratio can be good representative of cluster and that variable would be used and other variables from that cluster will be discarded. This is how we can achieve the dimension reduction.

Let’s implement in SAS and Python. Total number of Clusters are not matching between SAS and Python. In SAS, there are total 35 clusters and in Python, there are 40. However, variable allocations in most of the clusters and their 1 — R2 Ratio are matching.

The input dataset contains 63 variables including dummy variables, missing indicator variables and so on. In last outpt, it gives us the clusters containing different variables and we will select one variable based on 1-R2_Ratio.

As there are 40 clusters, there will be 40 variables instead of 63 variables.

I used maxeigval = 0.7. It means that clusters will split if the second eigenvalue is greater 0.7. larger value of this parameter gives fewer clusters and less of the variations explained. Smaller value gives more clusters and more variation explained. The common choice is 1 as it represents average size of eigenvalues.

SAS Code

SAS Output for the first 10 clusters:

Thanks,

Urvish Shah

--

--

Urvish Shah
Analytics Vidhya

Statistical Modeling, Machine Learning, Deep Learning, SAS, Python, Big Data, Teaching and Training.