<!DOCTYPE html>
<!-- Generated by pkgdown: do not edit by hand --><html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Estimate optimal number of clusters. — mt_cluster_k • mousetrap</title><!-- jquery --><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script><!-- Bootstrap --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha256-bZLfwXAP04zRMK2BjiO8iu9pf4FbLqX6zitd+tIvLhE=" crossorigin="anonymous"><script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha256-nuL8/2cJ5NDSSwnKD8VqreErSWHtnEP9E7AySL+1ev4=" crossorigin="anonymous"></script><!-- bootstrap-toc --><link rel="stylesheet" href="../bootstrap-toc.css"><script src="../bootstrap-toc.js"></script><!-- Font Awesome icons --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" integrity="sha256-mmgLkCYLUQbXn0B1SRqzHar6dCnv9oZFPEC1g1cwlkk=" crossorigin="anonymous"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/v4-shims.min.css" integrity="sha256-wZjR52fzng1pJHwx4aV2AO3yyTOXrcDW7jBpJtTwVxw=" crossorigin="anonymous"><!-- clipboard.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><!-- headroom.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/headroom.min.js" integrity="sha256-AsUX4SJE1+yuDu5+mAVzJbuYNPHj/WroHuZ8Ir/CkE0=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/jQuery.headroom.min.js" integrity="sha256-ZX/yNShbjqsohH1k95liqY9Gd8uOiE1S4vZc+9KQ1K4=" crossorigin="anonymous"></script><!-- pkgdown --><link href="../pkgdown.css" rel="stylesheet"><script src="../pkgdown.js"></script><meta property="og:title" content="Estimate optimal number of clusters. — mt_cluster_k"><meta property="og:description" content="Estimates the optimal number of clusters (k) using various methods."><!-- mathjax --><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script><!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]--></head><body data-spy="scroll" data-target="#toc">
    

    <div class="container template-reference-topic">
      <header><div class="navbar navbar-default navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <span class="navbar-brand">
        <a class="navbar-link" href="../index.html">mousetrap</a>
        <span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="">3.2.3</span>
      </span>
    </div>

    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav"><li>
  <a href="../reference/mousetrap-package.html">Overview</a>
</li>
<li>
  <a href="../reference/index.html">Reference</a>
</li>
<li>
  <a href="../news/index.html">News</a>
</li>
      </ul><ul class="nav navbar-nav navbar-right"><li>
  <a href="https://github.com/pascalkieslich/mousetrap" class="external-link">
    <span class="fa fa-github fa-lg"></span>
     
  </a>
</li>
      </ul></div><!--/.nav-collapse -->
  </div><!--/.container -->
</div><!--/.navbar -->

      

      </header><div class="row">
  <div class="col-md-9 contents">
    <div class="page-header">
    <h1>Estimate optimal number of clusters.</h1>
    <small class="dont-index">Source: <a href="https://github.com/pascalkieslich/mousetrap/blob/HEAD/R/cluster_k.R" class="external-link"><code>R/cluster_k.R</code></a></small>
    <div class="hidden name"><code>mt_cluster_k.Rd</code></div>
    </div>

    <div class="ref-description">
    <p>Estimates the optimal number of clusters (<code>k</code>) using various methods.</p>
    </div>

    <div id="ref-usage">
    <div class="sourceCode"><pre class="sourceCode r"><code><span><span class="fu">mt_cluster_k</span><span class="op">(</span></span>
<span>  <span class="va">data</span>,</span>
<span>  use <span class="op">=</span> <span class="st">"ln_trajectories"</span>,</span>
<span>  dimensions <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"xpos"</span>, <span class="st">"ypos"</span><span class="op">)</span>,</span>
<span>  kseq <span class="op">=</span> <span class="fl">2</span><span class="op">:</span><span class="fl">15</span>,</span>
<span>  compute <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"stability"</span>, <span class="st">"gap"</span>, <span class="st">"jump"</span>, <span class="st">"slope"</span><span class="op">)</span>,</span>
<span>  method <span class="op">=</span> <span class="st">"hclust"</span>,</span>
<span>  weights <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/rep.html" class="external-link">rep</a></span><span class="op">(</span><span class="fl">1</span>, <span class="fu"><a href="https://rdrr.io/r/base/length.html" class="external-link">length</a></span><span class="op">(</span><span class="va">dimensions</span><span class="op">)</span><span class="op">)</span>,</span>
<span>  pointwise <span class="op">=</span> <span class="cn">TRUE</span>,</span>
<span>  minkowski_p <span class="op">=</span> <span class="fl">2</span>,</span>
<span>  hclust_method <span class="op">=</span> <span class="st">"ward.D"</span>,</span>
<span>  kmeans_nstart <span class="op">=</span> <span class="fl">10</span>,</span>
<span>  n_bootstrap <span class="op">=</span> <span class="fl">10</span>,</span>
<span>  model_based <span class="op">=</span> <span class="cn">FALSE</span>,</span>
<span>  n_gap <span class="op">=</span> <span class="fl">10</span>,</span>
<span>  na_rm <span class="op">=</span> <span class="cn">FALSE</span>,</span>
<span>  verbose <span class="op">=</span> <span class="cn">FALSE</span></span>
<span><span class="op">)</span></span></code></pre></div>
    </div>

    <div id="arguments">
    <h2>Arguments</h2>
    <dl><dt>data</dt>
<dd><p>a mousetrap data object created using one of the mt_import
functions (see <a href="mt_example.html">mt_example</a> for details). Alternatively, a trajectory
array can be provided directly (in this case <code>use</code> will be ignored).</p></dd>


<dt>use</dt>
<dd><p>a character string specifying which trajectory data should be
used.</p></dd>


<dt>dimensions</dt>
<dd><p>a character vector specifying which trajectory variables
should be used. Can be of length 2 or 3, for two-dimensional or
three-dimensional trajectories respectively.</p></dd>


<dt>kseq</dt>
<dd><p>a numeric vector specifying set of candidates for k. Defaults to
2:15, implying that all values of k within that range are compared using
the metrics specified in <code>compute</code>.</p></dd>


<dt>compute</dt>
<dd><p>character vector specifying the to be computed measures. Can
be any subset of <code>c("stability","gap","jump","slope")</code>.</p></dd>


<dt>method</dt>
<dd><p>character string specifying the type of clustering procedure
for the stability-based method. Either <code>hclust</code> or <code>kmeans</code>.</p></dd>


<dt>weights</dt>
<dd><p>numeric vector specifying the relative importance of the
variables specified in <code>dimensions</code>. Defaults to a vector of 1s
implying equal importance. Technically, each variable is rescaled so that
the standard deviation matches the corresponding value in <code>weights</code>.
To use the original variables, set <code>weights = NULL</code>.</p></dd>


<dt>pointwise</dt>
<dd><p>boolean specifying the way in which dissimilarity between
the trajectories is measured. If <code>TRUE</code> (the default),
<code>mt_distmat</code> measures the average dissimilarity and then sums the
results. If <code>FALSE</code>, <code>mt_distmat</code>  measures dissimilarity once
(by treating the various points as independent dimensions). This is only
relevant if <code>method</code> is "hclust". See <a href="mt_distmat.html">mt_distmat</a> for further
details.</p></dd>


<dt>minkowski_p</dt>
<dd><p>an integer specifying the distance metric for the cluster
solution. <code>minkowski_p = 1</code> computes the city-block distance,
<code>minkowski_p = 2</code> (the default) computes the Euclidian distance,
<code>minkowski_p = 3</code> the cubic distance, etc. Only relevant if
<code>method</code> is "hclust". See <a href="mt_distmat.html">mt_distmat</a> for further details.</p></dd>


<dt>hclust_method</dt>
<dd><p>character string specifying the linkage criterion used.
Passed on to the <code>method</code> argument of <a href="https://rdrr.io/r/stats/hclust.html" class="external-link">hclust</a>. Default is
set to <code>ward.D</code>. Only relevant if <code>method</code> is "hclust".</p></dd>


<dt>kmeans_nstart</dt>
<dd><p>integer specifying the number of reruns of the kmeans
procedure. Larger numbers minimize the risk of finding local minima. Passed
on to the <code>nstart</code> argument of <a href="https://rdrr.io/r/stats/kmeans.html" class="external-link">kmeans</a>. Only relevant if
<code>method</code> is "kmeans".</p></dd>


<dt>n_bootstrap</dt>
<dd><p>an integer specifying the number of bootstrap comparisons
used by <code>stability</code>. See <a href="https://rdrr.io/pkg/cstab/man/cStability.html" class="external-link">cStability</a>.</p></dd>


<dt>model_based</dt>
<dd><p>boolean specifying whether the model-based or the
model-free should be used by <code>stability</code>, when method is
<code>kmeans</code>. See <a href="https://rdrr.io/pkg/cstab/man/cStability.html" class="external-link">cStability</a> and Haslbeck &amp; Wulff (2020).</p></dd>


<dt>n_gap</dt>
<dd><p>integer specifying the number of simulated datasets used by
<code>gap</code>. See Tibshirani et al. (2001).</p></dd>


<dt>na_rm</dt>
<dd><p>logical specifying whether trajectory points containing NAs
should be removed. Removal is done column-wise. That is, if any trajectory
has a missing value at, e.g., the 10th recorded position, the 10th position
is removed for all trajectories. This is necessary to compute distance
between trajectories.</p></dd>


<dt>verbose</dt>
<dd><p>logical indicating whether function should report its
progress.</p></dd>

</dl></div>
    <div id="value">
    <h2>Value</h2>
    

<p>A list containing two lists that store the results of the different
methods. <code>kopt</code> contains the estimated <code>k</code> for each of the
methods specified in <code>compute</code>. <code>paths</code> contains the values for
each <code>k</code> in <code>kseq</code> as computed by each of the methods specified
in <code>compute</code>. The values in <code>kopt</code> are optima for each of the
vectors in <code>paths</code>.</p>
    </div>
    <div id="details">
    <h2>Details</h2>
    <p><code>mt_cluster_k</code> estimates the number of clusters (<code>k</code>) using four
commonly used k-selection methods (specified via <code>compute</code>): cluster
stability (<code>stability</code>), the gap statistic (<code>gap</code>), the jump
statistic (<code>jump</code>), and the slope statistic (<code>slope</code>).</p>
<p>Cluster stability methods select <code>k</code> as the number of clusters for which
the assignment of objects to clusters is most stable across bootstrap
samples. This function implements the model-based and model-free methods
described by Haslbeck &amp; Wulff (2020). See references.</p>
<p>The remaining three methods select <code>k</code> as the value that optimizes the
gap statistic (Tibshirani, Walther, &amp; Hastie, 2001), the jump statistic
(Sugar &amp; James, 2013), and the slope statistic (Fujita, Takahashi, &amp;
Patriota, 2014), respectively.</p>
<p>For clustering trajectories, it is often useful that the endpoints of all
trajectories share the same direction, e.g., that all trajectories end in the
top-left corner of the coordinate system (<a href="mt_remap_symmetric.html">mt_remap_symmetric</a> or
<a href="mt_align.html">mt_align</a> can be used to achieve this). Furthermore, it is recommended
to use length normalized trajectories (see <a href="mt_length_normalize.html">mt_length_normalize</a>; Wulff
et al., 2019).</p>
    </div>
    <div id="references">
    <h2>References</h2>
    <p>Haslbeck, J. M. B., &amp; Wulff, D. U. (2020). Estimating the Number
of Clusters via a Corrected Clustering Instability. <em>Computational
Statistics, 35</em>, 1879–1894.</p>
<p>Wulff, D. U., Haslbeck, J. M. B., Kieslich, P. J., Henninger, F., &amp;
Schulte-Mecklenbeck, M. (2019). Mouse-tracking: Detecting types in movement
trajectories. In M. Schulte-Mecklenbeck, A. Kühberger, &amp; J. G. Johnson
(Eds.), <em>A Handbook of Process Tracing Methods</em> (pp. 131-145). New
York, NY: Routledge.</p>
<p>Tibshirani, R., Walther, G., &amp; Hastie, T. (2001). Estimating the number of
clusters in a data set via the gap statistic. <em>Journal of the Royal
Statistical Society: Series B (Statistical Methodology), 63</em>(2), 411-423.</p>
<p>Sugar, C. A., &amp; James, G. M. (2013). Finding the number of clusters in a
dataset. <em>Journal of the American Statistical Association, 98</em>(463),
750-763.</p>
<p>Fujita, A., Takahashi, D. Y., &amp; Patriota, A. G. (2014). A non-parametric
method to estimate the number of clusters. <em>Computational Statistics &amp;
Data Analysis, 73</em>, 27-39.</p>
    </div>
    <div id="see-also">
    <h2>See also</h2>
    <div class="dont-index"><p><a href="mt_distmat.html">mt_distmat</a> for more information about how the distance matrix
is computed when the hclust method is used.</p>
<p><a href="mt_cluster.html">mt_cluster</a> for performing trajectory clustering with a specified
number of clusters.</p></div>
    </div>
    <div id="author">
    <h2>Author</h2>
    <p>Dirk U. Wulff</p>
<p>Jonas M. B. Haslbeck</p>
    </div>

    <div id="ref-examples">
    <h2>Examples</h2>
    <div class="sourceCode"><pre class="sourceCode r"><code><span class="r-in"><span></span></span>
<span class="r-in"><span><span class="kw">if</span> <span class="op">(</span><span class="cn">FALSE</span><span class="op">)</span> <span class="op">{</span></span></span>
<span class="r-in"><span><span class="co"># Length normalize trajectories</span></span></span>
<span class="r-in"><span><span class="va">KH2017</span> <span class="op">&lt;-</span> <span class="fu"><a href="mt_length_normalize.html">mt_length_normalize</a></span><span class="op">(</span><span class="va">KH2017</span><span class="op">)</span></span></span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="co"># Find k</span></span></span>
<span class="r-in"><span><span class="va">results</span> <span class="op">&lt;-</span> <span class="fu">mt_cluster_k</span><span class="op">(</span><span class="va">KH2017</span>, use<span class="op">=</span><span class="st">"ln_trajectories"</span><span class="op">)</span></span></span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="co"># Retrieve results</span></span></span>
<span class="r-in"><span><span class="va">results</span><span class="op">$</span><span class="va">kopt</span></span></span>
<span class="r-in"><span><span class="va">results</span><span class="op">$</span><span class="va">paths</span></span></span>
<span class="r-in"><span><span class="op">}</span></span></span>
<span class="r-in"><span></span></span>
</code></pre></div>
    </div>
  </div>
  <div class="col-md-3 hidden-xs hidden-sm" id="pkgdown-sidebar">
    <nav id="toc" data-toggle="toc" class="sticky-top"><h2 data-toc-skip>Contents</h2>
    </nav></div>
</div>


      <footer><div class="copyright">
  <p></p><p>Developed by Pascal J. Kieslich, Dirk U. Wulff, Felix Henninger, Jonas M. B. Haslbeck.</p>
</div>

<div class="pkgdown">
  <p></p><p>Site built with <a href="https://pkgdown.r-lib.org/" class="external-link">pkgdown</a> 2.0.7.</p>
</div>

      </footer></div>

  


  

  </body></html>

