<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/simulating-nxn-dimensional-gaussian-clusters-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://intobioinformatics.wordpress.com/2018/08/25/simulating-nxn-dimensional-gaussian-clusters-in-r/"> R – intobioinformatics</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Gaussian clusters are found in a range of fields and simulating them is important as often we will want to test a given class discovery tools performance under conditions where the ground truth is known (e.g. K=6). However, a flexible Gaussian cluster simulator for simulating Gaussian clusters with defined variance, spacing, and size does not exist. This is why we made ‘clusterlab’, a new CRAN package (<a rel="nofollow" href="https://cran.r-project.org/web/packages/clusterlab/index.html" target="_blank">https://cran.r-project.org/web/packages/clusterlab/index.html</a>). It was initially based on the Scikit-Learn make.blobs function, but now is much more sophisticated.</p>
<p>Clusterlab works in 2D space initially, because it is easy to work in mathematically. First, single 2D coordinates are placed on the perimeter of a circle by the algorithm, then they may be ‘pushed’ through vector multiplication with scalars outwards. Multiple rings of coordinates may be generated, this allows the user to create complex patterns through rotations of the individual rings. So by this point, we have the cluster center coordinates created in 2D space. Next, clusters are formed by adding Gaussian noise to the cluster centers and concatenating the new sample coordinates to our set of coordinates. Then, finally, to create high dimensional datasets these 2D coordinates are treated as principal component scores and projected into higher dimensions using fixed eigenvectors that are simulated again from a Gaussian distribution.</p>
<p>Let’s take a look at generating a six cluster dataset with clusterlab. The plots show the data in 2D principal component space.</p>
<pre>
library(clusterlab)
synthetic &lt;- clusterlab(centers=6,centralcluster=TRUE)
</pre>
<p><img data-orig-file="https://intobioinformatics.files.wordpress.com/2018/08/sixclustersolution.png?w=450&amp;h=492&amp;fit=492%2C492" data-orig-size="4960,4960" data-medium-file="https://intobioinformatics.files.wordpress.com/2018/08/sixclustersolution.png?w=450&amp;h=492&amp;fit=492%2C492?w=300" data-attachment-id="117" data-image-meta="{" data-image-description="" src="https://intobioinformatics.files.wordpress.com/2018/08/sixclustersolution.png?w=450&h=492&fit=492%2C492" data-permalink="https://intobioinformatics.wordpress.com/2018/08/25/simulating-nxn-dimensional-gaussian-clusters-in-r/sixclustersolution/" data-large-file="https://intobioinformatics.files.wordpress.com/2018/08/sixclustersolution.png?w=450&amp;h=492&amp;fit=492%2C492?w=620" alt="sixclustersolution.png" data-image-title="sixclustersolution" sizes="(max-width: 492px) 100vw, 492px" data-comments-opened="1" class="wp-image-117" data-recalc-dims="1"></p>
<p>Next, we will change the variance of the clusters using the ‘sdvec’ parameter, this allows the user to set the variance of each cluster individually.</p>
<pre>
synthetic &lt;- clusterlab(centers=6,sdvec=c(0.5,1,1.5,2,2,2))
</pre>
<p><img data-orig-file="https://intobioinformatics.files.wordpress.com/2018/08/sixclustersolution_sd_demo.png" data-orig-size="4960,4960" data-medium-file="https://intobioinformatics.files.wordpress.com/2018/08/sixclustersolution_sd_demo.png?w=300" data-attachment-id="122" data-image-meta="{" data-image-description="" src="https://intobioinformatics.files.wordpress.com/2018/08/sixclustersolution_sd_demo.png?w=450&h=478&fit=478%2C478" data-permalink="https://intobioinformatics.wordpress.com/2018/08/25/simulating-nxn-dimensional-gaussian-clusters-in-r/sixclustersolution_sd_demo/" data-large-file="https://intobioinformatics.files.wordpress.com/2018/08/sixclustersolution_sd_demo.png?w=620" alt="sixclustersolution_sd_demo.png" data-image-title="sixclustersolution_sd_demo" sizes="(max-width: 478px) 100vw, 478px" data-comments-opened="1" class="wp-image-122" data-recalc-dims="1"></p>
<p>We can also modify the ‘alpha’ parameter to push each cluster away from its central coordinate by a specified degree (a scalar) as follows:</p>
<pre>
synthetic &lt;- clusterlab(centers=8,alphas=c(0,1,2,3,4,5,6,7))
</pre>
<p><img data-orig-file="https://intobioinformatics.files.wordpress.com/2018/08/eightclustersolution_alphas_demo.png?w=450&amp;h=468&amp;fit=468%2C468" data-orig-size="4960,4960" data-medium-file="https://intobioinformatics.files.wordpress.com/2018/08/eightclustersolution_alphas_demo.png?w=450&amp;h=468&amp;fit=468%2C468?w=300" data-attachment-id="123" data-image-meta="{" data-image-description="" src="https://intobioinformatics.files.wordpress.com/2018/08/eightclustersolution_alphas_demo.png?w=450&h=468&fit=468%2C468" data-permalink="https://intobioinformatics.wordpress.com/2018/08/25/simulating-nxn-dimensional-gaussian-clusters-in-r/eightclustersolution_alphas_demo/" data-large-file="https://intobioinformatics.files.wordpress.com/2018/08/eightclustersolution_alphas_demo.png?w=450&amp;h=468&amp;fit=468%2C468?w=620" alt="eightclustersolution_alphas_demo.png" data-image-title="eightclustersolution_alphas_demo" sizes="(max-width: 468px) 100vw, 468px" data-comments-opened="1" class="wp-image-123" data-recalc-dims="1"></p>
<p>Let’s also try simulating a more complex multi-ringed structure we talked about earlier. In this code we set the ‘ringthetas’ parameter to rotate the individual rings by a specified number of degrees (theta).</p>
<pre>
synthetic &lt;- clusterlab(centers=5,r=7,sdvec=c(6,6,6,6,6),
alphas=c(2,2,2,2,2),centralcluster=FALSE,
numbervec=c(50,50,50,50),rings=5,ringalphas=c(2,4,6,8,10,12),
ringthetas = c(30,90,180,0,0,0), seed=123)
</pre>
<p><img data-orig-file="https://intobioinformatics.files.wordpress.com/2018/08/multiringedsolution.png" data-orig-size="4960,4960" data-medium-file="https://intobioinformatics.files.wordpress.com/2018/08/multiringedsolution.png?w=300" data-attachment-id="118" data-image-meta="{" data-image-description="" src="https://intobioinformatics.files.wordpress.com/2018/08/multiringedsolution.png?w=450&h=486&fit=486%2C486" data-permalink="https://intobioinformatics.wordpress.com/2018/08/25/simulating-nxn-dimensional-gaussian-clusters-in-r/multiringedsolution/" data-large-file="https://intobioinformatics.files.wordpress.com/2018/08/multiringedsolution.png?w=620" alt="multiringedsolution.png" data-image-title="multiringedsolution" sizes="(max-width: 486px) 100vw, 486px" data-comments-opened="1" class="wp-image-118" data-recalc-dims="1"></p>
<p>As a last demonstration, we will find the optimal K of the six cluster solution using the Bioconductor M3C package (<a rel="nofollow" href="https://www.bioconductor.org/packages/devel/bioc/html/M3C.html" target="_blank">https://www.bioconductor.org/packages/devel/bioc/html/M3C.html</a>), which is a consensus clustering algorithm I use. We are going to use a smaller test dataset because I am on my laptop and it is quite old without many cores. And, we are going to set the Monte Carlo iterations parameter to 50 and use Ward’sÂ hierarchical clustering because that should speed things up.</p>
<pre>
synthetic &lt;- clusterlab(centers=6,centralcluster=TRUE,features=1000,
numbervec=c(30,30,30,30,30,30))
library(M3C)
d &lt;- synthetic$synthetic_data
res &lt;- M3C(d,iters=50,printres = TRUE,clusteralg = 'hc')
</pre>
<p><img data-orig-file="https://intobioinformatics.files.wordpress.com/2018/08/rcsi1.png?w=413&amp;h=289&amp;fit=413%2C289&amp;resize=413%2C289" data-orig-size="7086,4960" data-medium-file="https://intobioinformatics.files.wordpress.com/2018/08/rcsi1.png?w=413&amp;h=289&amp;fit=413%2C289&amp;resize=413%2C289?w=300" data-attachment-id="119" data-image-meta="{" data-image-description="" src="https://intobioinformatics.files.wordpress.com/2018/08/rcsi1.png?w=413&h=289&fit=413%2C289&resize=413%2C289" data-permalink="https://intobioinformatics.wordpress.com/2018/08/25/simulating-nxn-dimensional-gaussian-clusters-in-r/rcsi-2/" data-large-file="https://intobioinformatics.files.wordpress.com/2018/08/rcsi1.png?w=413&amp;h=289&amp;fit=413%2C289&amp;resize=413%2C289?w=620" alt="RCSI.png" data-image-title="RCSI" sizes="(max-width: 413px) 100vw, 413px" width="413" data-comments-opened="1" class="wp-image-119" data-recalc-dims="1" height="289"></p>
<p>So as there is a maximal Relative Cluster Stability Index (RCSI) for K=6, M3C confirms our simulation. I think this is pretty neat. The clusterlab and M3C methods are described in our bioRxiv preprint (<a rel="nofollow" href="https://www.biorxiv.org/content/early/2018/07/25/377002" target="_blank">https://www.biorxiv.org/content/early/2018/07/25/377002</a>).</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://intobioinformatics.wordpress.com/2018/08/25/simulating-nxn-dimensional-gaussian-clusters-in-r/"> R – intobioinformatics</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/ErUyLTZxrC4" height="1"></a></div>