# Exploring model-based clustering with condvis2

#### 2019-06-28

mclust is a R package that offers - model-based clustering: Mclust - classification ( discriminant analysis): MclustDA - density estimation densityMclust all based on mixtures of Normals.

condvis2 offers an interface to all of these.

## Classification: MclustDA

library(condvis2)
#> Registered S3 methods overwritten by 'ggplot2':
#>   method         from
#>   [.quosures     rlang
#>   c.quosures     rlang
#>   print.quosures rlang
#> Registered S3 method overwritten by 'seriation':
#>   method         from
#>   reorder.hclust gclus
library(mclust)
#> Package 'mclust' version 5.4.3
#> Type 'citation("mclust")' for citing this R package in publications.
data(banknote)
bankDA <- MclustDA(banknote[,-1], banknote[,1],verbose=F)

This fits an XXX=ellipsoidal multivariate normal for the genuine notes and EVE2 = ellipsoidal, equal volume and equal shape with two components for counterfeits.

Condvis uses a generic CVpredict to provide a uniform interface to predict methods. We will use it to find the number of misclassifications:

table(banknote\$Status, CVpredict(bankDA, banknote))
#>
#>               counterfeit genuine
#>   counterfeit         100       0
#>   genuine               0     100
svars <- c("Top", "Diagonal")
cvars <- setdiff(names(banknote)[-1], svars)
condvis(data = banknote, model = bankDA,
response="Status", sectionvars=svars,conditionvars=cvars,
pointColor="Status", showsim=TRUE
)

The showsim=TRUE setting means that the condition plots will mark points considered sufficiently near the section with a dark outline. This is the default setting only when $$n <= 150$$. In the condition plots, the green points are the genuine ones, pink are counterfeit.

Here is a views of the result: There is one point that appears to be mis-classified (though it is not). However, as it’s size is small it is not very close to the selected section.

Selecting Show probs: we see there is some uncertainty about the classification on the classification boundaries.

A different, simpler fit is got using

bankDAe <- MclustDA(banknote[,-1], banknote[,1], modelType="EDDA",verbose=F)

This uses an EVE1 for each class, and there is one mis-classification, at case 70.

We can use condvis to compare the two fits:

condvis(data = banknote, model = list(bankDA=bankDA, bankDAe=bankDAe),
response="Status", sectionvars=svars,conditionvars=cvars,
pointColor="Status", showsim=T
)

If you go to Tour, Diff fits, you can move through views where the conditioning points are those where the fits disagree. In this case there is only one such point. Here Show probs is selected so there is some uncertainty visible at the classification boundaries.

The screenshot shows that in this view. The mis-classified point is a genuine note (green) which is just inside the region classified as counterfeit by EDDA. Here also the Similarity threshold is made small so only the point on the section is visible in the section plot. From the condition plots it is evident this note has a low Diagonal size, like the counterfeits.

## Density estimation

First calculate a density estimate of two variables.

data(banknote)
dens2 <- densityMclust(banknote[,c("Diagonal","Left")],verbose=F)
summary(dens2)
#> -------------------------------------------------------
#> Density estimation via Gaussian finite mixture modeling
#> -------------------------------------------------------
#>
#> Mclust EEI (diagonal, equal volume and shape) model with 4 components:
#>
#>  log-likelihood   n df       BIC       ICL
#>       -289.6722 200 13 -648.2225 -664.0955
#>
#> Clustering table:
#>  1  2  3  4
#> 16 84 87 13

We can visualise the density as a surface or contour plot. Alternatively, we can use condvis to show the conditional density, fixing one of the variables.

condvis(data = banknote, model = dens2, response=NULL,
sectionvars="Diagonal",conditionvars="Left",
density=T, showdata=T)

The density of Diagonal varies with the Left value. Click on left to check this. Estimating the density of three variables:

dens3 <- densityMclust(banknote[,c("Right", "Bottom", "Diagonal")],verbose=F)
summary(dens3)
#> -------------------------------------------------------
#> Density estimation via Gaussian finite mixture modeling
#> -------------------------------------------------------
#>
#> Mclust EEI (diagonal, equal volume and shape) model with 4 components:
#>
#>  log-likelihood   n df       BIC       ICL
#>        -544.611 200 18 -1184.592 -1197.047
#>
#> Clustering table:
#>  1  2  3  4
#> 14 12 97 77

By way of comparison, here is the kernel density estimate to compare with mclust:

library(ks)
kdens3 <- kde(banknote[,c("Right", "Bottom", "Diagonal")])
condvis(data = banknote, model = list(mclust=dens3, kde=kdens3), response=NULL,
sectionvars=c("Bottom", "Diagonal"),conditionvars="Right",
density=T, showdata=T)

As you vary the level of Right, both densities look quite similar. Click on “Show 3d surface” to get a wireframe plot.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7425415,"math_prob":0.9061044,"size":4526,"snap":"2019-35-2019-39","text_gpt3_token_len":1164,"char_repetition_ratio":0.1501548,"word_repetition_ratio":0.115625,"special_character_ratio":0.2934158,"punctuation_ratio":0.13802083,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9781725,"pos_list":[0,1,2,3,4,5,6,7,8],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-09-19T13:22:07Z\",\"WARC-Record-ID\":\"<urn:uuid:9686a7af-8bf2-4675-9c98-700fcbdb094e>\",\"Content-Length\":\"883518\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:940dd097-24ca-4191-a191-28cf869e54d5>\",\"WARC-Concurrent-To\":\"<urn:uuid:2a4581b7-17f6-4084-9991-7f7329e137f9>\",\"WARC-IP-Address\":\"99.84.216.60\",\"WARC-Target-URI\":\"https://cran.rstudio.com/web/packages/condvis2/vignettes/mclust.html\",\"WARC-Payload-Digest\":\"sha1:42B73OZCCMBDRC4KJDAO2FW53M2NWTPN\",\"WARC-Block-Digest\":\"sha1:DZ532EIIZG7DP242UTW3JIWNPXV6VP4C\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-39/CC-MAIN-2019-39_segments_1568514573519.72_warc_CC-MAIN-20190919122032-20190919144032-00281.warc.gz\"}"}