Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Selecting Data for mouse measures

Hi! Is there any easy way to pre-select data to make measures on before making any measures based on them?
Suppose I want to calculate total distance after a certain timestamp (e.g. after 600 ms since the start of the trial). Is there any (easy) way in Mousetrap library for it?
Thank you!


  • edited September 19


    that is in principle possible, but the specific implementation depends on the measure you want to use.

    The following code gives an example how you can calculate the Euclidean distance traveled from the beginning of the trajectory until timestamp 600 (ms):

    # Load mousetrap library
    # Calculate mouse-tracking derivatives for each position (distance, velocity, acceleration)
    mt_example <- mt_derivatives(mt_example)
    # Calculate standard mouse-tracking measures
    mt_example <- mt_measures(mt_example)
    # Demonstrate that sum of distances corresponds to total_dist measure
    # Set distances for timestamps beyond 600 ms to 0
    mt_example$trajectories[,,"dist"][mt_example$trajectories[,,"timestamps"]>600] <- 0
    # Calculate sum of distances for timestamps <= 600 ms as a new measure in mt_example$measures
    mt_example$measures$total_dist_600 <- rowSums(mt_example$trajectories[,,"dist"],na.rm=TRUE)

    Hope this helps. If you have any additional questions, feel free to ask!



  • edited September 2018

    Thank you so much! It really does help!

    I have one additional question: how do I summarize these measures by subjID (and possibly, by some other factors)?

    thank you!

  • This is how I did it:

    average_measures <- mt_aggregate_per_subject(
  • You can use the mt_aggregate_per_subject function for this. If you specify a condition variable in use2_variables, the measures will be summarized per participant and condition.

    average_measures <- mt_aggregate_per_subject(
      use_variables=c("total_dist_600 "),



  • I have another question. Should I want to calculate other measures, like acceleration and velocity, will setting trajectories to zero be sufficient? In other words, if i want to calculate say velocity after certain time period, do I only need to change total_dist to vel_max?

    Thank you!

  • You would have to make a few changes:

    # Set velocities for timestamps beyond 600 ms to 0
    mt_example$trajectories[,,"vel"][mt_example$trajectories[,,"timestamps"]>600] <- 0
    # Calculate maximum velocity
    mt_example$measures$vel_max_600 <- apply(mt_example$trajectories[,,"vel"],1,max,na.rm=TRUE)

    Instead of the second line, you could also run mt_measures afterwards. It will automatically calculate the total_dist and vel_max based on the modified values, so the resulting values for total_dist an vel_max will be the ones for the first 600 ms.

Sign In or Register to comment.