<!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>Process and analyze mouse-tracking data — mousetrap • 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="Process and analyze mouse-tracking data — mousetrap"><meta property="og:description" content="The mousetrap package provides functions for importing, preprocessing,
analyzing, aggregating, and visualizing mouse-tracking data. In the
following, a brief overview of the functions in this package is given."><!-- 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.1</span>
      </span>
    </div>

    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav"><li>
  <a href="../reference/mousetrap.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>Process and analyze mouse-tracking data</h1>
    <small class="dont-index">Source: <a href="https://github.com/pascalkieslich/mousetrap/blob/HEAD/R/mousetrap.R" class="external-link"><code>R/mousetrap.R</code></a></small>
    <div class="hidden name"><code>mousetrap.Rd</code></div>
    </div>

    <div class="ref-description">
    <p>The mousetrap package provides functions for importing, preprocessing,
analyzing, aggregating, and visualizing mouse-tracking data. In the
following, a brief overview of the functions in this package is given.</p>
    </div>


    <div id="read-functions">
    <h2>Read functions</h2>
    


<p>Depending on the file format, one of the standard R functions for reading
files into R can be used (e.g., <a href="https://rdrr.io/r/utils/read.table.html" class="external-link">read.table</a> or
<a href="https://rdrr.io/r/utils/read.table.html" class="external-link">read.csv</a>).</p>
<p>If raw data were collected using
<a href="http://www.mousetracker.org/" class="external-link">MouseTracker</a>, the mousetrap package
provides the <a href="read_mt.html">read_mt</a> function to read files in the ".mt" format.</p>
<p>If several raw data files should be read and merged, the
<a href="https://rdrr.io/pkg/readbulk/man/read_bulk.html" class="external-link">read_bulk</a> function from the
<a href="http://pascalkieslich.github.io/readbulk/" class="external-link">readbulk</a> package can be
used (or the <a href="https://rdrr.io/pkg/readbulk/man/read_opensesame.html" class="external-link">read_opensesame</a> function, if data were
collected using <a href="https://osdoc.cogsci.nl/" class="external-link">OpenSesame</a>).</p>
    </div>
    <div id="import-functions">
    <h2>Import functions</h2>
    


<p>The initial step to prepare data for analysis in the mousetrap package is
to create a mousetrap data object. Depending on the input format, one of
the following functions can be used. A detailed description (and example)
of the resulting mousetrap data object can be found in <a href="mt_example.html">mt_example</a>.</p>
<p><a href="mt_import_mousetrap.html">mt_import_mousetrap</a> imports mouse-tracking data that were recorded
using the <a href="https://github.com/pascalkieslich/mousetrap-os" class="external-link">mousetrap
plugin</a> for <a href="https://osdoc.cogsci.nl/" class="external-link">OpenSesame</a>.</p>
<p><a href="mt_import_wide.html">mt_import_wide</a> imports mouse-tracking data saved in a wide format
(e.g., data collected using
<a href="http://www.mousetracker.org/" class="external-link">MouseTracker</a>).</p>
<p><a href="mt_import_long.html">mt_import_long</a> imports mouse-tracking data saved in a long format.
(e.g., trajectories exported using <a href="mt_export_long.html">mt_export_long</a>).</p>
    </div>
    <div id="geometric-preprocessing-functions">
    <h2>Geometric preprocessing functions</h2>
    


<p>A number of functions are available that perform geometric preprocessing
operations.</p>
<p><a href="mt_remap_symmetric.html">mt_remap_symmetric</a> remaps mouse trajectories to one side (or one
quadrant) of the coordinate system.</p>
<p><a href="mt_align.html">mt_align</a> is a general purpose function for aligning and rescaling
trajectories. For specific operations, you can rely on one of the
following functions.</p>
<p><a href="mt_align_start.html">mt_align_start</a> aligns the start position of trajectories.</p>
<p><a href="mt_align_start_end.html">mt_align_start_end</a> aligns all trajectories so that they share a
common initial and final coordinate (this is also sometimes referred to as
"space-normalization").</p>
    </div>
    <div id="resampling-and-interpolation-functions">
    <h2>Resampling and interpolation functions</h2>
    


<p>A number of functions are available that perform resampling and
interpolation operations.</p>
<p><a href="mt_exclude_initiation.html">mt_exclude_initiation</a>	excludes the initial phase of a trial without
mouse movement.</p>
<p><a href="mt_exclude_finish.html">mt_exclude_finish</a>	excludes a potential phase without mouse movement at the end of a trial.</p>
<p><a href="mt_time_normalize.html">mt_time_normalize</a>	performs time-normalization using equidistant time
intervals, resulting in an identical number of samples for all
trajectories.</p>
<p><a href="mt_resample.html">mt_resample</a>	resamples trajectories so that samples occur at constant
intervals of a specified length.</p>
<p><a href="mt_average.html">mt_average</a> averages trajectory coordinates (and related variables)
for time bins of constant duration.</p>
<p><a href="mt_length_normalize.html">mt_length_normalize</a> re-represents each trajectory spatially so that
adjacent points on the trajectory become equidistant to each other.</p>
    </div>
    <div id="data-handling-functions">
    <h2>Data handling functions</h2>
    


<p>A number of functions are available for data handling operations, such as
filtering or adding of new variables or trajectories.</p>
<p><a href="mt_subset.html">mt_subset</a>	filters mouse-tracking data by trials, so that only those
meeting the defined criteria are included.</p>
<p><a href="mt_add_variables.html">mt_add_variables</a> adds new, self created variables to a trajectory
array.</p>
<p><a href="mt_add_trajectory.html">mt_add_trajectory</a> adds a new trajectory to a trajectory array.</p>
<p><a href="mt_bind.html">mt_bind</a> joins two trajectory arrays.</p>
    </div>
    <div id="analysis-functions">
    <h2>Analysis functions</h2>
    


<p>A number of different analysis procedures and summary statistics for mouse
trajectories have been established in the existing literature. The following
functions implement many of these approaches.</p>
<p><a href="mt_derivatives.html">mt_derivatives</a> calculates distance, velocity, and
acceleration for trajectories.</p>
<p><a href="mt_angles.html">mt_angles</a> calculates movement angles for trajectories.</p>
<p><a href="mt_deviations.html">mt_deviations</a> calculates the deviations from an idealized
trajectory (straight line).</p>
<p><a href="mt_measures.html">mt_measures</a>	calculates a set of mouse-tracking measures.</p>
<p><a href="mt_sample_entropy.html">mt_sample_entropy</a>	calculates sample entropy.</p>
<p><a href="mt_standardize.html">mt_standardize</a> standardizes mouse-tracking measures onto a common
scale (separately for subsets of the data, e.g., per participant).</p>
<p><a href="mt_scale_trajectories.html">mt_scale_trajectories</a> provides different options for standardizing
variables in a mouse trajectory array.</p>
<p><a href="mt_check_bimodality.html">mt_check_bimodality</a>	assesses the bimodality of mouse-tracking
measure distributions.</p>
<p><a href="mt_check_resolution.html">mt_check_resolution</a>	checks the (temporal) logging resolution of raw
trajectories.</p>
<p><a href="mt_count.html">mt_count</a> counts the number of observations for each trajectory.</p>
    </div>
    <div id="cluster-functions">
    <h2>Cluster functions</h2>
    


<p>A number of different functions for clustering trajectories is provided.</p>
<p><a href="mt_distmat.html">mt_distmat</a> computes the dissimilarity/distance between each pair of
trajectories.</p>
<p><a href="mt_cluster.html">mt_cluster</a> performs trajectory clustering with a specified number of
clusters.</p>
<p><a href="mt_cluster_k.html">mt_cluster_k</a> estimates the optimal number of clusters using various
methods.</p>
<p><a href="mt_map.html">mt_map</a> maps trajectories onto a predefined set of prototype
trajectories (a core set is provided in <a href="mt_prototypes.html">mt_prototypes</a>).</p>
    </div>
    <div id="reshaping-aggregation-and-export-functions">
    <h2>Reshaping, aggregation, and export functions</h2>
    


<p>A number of helper functions are provided for aggregating, plotting, and
exporting the multi-dimensional mouse trajectory arrays.</p>
<p><a href="mt_reshape.html">mt_reshape</a> is a general purpose reshaping and aggregation function
for mousetrap data.</p>
<p><a href="mt_aggregate.html">mt_aggregate</a> aggregates mouse-tracking data per condition.</p>
<p><a href="mt_aggregate_per_subject.html">mt_aggregate_per_subject</a>	aggregates mouse-tracking data per
(within subjects-) condition separately for each subject.</p>
<p><a href="mt_export_long.html">mt_export_long</a> exports mouse-tracking data in long format.</p>
<p><a href="mt_export_long.html">mt_export_wide</a> exports mouse-tracking data in wide format.</p>
    </div>
    <div id="visualization-functions">
    <h2>Visualization functions</h2>
    


<p>The following functions can be used for plotting trajectory data, e.g.,
individual and aggregated trajectories or velocity profiles.</p>
<p><a href="mt_plot.html">mt_plot</a>	plots individual trajectory data.</p>
<p><a href="mt_plot.html">mt_plot_aggregate</a>	plots aggregated trajectory data.</p>
<p><a href="mt_plot_add_rect.html">mt_plot_add_rect</a> adds rectangles to a trajectory plot.</p>
<p><a href="mt_plot_riverbed.html">mt_plot_riverbed</a> plots the relative frequency of a selected variable
across time.</p>
<p><a href="mt_plot_per_trajectory.html">mt_plot_per_trajectory</a> creates a pdf with separate plots per
trajectory.</p>
<p><a href="mt_heatmap.html">mt_heatmap</a> and <a href="mt_heatmap_ggplot.html">mt_heatmap_ggplot</a> plot trajectory heatmaps.</p>
<p><a href="mt_diffmap.html">mt_diffmap</a> for creating a difference-heatmap of two trajectory
heatmap images.</p>
<p><a href="mt_animate.html">mt_animate</a> creates a gif trajectory animation.</p>
    </div>
    <div id="helper-functions">
    <h2>Helper functions</h2>
    


<p><a href="bimodality_coefficient.html">bimodality_coefficient</a> calculates the bimodality coefficient.</p>
<p><a href="scale_within.html">scale_within</a> scales and centers variables within the levels of
another variable.</p>
<p><a href="bezier.html">bezier</a> creates Bezier-curves using the Bernstein approximation.</p>
    </div>
    <div id="datasets">
    <h2>Datasets</h2>
    


<p><a href="mt_example.html">mt_example</a> and <a href="mt_example_raw.html">mt_example_raw</a> contain a mouse-tracking
example dataset for demonstrations using the <code>mousetrap</code> package.</p>
<p><a href="KH2017.html">KH2017</a> and <a href="KH2017_raw.html">KH2017_raw</a> contain a mouse-tracking dataset from
Kieslich &amp; Henninger (2017).</p>
    </div>

    <div id="ref-examples">
    <h2>Examples</h2>
    <div class="sourceCode"><pre class="sourceCode r"><code><span class="r-in"><span class="kw">if</span> <span class="op">(</span><span class="cn">FALSE</span><span class="op">)</span> <span class="op">{</span></span>
<span class="r-in"><span class="va">KH2017</span> <span class="op">&lt;-</span> <span class="fu"><a href="mt_import_mousetrap.html">mt_import_mousetrap</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/subset.html" class="external-link">subset</a></span><span class="op">(</span><span class="va">KH2017_raw</span>,<span class="va">correct</span><span class="op">==</span><span class="fl">1</span><span class="op">)</span><span class="op">)</span></span>
<span class="r-in"><span class="va">KH2017</span> <span class="op">&lt;-</span> <span class="fu"><a href="mt_remap_symmetric.html">mt_remap_symmetric</a></span><span class="op">(</span><span class="va">KH2017</span><span class="op">)</span></span>
<span class="r-in"><span class="va">KH2017</span> <span class="op">&lt;-</span> <span class="fu"><a href="mt_align_start.html">mt_align_start</a></span><span class="op">(</span><span class="va">KH2017</span><span class="op">)</span></span>
<span class="r-in"><span class="op">}</span></span>
<span class="r-in"></span>
<span class="r-in"><span class="va">KH2017</span> <span class="op">&lt;-</span> <span class="fu"><a href="mt_time_normalize.html">mt_time_normalize</a></span><span class="op">(</span><span class="va">KH2017</span><span class="op">)</span></span>
<span class="r-in"><span class="va">KH2017</span> <span class="op">&lt;-</span> <span class="fu"><a href="mt_measures.html">mt_measures</a></span><span class="op">(</span><span class="va">KH2017</span><span class="op">)</span></span>
<span class="r-in"></span>
<span class="r-in"><span class="fu"><a href="mt_aggregate.html">mt_aggregate</a></span><span class="op">(</span></span>
<span class="r-in">  <span class="va">KH2017</span>, use<span class="op">=</span><span class="st">"measures"</span>,</span>
<span class="r-in">  use_variables<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">"MAD"</span>, <span class="st">"AD"</span><span class="op">)</span>,</span>
<span class="r-in">  use2_variables<span class="op">=</span><span class="st">"Condition"</span>,</span>
<span class="r-in">  subject_id<span class="op">=</span><span class="st">"subject_nr"</span></span>
<span class="r-in"><span class="op">)</span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span>   Condition      MAD        AD</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> 1  Atypical 343.7954 100.36799</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> 2   Typical 172.2093  39.72137</span>
<span class="r-in"></span>
<span class="r-in"><span class="fu"><a href="mt_plot.html">mt_plot_aggregate</a></span><span class="op">(</span><span class="va">KH2017</span>,</span>
<span class="r-in">  use<span class="op">=</span><span class="st">"tn_trajectories"</span>,</span>
<span class="r-in">  x<span class="op">=</span><span class="st">"xpos"</span>, y<span class="op">=</span><span class="st">"ypos"</span>, color<span class="op">=</span><span class="st">"Condition"</span>,</span>
<span class="r-in">  subject_id<span class="op">=</span><span class="st">"subject_nr"</span></span>
<span class="r-in"><span class="op">)</span></span>
<span class="r-plt img"><img src="mousetrap-1.png" alt="" width="700" height="433"></span>
<span class="r-in"></span>
<span class="r-in"><span class="kw">if</span> <span class="op">(</span><span class="cn">FALSE</span><span class="op">)</span> <span class="op">{</span></span>
<span class="r-in"><span class="fu"><a href="mt_plot.html">mt_plot</a></span><span class="op">(</span><span class="va">KH2017</span>,</span>
<span class="r-in">  use<span class="op">=</span><span class="st">"tn_trajectories"</span>,</span>
<span class="r-in">  x<span class="op">=</span><span class="st">"xpos"</span>, y<span class="op">=</span><span class="st">"ypos"</span>, color<span class="op">=</span><span class="st">"Condition"</span></span>
<span class="r-in"><span class="op">)</span></span>
<span class="r-in"><span class="op">}</span></span>
<span class="r-in"></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.1.</p>
</div>

      </footer></div>

  


  

  </body></html>

