{"items":["5f69d96928d08f0017ed0499","5f69d96928d08f0017ed049a","5caa6b98631a3e00159692df"],"styles":{"galleryType":"Columns","groupSize":1,"showArrows":true,"cubeImages":true,"cubeType":"max","cubeRatio":1.7777777777777777,"isVertical":true,"gallerySize":30,"collageAmount":0,"collageDensity":0,"groupTypes":"1","oneRow":false,"imageMargin":30,"galleryMargin":0,"scatter":0,"chooseBestGroup":true,"smartCrop":false,"hasThumbnails":false,"enableScroll":true,"isGrid":true,"isSlider":false,"isColumns":false,"isSlideshow":false,"cropOnlyFill":false,"fixedColumns":0,"enableInfiniteScroll":true,"isRTL":false,"minItemSize":50,"rotatingGroupTypes":"","rotatingCubeRatio":"","gallerySliderImageRatio":1.7777777777777777,"numberOfImagesPerRow":3,"numberOfImagesPerCol":1,"groupsPerStrip":0,"borderRadius":0,"boxShadow":0,"gridStyle":0,"mobilePanorama":false,"placeGroupsLtr":false,"viewMode":"preview","thumbnailSpacings":4,"galleryThumbnailsAlignment":"bottom","isMasonry":false,"isAutoSlideshow":false,"slideshowLoop":false,"autoSlideshowInterval":4,"bottomInfoHeight":0,"titlePlacement":["SHOW_ON_THE_RIGHT","SHOW_BELOW"],"galleryTextAlign":"center","scrollSnap":false,"itemClick":"nothing","fullscreen":true,"videoPlay":"hover","scrollAnimation":"NO_EFFECT","slideAnimation":"SCROLL","scrollDirection":0,"overlayAnimation":"FADE_IN","arrowsPosition":0,"arrowsSize":23,"watermarkOpacity":40,"watermarkSize":40,"useWatermark":true,"watermarkDock":{"top":"auto","left":"auto","right":0,"bottom":0,"transform":"translate3d(0,0,0)"},"loadMoreAmount":"all","defaultShowInfoExpand":1,"allowLinkExpand":true,"expandInfoPosition":0,"allowFullscreenExpand":true,"fullscreenLoop":false,"galleryAlignExpand":"left","addToCartBorderWidth":1,"addToCartButtonText":"","slideshowInfoSize":200,"playButtonForAutoSlideShow":false,"allowSlideshowCounter":false,"hoveringBehaviour":"NEVER_SHOW","thumbnailSize":120,"magicLayoutSeed":1,"imageHoverAnimation":"NO_EFFECT","imagePlacementAnimation":"NO_EFFECT","calculateTextBoxWidthMode":"PERCENT","textBoxHeight":60,"textBoxWidth":200,"textBoxWidthPercent":75,"textImageSpace":10,"textBoxBorderRadius":0,"textBoxBorderWidth":0,"loadMoreButtonText":"","loadMoreButtonBorderWidth":1,"loadMoreButtonBorderRadius":0,"imageInfoType":"ATTACHED_BACKGROUND","itemBorderWidth":0,"itemBorderRadius":0,"itemEnableShadow":false,"itemShadowBlur":20,"itemShadowDirection":135,"itemShadowSize":10,"imageLoadingMode":"BLUR","expandAnimation":"NO_EFFECT","imageQuality":90,"usmToggle":false,"usm_a":0,"usm_r":0,"usm_t":0,"videoSound":false,"videoSpeed":"1","videoLoop":true,"gallerySizeType":"px","gallerySizePx":1000,"allowTitle":true,"allowContextMenu":true,"textsHorizontalPadding":-30,"itemBorderColor":{"themeName":"color_12","value":"rgba(216,216,216,0)"},"showVideoPlayButton":true,"galleryLayout":2,"calculateTextBoxHeightMode":"MANUAL","targetItemSize":1000,"selectedLayout":"2|bottom|1|max|true|0|true","layoutsVersion":2,"selectedLayoutV2":2,"isSlideshowFont":true,"externalInfoHeight":60,"externalInfoWidth":0.75},"container":{"width":300,"galleryWidth":330,"galleryHeight":0,"scrollBase":0,"height":null}}

# Clustering of Predictor Variables in a Statistical Model

*“Ok, so we have 300 variables available for our model, we don’t know what most of them are, we don’t know if they are stable or suitable for use in the model. We will need our IT department to investigate.*

*So what we’d like to do is have say 8-12 variables in our model but we’d like to be able to swap in the next best variable if the one we find is unsuitable for implementation”*

This may seem like a contrived example, but it was actually a situation I faced many years ago.

My mind immediately went to cluster analysis, but a cluster analysis involving predictor variables.

At the time I was using SAS for my data analysis, thankfully in SAS there was a procedure called “PROC VARCLUS”

Check out https://www.listendata.com/2015/03/proc-varclus-explained.html

How the procedure works is like this:

**1)** All variables start in one cluster. Then, a principal components analysis is done on the variables in the cluster to determine whether the cluster should be split into two subsets of variables.

**2) **If the second eigenvalue for the cluster is greater than the specified cutoff, then the initial cluster is split into two clusters. If the second eigenvalue is large, it means that at least two principal components account for a large amount of variation among the inputs.

**3)** To determine which inputs are included in each cluster, the principal component scores are rotated obliquely to maximize the correlation within a cluster and minimize the correlation between clusters.

**4)** This process ends when the second eigenvalues of all current clusters fall below the cutoff.

What this procedure gave me were groups of variables. Within each group, variables were similar to each other but different to other groups. The most representative of the group, or the variables that were most uniquely members of their group appeared first, and the weaker variables in the group appeared last.

So this allowed me to create I think 10 groups and include a variable from each group in the model, now where a variable from a group could not be implemented it’s next best candidate variable was included instead.

I haven’t used SAS for many years, these days I use R and Python.

There is something equivalent to PROC VARCLUS in R, in the Hmisc package there is a procedure called “varclus”.

Let’s give it a whirl! Let’s use the German credit card dataset for practice:

https://archive.ics.uci.edu/ml/datasets/statlog+(german+credit+data)

I see anywhere from 5 to 10 clusters there, which obviously need to be tested. But there’s a pretty simple method for clustering predictor variables.

install.packages("Hmisc")

library(Hmisc)

numeric_data = read.table("https://archive.ics.uci.edu/ml/machine-learning-databases/statlog/german/german.data-numeric")

# as we have many categoricals converted to dummies, let's use spearman correlation

pairs(numeric_data)

predictor_clusters = varclus(as.matrix(numeric_data))

summary(predictor_clusters)

# plot the hierarchical clusters as a tree

plot(predictor_clusters)