References:
VarSelLCM permits a full model selection (detection of the relevant features for clustering and selection of the number of clusters) in model-based clustering, according to classical information criteria (BIC, MICL or AIC).
Data to analyzed can be composed of continuous, integer and/or categorical features. Moreover, missing values are managed, without any pre-processing, by the model used to cluster with the assumption that values are missing completely at random. Thus, VarSelLCM can also be used for data imputation via mixture models.
An R-Shiny application is implemented to easily interpret the clustering results
This section performs the whole analysis of the Heart data set. Warning the univariate margin distribution are defined by class of the features: numeric columns imply Gaussian distributions, integer columns imply Poisson distribution while factor (or ordered) columns imply multinomial distribution
Attaching package: 'VarSelLCM'
The following object is masked from 'package:stats':
predict
# Data loading:
# x contains the observed variables
# z the known status (i.e. 1: absence and 2: presence of heart disease)
data(heart)
ztrue <- heart[,"Class"]
x <- heart[,-13]
# Add a missing value artificially (just to show that it works!)
x[1,1] <- NAClustering is performed with variable selection. Model selection is done with BIC because the number of observations is large (compared to the number of features). The number of components is between 1 and 3. Do not hesitate to use parallelization (here only two cores are used).
# Cluster analysis without variable selection
res_without <- VarSelCluster(x, gvals = 1:3, vbleSelec = FALSE, crit.varsel = "BIC")
# Cluster analysis with variable selection (with parallelisation)
res_with <- VarSelCluster(x, gvals = 1:3, nbcores = 2, crit.varsel = "BIC")Comparison of the BIC for both models: variable selection permits to improve the BIC
[1] -6516.216
[1] -6509.506
Comparison of the partition accuracy. ARI is computed between the true partition (ztrue) and its estimators. ARI is an index between 0 (partitions are independent) and 1 (partitions are equals). Variable selection permits to improve the ARI. Note that ARI cannot be used for model selection in clustering, because there is no true partition.
[1] 0.2218655
[1] 0.2661321
To obtained the partition and the probabilities of classification
[1] 2 2 1 2 2 2 2 2 2 2 2 2 1 2 1 2 2 2 2 1 2 1 1 1 1 1 2 1 2 2 2 2 1 2 2 2 2
[38] 2 1 1 1 1 1 1 2 1 2 1 2 2 2 1 1 1 1 1 2 2 2 2 1 2 1 1 2 2 1 1 1 1 2 1 1 2
[75] 2 2 2 1 1 1 2 2 2 1 2 1 1 2 1 2 1 1 2 2 1 2 2 2 2 1 1 2 1 2 2 2 2 2 2 1 2
[112] 1 1 1 1 1 1 2 2 2 2 2 2 1 1 1 2 1 1 2 2 2 1 2 1 1 1 2 1 2 2 1 2 2 1 2 1 2
[149] 1 1 1 1 1 2 1 1 2 1 2 2 2 2 1 2 1 2 1 1 2 2 2 2 2 2 1 2 2 1 2 1 1 2 1 2 1
[186] 1 2 2 1 2 1 2 1 1 1 1 2 1 2 2 2 2 2 2 2 1 1 2 2 1 2 1 1 2 1 1 1 2 2 1 2 2
[223] 1 2 1 2 2 2 1 2 2 2 1 2 2 2 1 2 1 1 2 1 2 2 1 2 2 1 1 2 2 1 1 2 1 1 2 2 1
[260] 1 1 2 1 1 1 1 1 1 2 2
class-1 class-2
[1,] 8.270815e-06 0.9999917
[2,] 3.667447e-01 0.6332553
[3,] 8.245960e-01 0.1754040
[4,] 4.451354e-08 1.0000000
[5,] 3.888408e-03 0.9961116
[6,] 4.526259e-02 0.9547374
To get a summary of the selected model.
Model:
Number of components: 2
Model selection has been performed according to the BIC criterion
Variable selection has been performed, 8 ( 66.67 % ) of the variables are relevant for clustering
Discriminative power of the variables (here, the most discriminative variable is MaxHeartRate). The greater this index, the more the variable distinguishes the clusters.
Distribution of the most discriminative variable per clusters
Empirical and theoretical distributions of the most discriminative variable (to check that the distribution is well-fitted)
# Empirical and theoretical distributions (to check that the distribution is well-fitted)
plot(res_with, y="MaxHeartRate", type="cdf")Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
ℹ The deprecated feature was likely used in the VarSelLCM package.
Please report the issue to the authors.
This warning is displayed once per session.
Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
generated.
Distribution of a categorical variable per clusters
To have details about the selected model
Data set:
Number of individuals: 270
Number of continuous variables: 3
Number of count variables: 1
Percentile of missing values for the integer variables: 0.37
Number of categorical variables: 8
Model:
Number of components: 2
Model selection has been performed according to the BIC criterion
Variable selection has been performed, 8 ( 66.67 % ) of the variables are relevant for clustering
Information Criteria:
loglike: -6403.136
AIC: -6441.136
BIC: -6509.506
ICL: -6638.116
To print the parameters
An object of class "VSLCMparam"
Slot "pi":
class-1 class-2
0.4779414 0.5220586
Slot "paramContinuous":
An object of class "VSLCMparamContinuous"
Slot "pi":
numeric(0)
Slot "mu":
class-1 class-2
RestBloodPressure 131.3444 131.3444
SerumCholestoral 249.6593 249.6593
MaxHeartRate 165.2579 135.4143
Slot "sd":
class-1 class-2
RestBloodPressure 17.82850 17.82850
SerumCholestoral 51.59043 51.59043
MaxHeartRate 13.14888 20.98067
Slot "paramInteger":
An object of class "VSLCMparamInteger"
Slot "pi":
numeric(0)
Slot "lambda":
class-1 class-2
Age 50.32103 58.11379
Slot "paramCategorical":
An object of class "VSLCMparamCategorical"
Slot "pi":
numeric(0)
Slot "alpha":
$Sex
0 1
class-1 0.4166157 0.5833843
class-2 0.2358055 0.7641945
$ChestPainType
1 2 3 4
class-1 0.05754103 0.28951585 0.4222930 0.2306501
class-2 0.08920998 0.03291573 0.1738526 0.7040217
$FastingBloodSugar
0 1
class-1 0.8518519 0.1481481
class-2 0.8518519 0.1481481
$ResElectrocardiographic
0 1 2
class-1 0.4851852 0.007407407 0.5074074
class-2 0.4851852 0.007407407 0.5074074
$ExerciseInduced
0 1
class-1 0.9127861 0.08721392
class-2 0.4484403 0.55155974
$Slope
1 2 3
class-1 0.7598751 0.1934119 0.04671297
class-2 0.2266138 0.6884521 0.08493415
$MajorVessels
0 1 2 3
class-1 0.7915841 0.1402742 0.05988695 0.008254773
class-2 0.4104171 0.2830563 0.17928979 0.127236821
$Thal
3 6 7
class-1 0.8302076 3.583226e-09 0.1697924
class-2 0.3183021 9.932190e-02 0.5823760
Probabilities of classification for new observations
class-1 class-2
[1,] 8.644360e-06 0.9999914
[2,] 3.770629e-01 0.6229371
[3,] 8.308939e-01 0.1691061
The model can be used for imputation (of the clustered data or of a new observation)
# Imputation by posterior mean for the first observation
not.imputed <- x[1,]
imputed <- VarSelImputation(res_with, x[1,], method = "sampling")
rbind(not.imputed, imputed) Age Sex ChestPainType RestBloodPressure SerumCholestoral FastingBloodSugar
1 NA 1 4 130 322 0
2 53 1 4 130 322 0
ResElectrocardiographic MaxHeartRate ExerciseInduced Slope MajorVessels Thal
1 2 109 0 2 3 3
2 2 109 0 2 3 3