Jani Kotakoski's blog -- RSS Feed code\n"http://kotakoski.fi/" Thoughts on science, sports and everything else R-R-Running with R(Plotting GPS tracks and heart rate data) http://kotakoski.fi/blog.php?blogpost=20140929-RRRunningWithR http://kotakoski.fi/blog.php?blogpost=20140929-RRRunningWithR Tuesday, 30th September, 2014 <?php $id = (string) "20140929-RRRunningWithR"; ?> <h1>R-R-Running with R<br/>(Plotting GPS tracks and heart rate data)</h1> <?php blogref($id); ?> <div class="txt"> <p> I have recently been toying around with the idea to get myself a bit better versed in modern tools used in data analysis and so-called big data. One of the often mentioned tools on this arena is <b>R</b>, the <a href="http://www.r-project.org/">free software environment for statistical computing and graphics</a>. Since I like to practice with new programming languages, I have already for a couple of months been looking for a good excuse to try R in a real application. </p> <p> Until recently, I have been logging all my running activities on online services, which have provided a nice overview on each of the runs individually and also a convenient way for browsing through old entries. However, this has always felt a bit silly&mdash;while having the data online is good for sharing on social media, for anything that interests just myself it is crazy. Nevertheless, unaware of any good offline tools for this purpose, I was stuck with the online solutions. As you can guess, this is where R came to the resque. I can't remember how it happened, but this Sunday I landed on <b>Mollie Taylor</b>'s blog, where she discussed <a href="http://blog.mollietaylor.com/2012/12/mapping-gps-tracks-in-r.html">Mapping GPS Tracks in R</a>. Now I had the long-awaited excuse for getting my hands dirty with R. </p> <h2>Obtaining data from the GPS watch</h2> <p> I already had the GPS and heart rate data from my <em>Garmin Forerunner 405</em> transferred to the harddrive with <b>Braiden Kindt</b>'s <a href="https://github.com/braiden/python-ant-downloader">python-ant-downloader</a>, and Mollie pointed out in her blog that the tcx files I had could be converted into csv using <a href="http://www.gpsbabel.org/">GPSBabel</a>. What I still needed to figure out myself was how to use <a href="http://www.openstreetmap.org/">OpenStreetMap</a> instead of data from one specific colossal corporation, how to include several plots within one figure and how to populate those plots with the data I wanted. </p> <h2>Calculating accurate distance from GPS data</h2> <p> The trickiest part turned out to be calculating accurate distances from the GPS coordinates. For reasons I can't fully comprehend, it turns out that almost everybody assumes that the Earth is a perfect ball when calculating the distance. This is obviously not true, and since I often run on different continents, I wanted to get this one nailed down. Of course, I wasn't the first one looking for a solution to this problem. The most reasonable implementation for R, by <b>Mario Pineda-Krch</b>, can be found on <a href="http://www.r-bloggers.com/great-circle-distance-calculations-in-r/">r-bloggers.com</a>. It is based on JavaScript code by <b>Chriss Veness</b> available at <a href="http://www.movable-type.co.uk/scripts/latlong-vincenty.html">movable-type.co.uk</a> (attribution license). (The <a href="https://en.wikipedia.org/wiki/Vincenty%27s_formulae">theory behind the method</a> is by <b>Thaddeus Vincenty</b>). Also this formula, however, disregards altitude variations, which can be significant if you like to run on hilly areas like I do. So, I needed to add that part myself. </p> <h2>Plotting the pace</h2> <p> Another point to consider was that the point-by-point pace information was much too noisy for being directly plotted from the data. I ended up solving this by calculating the average pace for each 100 m trek and plotting those instead of each individual point. The result seems to be reasonably good in terms of comparison with empirical analysis (pace variations while running). </p> <h2>End result</h2> <p> The end result from my Sunday coding is now R code, which produces results such as this one: </p> <p> <center> <img class="imgblog" src="pics/example.svg" alt="A 8.5 km run in New York City in 2014"/> <br/> <b>A 8.5 km run in New York City in 2014</b> </center> </p> <h2>Code is available on Github</h2> <p> The <tt>runmap</tt> code itself is available on <a href="https://github.com/jkkoski/runmap">github</a> under <a href="https://github.com/jkkoski/runmap/blob/master/LICENSE.txt">the MIT license</a>. I hope it will be useful also for others. </p> </div> <hr/> <?php blogcomment($id); ?>