metachronistic http://swingleydev.com/blog/ Latest metachronistic posts en-us Sat, 09 Jan 2016 15:00:45 -0900 FitBit Charge HR Accuracy During Exercise http://swingleydev.com/blog/p/1992/ <div class="document"> <div class="section" id="introduction"> <h1>Introduction</h1> <p>This week a class action lawsuit was filed against FitBit, claiming that their heart rate fitness trackers don’t perform as advertised, specifically during exercise. I’ve been wearing a FitBit Charge HR since October, and also wear a Scosche Rhythm+ heart rate monitor whenever I exercise, so I have a lot of data that I can use to assess the legitimacy of the lawsuit. The data and RMarkdown file used to produce this post is available from <a class="reference external" href="https://github.com/cswingle/fitbit_hr">GitHub</a>.</p> </div> <div class="section" id="the-data"> <h1>The Data</h1> <p>Heart rate data from the Rhythm+ is collected by the RunKeeper app on my phone, and after transferring the data to RunKeeper’s website, I can download GPX files containing all the GPS and heart rate data for each exercise. Data from the Charge HR is a little harder to get, but with the proper tools and permission from FitBit, you can get what they call “intraday” data. I use the <tt class="docutils literal">fitbit</tt> Python library and a set of routines I wrote (also available from <a class="reference external" href="https://github.com/cswingle/fb2psql">GitHub</a>) to pull this data.</p> <p>The data includes 116 activities, mostly from commuting to and from work by bicycle, fat bike, or on skis. The first step in the process is to pair the two data sets, but since the exact moment when each sensor recorded data won’t match, I grouped both sets of data into 15-second intervals, and calculated the mean heart rate for each sensor withing that 15-second window. The result looks like this:</p> <div class="highlight"><pre><span class="kp">load</span><span class="p">(</span><span class="s">&quot;matched_hr_data.rdata&quot;</span><span class="p">)</span> kable<span class="p">(</span><span class="kp">head</span><span class="p">(</span>matched_hr_data<span class="p">))</span> </pre></div> <table border="1" class="tosf docutils"> <colgroup> <col width="22%" /> <col width="13%" /> <col width="13%" /> <col width="13%" /> <col width="13%" /> <col width="14%" /> <col width="14%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">dt_rounded</th> <th class="head">track_id</th> <th class="head">type</th> <th class="head">min_temp</th> <th class="head">max_temp</th> <th class="head">rhythm_hr</th> <th class="head">fitbit_hr</th> </tr> </thead> <tbody valign="top"> <tr><td>2015-10-06 06:18:00</td> <td>3399</td> <td>Bicycling</td> <td>17.8</td> <td>35</td> <td>103.00000</td> <td>100.6</td> </tr> <tr><td>2015-10-06 06:19:00</td> <td>3399</td> <td>Bicycling</td> <td>17.8</td> <td>35</td> <td>101.50000</td> <td>94.1</td> </tr> <tr><td>2015-10-06 06:20:00</td> <td>3399</td> <td>Bicycling</td> <td>17.8</td> <td>35</td> <td>88.57143</td> <td>97.1</td> </tr> <tr><td>2015-10-06 06:21:00</td> <td>3399</td> <td>Bicycling</td> <td>17.8</td> <td>35</td> <td>115.14286</td> <td>104.2</td> </tr> <tr><td>2015-10-06 06:22:00</td> <td>3399</td> <td>Bicycling</td> <td>17.8</td> <td>35</td> <td>133.62500</td> <td>107.4</td> </tr> <tr><td>2015-10-06 06:23:00</td> <td>3399</td> <td>Bicycling</td> <td>17.8</td> <td>35</td> <td>137.00000</td> <td>113.3</td> </tr> <tr><td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> </tr> </tbody> </table> <p>Let’s take a quick look at a few of these activities. The squiggly lines show the heart rate data from the two sensors, and the horizontal lines show the average heart rate for the activity. In both cases, the FitBit Charge HR is shown in red and the Scosche Rhythm+ is blue.</p> <div class="highlight"><pre><span class="kn">library</span><span class="p">(</span>dplyr<span class="p">)</span> <span class="kn">library</span><span class="p">(</span>tidyr<span class="p">)</span> <span class="kn">library</span><span class="p">(</span>ggplot2<span class="p">)</span> <span class="kn">library</span><span class="p">(</span>lubridate<span class="p">)</span> <span class="kn">library</span><span class="p">(</span>scales<span class="p">)</span> heart_rate <span class="o">&lt;-</span> matched_hr_data <span class="o">%&gt;%</span> transmute<span class="p">(</span>dt<span class="o">=</span>dt_rounded<span class="p">,</span> track_id<span class="o">=</span>track_id<span class="p">,</span> title<span class="o">=</span><span class="kp">paste</span><span class="p">(</span><span class="kp">strftime</span><span class="p">(</span><span class="kp">as.Date</span><span class="p">(</span>dt_rounded<span class="p">,</span> tz<span class="o">=</span><span class="s">&quot;US/Alaska&quot;</span><span class="p">),</span> <span class="s">&quot;%b-%d&quot;</span><span class="p">),</span> type<span class="p">),</span> fitbit<span class="o">=</span>fitbit_hr<span class="p">,</span> rhythm<span class="o">=</span>rhythm_hr<span class="p">)</span> <span class="o">%&gt;%</span> gather<span class="p">(</span>key<span class="o">=</span>sensor<span class="p">,</span> value<span class="o">=</span>hr<span class="p">,</span> fitbit<span class="o">:</span>rhythm<span class="p">)</span> <span class="o">%&gt;%</span> filter<span class="p">(</span>track_id <span class="o">%in%</span> <span class="kt">c</span><span class="p">(</span><span class="m">3587</span><span class="p">,</span> <span class="m">3459</span><span class="p">,</span> <span class="m">3437</span><span class="p">,</span> <span class="m">3503</span><span class="p">))</span> activity_means <span class="o">&lt;-</span> heart_rate <span class="o">%&gt;%</span> group_by<span class="p">(</span>track_id<span class="p">,</span> sensor<span class="p">)</span> <span class="o">%&gt;%</span> summarize<span class="p">(</span>hr<span class="o">=</span><span class="kp">mean</span><span class="p">(</span>hr<span class="p">))</span> facet_labels <span class="o">&lt;-</span> heart_rate <span class="o">%&gt;%</span> select<span class="p">(</span>track_id<span class="p">,</span> title<span class="p">)</span> <span class="o">%&gt;%</span> distinct<span class="p">()</span> hr_labeller <span class="o">&lt;-</span> <span class="kr">function</span><span class="p">(</span>values<span class="p">)</span> <span class="p">{</span> <span class="kp">lapply</span><span class="p">(</span>values<span class="p">,</span> FUN<span class="o">=</span><span class="kr">function</span><span class="p">(</span>x<span class="p">)</span> <span class="p">(</span>facet_labels <span class="o">%&gt;%</span> filter<span class="p">(</span>track_id<span class="o">==</span>x<span class="p">))</span><span class="o">$</span>title<span class="p">)</span> <span class="p">}</span> r <span class="o">&lt;-</span> ggplot<span class="p">(</span>data<span class="o">=</span>heart_rate<span class="p">,</span> aes<span class="p">(</span>x<span class="o">=</span>dt<span class="p">,</span> y<span class="o">=</span>hr<span class="p">,</span> colour<span class="o">=</span>sensor<span class="p">))</span> <span class="o">+</span> geom_hline<span class="p">(</span>data<span class="o">=</span>activity_means<span class="p">,</span> aes<span class="p">(</span>yintercept<span class="o">=</span>hr<span class="p">,</span> colour<span class="o">=</span>sensor<span class="p">),</span> alpha<span class="o">=</span><span class="m">0.5</span><span class="p">)</span> <span class="o">+</span> geom_line<span class="p">()</span> <span class="o">+</span> theme_bw<span class="p">()</span> <span class="o">+</span> scale_color_brewer<span class="p">(</span>name<span class="o">=</span><span class="s">&quot;Sensor&quot;</span><span class="p">,</span> breaks<span class="o">=</span><span class="kt">c</span><span class="p">(</span><span class="s">&quot;fitbit&quot;</span><span class="p">,</span> <span class="s">&quot;rhythm&quot;</span><span class="p">),</span> labels<span class="o">=</span><span class="kt">c</span><span class="p">(</span><span class="s">&quot;FitBit Charge HR&quot;</span><span class="p">,</span> <span class="s">&quot;Scosche Rhythm+&quot;</span><span class="p">),</span> palette<span class="o">=</span><span class="s">&quot;Set1&quot;</span><span class="p">)</span> <span class="o">+</span> scale_x_datetime<span class="p">(</span>name<span class="o">=</span><span class="s">&quot;Time&quot;</span><span class="p">)</span> <span class="o">+</span> theme<span class="p">(</span>axis.text.x<span class="o">=</span>element_blank<span class="p">(),</span> axis.ticks.x<span class="o">=</span>element_blank<span class="p">())</span> <span class="o">+</span> scale_y_continuous<span class="p">(</span>name<span class="o">=</span><span class="s">&quot;Heart rate (bpm)&quot;</span><span class="p">)</span> <span class="o">+</span> facet_wrap<span class="p">(</span><span class="o">~</span>track_id<span class="p">,</span> scales<span class="o">=</span><span class="s">&quot;free&quot;</span><span class="p">,</span> labeller<span class="o">=</span>hr_labeller<span class="p">,</span> ncol<span class="o">=</span><span class="m">1</span><span class="p">)</span> <span class="o">+</span> ggtitle<span class="p">(</span><span class="s">&quot;Comparison between heart rate monitors during a single activity&quot;</span><span class="p">)</span> <span class="kp">print</span><span class="p">(</span>r<span class="p">)</span> </pre></div> <div class="figure"> <object class="img-responsive" data="//media.swingleydev.com/img/blog/2016/01/two_good_two_bad.svg" type="image/svg+xml"> //media.swingleydev.com/img/blog/2016/01/two_good_two_bad.svg</object> </div> <p>You can see that for each activity type, one of the plots shows data where the two heart rate monitors track well, and one where they don’t. And when they don’t agree the FitBit is <em>wildly</em> inaccurate. When I initially got my FitBit I experimented with different positions on my arm for the device but it didn’t seem to matter, so I settled on the advice from FitBit, which is to place the band slightly higher on the wrist (two to three fingers from the wrist bone) than in normal use.</p> <p>One other pattern is evident from the two plots where the FitBit does poorly: the heart rate readings are always much lower than reality.</p> <p>A scatterplot of all the data, plotting the FitBit heart rate against the Rhythm+ shows the overall pattern.</p> <div class="highlight"><pre>q <span class="o">&lt;-</span> ggplot<span class="p">(</span>data<span class="o">=</span>matched_hr_data<span class="p">,</span> aes<span class="p">(</span>x<span class="o">=</span>rhythm_hr<span class="p">,</span> y<span class="o">=</span>fitbit_hr<span class="p">,</span> colour<span class="o">=</span>type<span class="p">))</span> <span class="o">+</span> geom_abline<span class="p">(</span>intercept<span class="o">=</span><span class="m">0</span><span class="p">,</span> slope<span class="o">=</span><span class="m">1</span><span class="p">)</span> <span class="o">+</span> geom_point<span class="p">(</span>alpha<span class="o">=</span><span class="m">0.25</span><span class="p">,</span> size<span class="o">=</span><span class="m">1</span><span class="p">)</span> <span class="o">+</span> geom_smooth<span class="p">(</span>method<span class="o">=</span><span class="s">&quot;lm&quot;</span><span class="p">,</span> inherit.aes<span class="o">=</span><span class="kc">FALSE</span><span class="p">,</span> aes<span class="p">(</span>x<span class="o">=</span>rhythm_hr<span class="p">,</span> y<span class="o">=</span>fitbit_hr<span class="p">))</span> <span class="o">+</span> theme_bw<span class="p">()</span> <span class="o">+</span> scale_x_continuous<span class="p">(</span>name<span class="o">=</span><span class="s">&quot;Scosche Rhythm+ heart rate (bpm)&quot;</span><span class="p">)</span> <span class="o">+</span> scale_y_continuous<span class="p">(</span>name<span class="o">=</span><span class="s">&quot;FitBit Charge HR heart rate (bpm)&quot;</span><span class="p">)</span> <span class="o">+</span> scale_colour_brewer<span class="p">(</span>name<span class="o">=</span><span class="s">&quot;Activity type&quot;</span><span class="p">,</span> palette<span class="o">=</span><span class="s">&quot;Set1&quot;</span><span class="p">)</span> <span class="o">+</span> ggtitle<span class="p">(</span><span class="s">&quot;Comparison between heart rate monitors during exercise&quot;</span><span class="p">)</span> <span class="kp">print</span><span class="p">(</span><span class="kp">q</span><span class="p">)</span> </pre></div> <div class="figure"> <object class="img-responsive" data="//media.swingleydev.com/img/blog/2016/01/fitbit_vs_rhythm_scatterplot.svg" type="image/svg+xml"> //media.swingleydev.com/img/blog/2016/01/fitbit_vs_rhythm_scatterplot.svg</object> </div> <p>If the FitBit device were always accurate, the points would all be distributed along the 1:1 line, which is the diagonal black line under the point cloud. The blue diagonal line shows the actual linear relationship between the FitBit and Rhythm+ data. What’s curious is that the two lines cross near 100 bpm, which means that the FitBit is underestimating heart rate when my heart is beating fast, but <em>over</em>estimates it when it’s not.</p> <p>The color of the points indicate the type of activity for each point, and you can see that most of the lower heart rate points (and overestimation by the FitBit) come from hiking activities. Is it the type of activity that triggers over- or underestimation of heart rate from the FitBit, or is is just that all the lower heart rate activities tend to be hiking?</p> <p>Another way to look at the same data is to calculate the difference between the Rhythm+ and FitBit and plot those anomalies against the actual (Rhythm+) heart rate.</p> <div class="highlight"><pre>anomaly_by_hr <span class="o">&lt;-</span> matched_hr_data <span class="o">%&gt;%</span> mutate<span class="p">(</span>anomaly<span class="o">=</span>fitbit_hr<span class="o">-</span>rhythm_hr<span class="p">)</span> <span class="o">%&gt;%</span> select<span class="p">(</span>rhythm_hr<span class="p">,</span> anomaly<span class="p">,</span> type<span class="p">)</span> q <span class="o">&lt;-</span> ggplot<span class="p">(</span>data<span class="o">=</span>anomaly_by_hr<span class="p">,</span> aes<span class="p">(</span>x<span class="o">=</span>rhythm_hr<span class="p">,</span> y<span class="o">=</span>anomaly<span class="p">,</span> colour<span class="o">=</span>type<span class="p">))</span> <span class="o">+</span> geom_abline<span class="p">(</span>intercept<span class="o">=</span><span class="m">0</span><span class="p">,</span> slope<span class="o">=</span><span class="m">0</span><span class="p">,</span> alpha<span class="o">=</span><span class="m">0.5</span><span class="p">)</span> <span class="o">+</span> geom_point<span class="p">(</span>alpha<span class="o">=</span><span class="m">0.25</span><span class="p">,</span> size<span class="o">=</span><span class="m">1</span><span class="p">)</span> <span class="o">+</span> theme_bw<span class="p">()</span> <span class="o">+</span> scale_x_continuous<span class="p">(</span>name<span class="o">=</span><span class="s">&quot;Scosche Rhythm+ heart rate (bpm)&quot;</span><span class="p">,</span> breaks<span class="o">=</span>pretty_breaks<span class="p">(</span>n<span class="o">=</span><span class="m">10</span><span class="p">))</span> <span class="o">+</span> scale_y_continuous<span class="p">(</span>name<span class="o">=</span><span class="s">&quot;Difference between FitBit Charge HR and Rhythm+ (bpm)&quot;</span><span class="p">,</span> breaks<span class="o">=</span>pretty_breaks<span class="p">(</span>n<span class="o">=</span><span class="m">10</span><span class="p">))</span> <span class="o">+</span> scale_colour_brewer<span class="p">(</span>palette<span class="o">=</span><span class="s">&quot;Set1&quot;</span><span class="p">)</span> <span class="kp">print</span><span class="p">(</span><span class="kp">q</span><span class="p">)</span> </pre></div> <div class="figure"> <object class="img-responsive" data="//media.swingleydev.com/img/blog/2016/01/fitbit_anomaly_vs_rhythm_scatterplot.svg" type="image/svg+xml"> //media.swingleydev.com/img/blog/2016/01/fitbit_anomaly_vs_rhythm_scatterplot.svg</object> </div> <p>In this case, all the points should be distributed along the zero line (no difference between FitBit and Rhythm+). We can see a large bluish (fat biking) cloud around the line between 130 and 165 bpm (indicating good results from the FitBit), but the rest of the points appear to be well distributed along a diagonal line which crosses the zero line around 90 bpm. It’s another way of saying the same thing: at lower heart rates the FitBit tends to overestimate heart rate, and as my heart rate rises above 90 beats per minute, the FitBit underestimates heart rate to a greater and greater extent.</p> </div> <div class="section" id="students-t-test-and-results"> <h1>Student’s t-test and results</h1> <p>A Student’s t-test can be used effectively with paired data like this to judge whether the two data sets are statistically different from one another. This routine runs a paired t-test on the data from each activity, testing the null hypothesis that the FitBit heart rate values are the same as the Rhythm+ values. I’m tacking on significance labels typical in analyses like these where one asterisk indicates the results would only happen by chance 5% of the time, two asterisks mean random data would only show this pattern 1% of the time, and three asterisks mean there’s less than a 0.1% chance of this happening by chance.</p> <p>One note: There are 116 activities, so at the 0.05 significance level, we would expect five or six of them to be different just by chance. That doesn’t mean our overall conclusions are suspect, but you do have to keep the number of tests in mind when looking at the results.</p> <div class="highlight"><pre>t_tests <span class="o">&lt;-</span> matched_hr_data <span class="o">%&gt;%</span> group_by<span class="p">(</span>track_id<span class="p">,</span> type<span class="p">,</span> min_temp<span class="p">,</span> max_temp<span class="p">)</span> <span class="o">%&gt;%</span> summarize_each<span class="p">(</span>funs<span class="p">(</span>p_value<span class="o">=</span>t.test<span class="p">(</span><span class="m">.</span><span class="p">,</span> rhythm_hr<span class="p">,</span> paired<span class="o">=</span><span class="kc">TRUE</span><span class="p">)</span><span class="o">$</span>p.value<span class="p">,</span> anomaly<span class="o">=</span>t.test<span class="p">(</span><span class="m">.</span><span class="p">,</span> rhythm_hr<span class="p">,</span> paired<span class="o">=</span><span class="kc">TRUE</span><span class="p">)</span><span class="o">$</span>estimate<span class="p">[</span><span class="m">1</span><span class="p">]),</span> vars<span class="o">=</span>fitbit_hr<span class="p">)</span> <span class="o">%&gt;%</span> ungroup<span class="p">()</span> <span class="o">%&gt;%</span> mutate<span class="p">(</span>sig<span class="o">=</span><span class="kp">ifelse</span><span class="p">(</span>p_value<span class="o">&lt;</span><span class="m">0.001</span><span class="p">,</span> <span class="s">&#39;***&#39;</span><span class="p">,</span> <span class="kp">ifelse</span><span class="p">(</span>p_value<span class="o">&lt;</span><span class="m">0.01</span><span class="p">,</span> <span class="s">&#39;**&#39;</span><span class="p">,</span> <span class="kp">ifelse</span><span class="p">(</span>p_value<span class="o">&lt;</span><span class="m">0.05</span><span class="p">,</span> <span class="s">&#39;*&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">))))</span> <span class="o">%&gt;%</span> select<span class="p">(</span>track_id<span class="p">,</span> type<span class="p">,</span> min_temp<span class="p">,</span> max_temp<span class="p">,</span> anomaly<span class="p">,</span> p_value<span class="p">,</span> sig<span class="p">)</span> kable<span class="p">(</span><span class="kp">head</span><span class="p">(</span>t_tests<span class="p">))</span> </pre></div> <table border="1" class="tosf docutils"> <colgroup> <col width="14%" /> <col width="14%" /> <col width="14%" /> <col width="14%" /> <col width="14%" /> <col width="14%" /> <col width="14%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">track_id</th> <th class="head">type</th> <th class="head">min_temp</th> <th class="head">max_temp</th> <th class="head">anomaly</th> <th class="head">p_value</th> <th class="head">sig</th> </tr> </thead> <tbody valign="top"> <tr><td>3399</td> <td>Bicycling</td> <td>17.8</td> <td>35.0</td> <td>-27.766016</td> <td>0.0000000</td> <td>***</td> </tr> <tr><td>3401</td> <td>Bicycling</td> <td>37.0</td> <td>46.6</td> <td>-12.464228</td> <td>0.0010650</td> <td>**</td> </tr> <tr><td>3403</td> <td>Bicycling</td> <td>15.8</td> <td>38.0</td> <td>-4.714672</td> <td>0.0000120</td> <td>***</td> </tr> <tr><td>3405</td> <td>Bicycling</td> <td>42.4</td> <td>44.3</td> <td>-1.652476</td> <td>0.1059867</td> <td>&nbsp;</td> </tr> <tr><td>3407</td> <td>Bicycling</td> <td>23.3</td> <td>40.0</td> <td>-7.142151</td> <td>0.0000377</td> <td>***</td> </tr> <tr><td>3409</td> <td>Bicycling</td> <td>44.6</td> <td>45.5</td> <td>-3.441501</td> <td>0.0439596</td> <td>*</td> </tr> <tr><td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> <td>...</td> </tr> </tbody> </table> <p>It’s easier to interpret the results summarized by activity type:</p> <div class="highlight"><pre>t_test_summary <span class="o">&lt;-</span> t_tests <span class="o">%&gt;%</span> mutate<span class="p">(</span>different<span class="o">=</span><span class="kp">grepl</span><span class="p">(</span><span class="s">&#39;\\*&#39;</span><span class="p">,</span> sig<span class="p">))</span> <span class="o">%&gt;%</span> select<span class="p">(</span>type<span class="p">,</span> anomaly<span class="p">,</span> different<span class="p">)</span> <span class="o">%&gt;%</span> group_by<span class="p">(</span>type<span class="p">,</span> different<span class="p">)</span> <span class="o">%&gt;%</span> summarize<span class="p">(</span>n<span class="o">=</span>n<span class="p">(),</span> mean_anomaly<span class="o">=</span><span class="kp">mean</span><span class="p">(</span>anomaly<span class="p">))</span> kable<span class="p">(</span>t_test_summary<span class="p">)</span> </pre></div> <table border="1" class="tosf docutils"> <colgroup> <col width="28%" /> <col width="26%" /> <col width="12%" /> <col width="34%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">type</th> <th class="head">different</th> <th class="head">n</th> <th class="head">mean_anomaly</th> </tr> </thead> <tbody valign="top"> <tr><td>Bicycling</td> <td>FALSE</td> <td>2</td> <td>-1.169444</td> </tr> <tr><td>Bicycling</td> <td>TRUE</td> <td>26</td> <td>-20.847136</td> </tr> <tr><td>Fat Biking</td> <td>FALSE</td> <td>15</td> <td>-1.128833</td> </tr> <tr><td>Fat Biking</td> <td>TRUE</td> <td>58</td> <td>-14.958953</td> </tr> <tr><td>Hiking</td> <td>FALSE</td> <td>2</td> <td>-0.691730</td> </tr> <tr><td>Hiking</td> <td>TRUE</td> <td>8</td> <td>10.947165</td> </tr> <tr><td>Skiing</td> <td>TRUE</td> <td>5</td> <td>-28.710941</td> </tr> </tbody> </table> <p>What this shows is that the FitBit underestimated heart rate by an average of 21 beats per minute in 26 of 28 (93%) bicycling trips, underestimated heart rate by an average of 15 bpm in 58 of 73 (79%) fat biking trips, overestimate heart rate by an average of 11 bpm in 80% of hiking trips, and always drastically underestimated my heart rate while skiing.</p> <p>For all the data:</p> <div class="highlight"><pre>t.test<span class="p">(</span>matched_hr_data<span class="o">$</span>fitbit_hr<span class="p">,</span> matched_hr_data<span class="o">$</span>rhythm_hr<span class="p">,</span> paired<span class="o">=</span><span class="kc">TRUE</span><span class="p">)</span> </pre></div> <pre class="literal-block"> ## ## Paired t-test ## ## data: matched_hr_data$fitbit_hr and matched_hr_data$rhythm_hr ## t = -38.6232, df = 4461, p-value &lt; 2.2e-16 ## alternative hypothesis: true difference in means is not equal to 0 ## 95 percent confidence interval: ## -13.02931 -11.77048 ## sample estimates: ## mean of the differences ## -12.3999 </pre> <p>Indeed, in aggregate, the FitBit does a poor job at estimating heart rate during exercise.</p> </div> <div class="section" id="conclusion"> <h1>Conclusion</h1> <p>Based on my data of more than 100 activities, I’d say the lawsuit has some merit. I only get accurate heart rate readings during exercise from my FitBit Charge HR about 16% of the time, and the error in the heart rate estimates appears to get worse as my actual heart rate increases. The advertising for these devices gives you the impression that they’re designed for high intensity exercise (showing people being very active, running, bicycling, etc.), but their performance during these activities is pretty poor.</p> <p>All that said, I knew this going in when I bought my FitBit, so I’m not hugely disappointed. There are plenty of other benefits to monitoring the data from these devices (including non-exercise heart rate), and it isn’t a major inconvenience for me to strap on a more accurate heart rate monitor for those times when it actually matters.</p> </div> </div> Sat, 09 Jan 2016 15:00:45 -0900 http://swingleydev.com/blog/p/1992/ heart rate exercise FitBit Temperature effects on fat biking, part 2 http://swingleydev.com/blog/p/1991/ <div class="document"> <div class="section" id="introduction"> <h1>Introduction</h1> <p>While riding to work this morning I figured out a way to disentangle the effects of trail quality and physical conditioning (both of which improve over the season) from temperature, which also tends to increase throughout the season. As you recall in my <a class="reference external" href="https://swingleydev.com/blog/p/1990/">previous post</a>, I found that days into the season (winter day of year) and minimum temperature were both negatively related with fat bike energy consumption. But because those variables are also related to each other, we can’t make statements about them individually.</p> <p>But what if we look at pairs of trips that are within two days of each other and look at the difference in temperature between those trips and the difference in energy consumption? We’ll only pair trips going the same direction (to or from work), and we’ll restrict the pairings to two days or less. That eliminates seasonality from the data because we’re always comparing two trips from the same few days.</p> </div> <div class="section" id="data"> <h1>Data</h1> <p>For this analysis, I’m using SQL to filter the data because I’m better at window functions and filtering in SQL than R. Here’s the code to grab the data from the database. (The CSV file and RMarkdown script is on my <a class="reference external" href="https://github.com/cswingle/fat_biking">GitHub repo</a> for this analysis). The trick here is to categorize trips as being to work (“north”) or from work (“south”) and then include this field in the partition statement of the window function so I’m only getting the next trip that matches direction.</p> <div class="highlight"><pre><span class="kn">library</span><span class="p">(</span>dplyr<span class="p">)</span> <span class="kn">library</span><span class="p">(</span>ggplot2<span class="p">)</span> <span class="kn">library</span><span class="p">(</span>scales<span class="p">)</span> exercise_db <span class="o">&lt;-</span> src_postgres<span class="p">(</span>host<span class="o">=</span><span class="s">&quot;example.com&quot;</span><span class="p">,</span> dbname<span class="o">=</span><span class="s">&quot;exercise_data&quot;</span><span class="p">)</span> diffs <span class="o">&lt;-</span> tbl<span class="p">(</span>exercise_db<span class="p">,</span> build_sql<span class="p">(</span> <span class="s">&quot;WITH all_to_work AS (</span> <span class="s"> SELECT *,</span> <span class="s"> CASE WHEN extract(hour from start_time) &lt; 11</span> <span class="s"> THEN &#39;north&#39; ELSE &#39;south&#39; END AS direction</span> <span class="s"> FROM track_stats</span> <span class="s"> WHERE type = &#39;Fat Biking&#39;</span> <span class="s"> AND miles between 4 and 4.3</span> <span class="s"> ), with_next AS (</span> <span class="s"> SELECT track_id, start_time, direction, kcal, miles, min_temp,</span> <span class="s"> lead(direction) OVER w AS next_direction,</span> <span class="s"> lead(start_time) OVER w AS next_start_time,</span> <span class="s"> lead(kcal) OVER w AS next_kcal,</span> <span class="s"> lead(miles) OVER w AS next_miles,</span> <span class="s"> lead(min_temp) OVER w AS next_min_temp</span> <span class="s"> FROM all_to_work</span> <span class="s"> WINDOW w AS (PARTITION BY direction ORDER BY start_time)</span> <span class="s"> )</span> <span class="s"> SELECT start_time, next_start_time, direction,</span> <span class="s"> min_temp, next_min_temp,</span> <span class="s"> kcal / miles AS kcal_per_mile,</span> <span class="s"> next_kcal / next_miles as next_kcal_per_mile,</span> <span class="s"> next_min_temp - min_temp AS temp_diff,</span> <span class="s"> (next_kcal / next_miles) - (kcal / miles) AS kcal_per_mile_diff</span> <span class="s"> FROM with_next</span> <span class="s"> WHERE next_start_time - start_time &lt; &#39;60 hours&#39;</span> <span class="s"> ORDER BY start_time&quot;</span><span class="p">))</span> <span class="o">%&gt;%</span> collect<span class="p">()</span> write.csv<span class="p">(</span>diffs<span class="p">,</span> file<span class="o">=</span><span class="s">&quot;fat_biking_trip_diffs.csv&quot;</span><span class="p">,</span> quote<span class="o">=</span><span class="kc">TRUE</span><span class="p">,</span> row.names<span class="o">=</span><span class="kc">FALSE</span><span class="p">)</span> kable<span class="p">(</span><span class="kp">head</span><span class="p">(</span>diffs<span class="p">))</span> </pre></div> <table border="1" class="docutils"> <colgroup> <col width="29%" /> <col width="29%" /> <col width="17%" /> <col width="24%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">start time</th> <th class="head">next start time</th> <th class="head">temp diff</th> <th class="head">kcal / mile diff</th> </tr> </thead> <tbody valign="top"> <tr><td>2013-12-03 06:21:49</td> <td>2013-12-05 06:31:54</td> <td>3.0</td> <td>-13.843866</td> </tr> <tr><td>2013-12-03 15:41:48</td> <td>2013-12-05 15:24:10</td> <td>3.7</td> <td>-8.823329</td> </tr> <tr><td>2013-12-05 06:31:54</td> <td>2013-12-06 06:39:04</td> <td>23.4</td> <td>-22.510564</td> </tr> <tr><td>2013-12-05 15:24:10</td> <td>2013-12-06 16:38:31</td> <td>13.6</td> <td>-5.505662</td> </tr> <tr><td>2013-12-09 06:41:07</td> <td>2013-12-11 06:15:32</td> <td>-27.7</td> <td>-10.227048</td> </tr> <tr><td>2013-12-09 13:44:59</td> <td>2013-12-11 16:00:11</td> <td>-25.4</td> <td>-1.034789</td> </tr> </tbody> </table> <p>Out of a total of 123 trips, 70 took place within 2 days of each other. We still don’t have a measure of trail quality, so pairs where the trail is smooth and hard one day and covered with fresh snow the next won’t be particularly good data points.</p> <p>Let’s look at a plot of the data.</p> <div class="highlight"><pre>s <span class="o">=</span> ggplot<span class="p">(</span>data<span class="o">=</span>diffs<span class="p">,</span> aes<span class="p">(</span>x<span class="o">=</span>temp_diff<span class="p">,</span> y<span class="o">=</span>kcal_per_mile_diff<span class="p">))</span> <span class="o">+</span> geom_point<span class="p">()</span> <span class="o">+</span> geom_smooth<span class="p">(</span>method<span class="o">=</span><span class="s">&quot;lm&quot;</span><span class="p">,</span> se<span class="o">=</span><span class="kc">FALSE</span><span class="p">)</span> <span class="o">+</span> scale_x_continuous<span class="p">(</span>name<span class="o">=</span><span class="s">&quot;Temperature difference between paired trips (degrees F)&quot;</span><span class="p">,</span> breaks<span class="o">=</span>pretty_breaks<span class="p">(</span>n<span class="o">=</span><span class="m">10</span><span class="p">))</span> <span class="o">+</span> scale_y_continuous<span class="p">(</span>name<span class="o">=</span><span class="s">&quot;Energy consumption difference (kcal / mile)&quot;</span><span class="p">,</span> breaks<span class="o">=</span>pretty_breaks<span class="p">(</span>n<span class="o">=</span><span class="m">10</span><span class="p">))</span> <span class="o">+</span> theme_bw<span class="p">()</span> <span class="o">+</span> ggtitle<span class="p">(</span><span class="s">&quot;Paired fat bike trips to and from work within 2 days of each other&quot;</span><span class="p">)</span> <span class="kp">print</span><span class="p">(</span>s<span class="p">)</span> </pre></div> <div class="figure"> <object class="img-responsive" data="//media.swingleydev.com/img/blog/2015/12/paired_trip_plot.svg" type="image/svg+xml"> //media.swingleydev.com/img/blog/2015/12/paired_trip_plot.svg</object> </div> <p>This shows that when the temperature difference between two paired trips is negative (the second trip is colder than the first), additional energy is required for the second (colder) trip. This matches the pattern we saw in my earlier post where minimum temperature and winter day of year were negatively associated with energy consumption. But because we’ve used differences to remove seasonal effects, we can actually determine how large of an effect temperature has.</p> <p>There are quite a few outliers here. Those that are in the region with very little difference in temperature are likey due to snowfall changing the trail conditions from one trip to the next. I’m not sure why there is so much scatter among the points on the left side of the graph, but I don’t see any particular pattern among those points that might explain the higher than normal variation, and we don’t see the same variation in the points with a large positive difference in temperature, so I think this is just normal variation in the data not explained by temperature.</p> <div class="section" id="results"> <h2>Results</h2> <p>Here’s the linear regression results for this data.</p> <div class="highlight"><pre><span class="kp">summary</span><span class="p">(</span>lm<span class="p">(</span>data<span class="o">=</span>diffs<span class="p">,</span> kcal_per_mile_diff <span class="o">~</span> temp_diff<span class="p">))</span> </pre></div> <pre class="literal-block"> ## ## Call: ## lm(formula = kcal_per_mile_diff ~ temp_diff, data = diffs) ## ## Residuals: ## Min 1Q Median 3Q Max ## -40.839 -4.584 -0.169 3.740 47.063 ## ## Coefficients: ## Estimate Std. Error t value Pr(&gt;|t|) ## (Intercept) -2.1696 1.5253 -1.422 0.159 ## temp_diff -0.7778 0.1434 -5.424 8.37e-07 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 12.76 on 68 degrees of freedom ## Multiple R-squared: 0.302, Adjusted R-squared: 0.2917 ## F-statistic: 29.42 on 1 and 68 DF, p-value: 8.367e-07 </pre> <p>The model and coefficient are both highly signficant, and as we might expect, the intercept in the model is not significantly different from zero (if there wasn’t a difference in temperature between two trips there shouldn’t be a difference in energy consumption either, on average). Temperature alone explains 30% of the variation in energy consumption, and the coefficient tells us the scale of the effect: each degree drop in temperature results in an increase in energy consumption of 0.78 kcalories per mile. So for a 4 mile commute like mine, the difference between a trip at 10°F vs −20°F is an additional 93 kilocalories (30 × 0.7778 × 4 = 93.34) on the colder trip. That might not sound like much in the context of the calories in food (93 kilocalories is about the energy in a large orange or a light beer), but my average energy consumption across all fat bike trips to and from work is 377 kilocalories so 93 represents a large portion of the total.</p> </div> </div> </div> Mon, 21 Dec 2015 16:58:53 -0900 http://swingleydev.com/blog/p/1991/ exercise fat bike R temperature Temperature effects on fat biking http://swingleydev.com/blog/p/1990/ <div class="document"> <div class="section" id="introduction"> <h1>Introduction</h1> <p>I’ve had a fat bike since late November 2013, mostly using it to commute the 4.1 miles to and from work on the Goldstream Valley trail system. I used to classic ski exclusively, but that’s not particularly pleasant once the temperatures are below 0°F because I can’t keep my hands and feet warm enough, and the amount of glide you get on skis declines as the temperature goes down.</p> <p>However, it’s also true that fat biking gets much harder the colder it gets. I think this is partly due to biking while wearing lots of extra layers, but also because of increased friction between the large tires and tubes in a fat bike. In this post I will look at how temperature and other variables affect the performance of a fat bike (and it’s rider).</p> <p>The code and data for this post is available on <a class="reference external" href="https://github.com/cswingle/fat_biking">GitHub</a>.</p> </div> <div class="section" id="data"> <h1>Data</h1> <p>I log all my commutes (and other exercise) using the <a class="reference external" href="http://runkeeper.com">RunKeeper</a> app, which uses the phone’s GPS to keep track of distance and speed, and connects to my heart rate monitor to track heart rate. I had been using a Polar HR chest strap, but after about a year it became flaky and I replaced it with a Scosche Rhythm+ arm band monitor. The data from RunKeeper is exported into GPX files, which I process and insert into a PostgreSQL database.</p> <p>From the heart rate data, I estimate energy consumption (in kilocalories, or what appears on food labels as calories) using a formula from Keytel LR, et al. 2005, which I talk about in this <a class="reference external" href="http://swingleydev.com/blog/p/1970/">blog post</a>.</p> <p>Let’s take a look at the data:</p> <div class="highlight"><pre><span class="kn">library</span><span class="p">(</span>dplyr<span class="p">)</span> <span class="kn">library</span><span class="p">(</span>ggplot2<span class="p">)</span> <span class="kn">library</span><span class="p">(</span>scales<span class="p">)</span> <span class="kn">library</span><span class="p">(</span>lubridate<span class="p">)</span> <span class="kn">library</span><span class="p">(</span>munsell<span class="p">)</span> fat_bike <span class="o">&lt;-</span> read.csv<span class="p">(</span><span class="s">&quot;fat_bike.csv&quot;</span><span class="p">,</span> stringsAsFactors<span class="o">=</span><span class="kc">FALSE</span><span class="p">,</span> header<span class="o">=</span><span class="kc">TRUE</span><span class="p">)</span> <span class="o">%&gt;%</span> tbl_df<span class="p">()</span> <span class="o">%&gt;%</span> mutate<span class="p">(</span>start_time<span class="o">=</span>ymd_hms<span class="p">(</span>start_time<span class="p">,</span> tz<span class="o">=</span><span class="s">&quot;US/Alaska&quot;</span><span class="p">))</span> kable<span class="p">(</span><span class="kp">head</span><span class="p">(</span>fat_bike<span class="p">))</span> </pre></div> <table border="1" class="docutils"> <colgroup> <col width="22%" /> <col width="9%" /> <col width="11%" /> <col width="9%" /> <col width="8%" /> <col width="9%" /> <col width="9%" /> <col width="13%" /> <col width="13%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">start_time</th> <th class="head">miles</th> <th class="head">time</th> <th class="head">hours</th> <th class="head">mph</th> <th class="head">hr</th> <th class="head">kcal</th> <th class="head">min_temp</th> <th class="head">max_temp</th> </tr> </thead> <tbody valign="top"> <tr><td>2013-11-27 06:22:13</td> <td>4.17</td> <td>0:35:11</td> <td>0.59</td> <td>7.12</td> <td>157.8</td> <td>518.4</td> <td>-1.1</td> <td>1.0</td> </tr> <tr><td>2013-11-27 15:27:01</td> <td>4.11</td> <td>0:35:49</td> <td>0.60</td> <td>6.89</td> <td>156.0</td> <td>513.6</td> <td>1.1</td> <td>2.2</td> </tr> <tr><td>2013-12-01 12:29:27</td> <td>4.79</td> <td>0:55:08</td> <td>0.92</td> <td>5.21</td> <td>172.6</td> <td>951.5</td> <td>-25.9</td> <td>-23.9</td> </tr> <tr><td>2013-12-03 06:21:49</td> <td>4.19</td> <td>0:39:16</td> <td>0.65</td> <td>6.40</td> <td>148.4</td> <td>526.8</td> <td>-4.6</td> <td>-2.1</td> </tr> <tr><td>2013-12-03 15:41:48</td> <td>4.22</td> <td>0:30:56</td> <td>0.52</td> <td>8.19</td> <td>154.6</td> <td>434.5</td> <td>6.0</td> <td>7.9</td> </tr> <tr><td>2013-12-05 06:31:54</td> <td>4.14</td> <td>0:32:14</td> <td>0.54</td> <td>7.71</td> <td>155.8</td> <td>463.2</td> <td>-1.6</td> <td>2.9</td> </tr> </tbody> </table> <p>There are a few things we need to do to the raw data before analyzing it. First, we want to restrict the data to just my commutes to and from work, and we want to categorize them as being one or the other. That way we can analyze trips to ABR and home separately, and we’ll reduce the variation within each analysis. If we were to analyze all fat biking trips together, we’d be lumping short and long trips, as well as those with a different proportion of hills or more challenging conditions. To get just trips to and from work, I’m restricting the distance to trips between 4.0 and 4.3 miles, and only those activities where there were two of them in a single day (to work and home from work). To categorize them into commutes to work and home, I filter based on the time of day.</p> <p>I’m also calculating energy per mile, and adding a “winter day of year” variable (<tt class="docutils literal">wdoy</tt>), which is a measure of how far into the winter season the trip took place. We can’t just use day of year because that starts over on January 1st, so we subtract the number of days between January and May from the date and get day of year from that. Finally, we split the data into trips to work and home.</p> <p>I’m also excluding the really early season data from 2015 because the trail was in really poor condition.</p> <div class="highlight"><pre>fat_bike_commute <span class="o">&lt;-</span> fat_bike <span class="o">%&gt;%</span> filter<span class="p">(</span>miles<span class="o">&gt;</span><span class="m">4</span><span class="p">,</span> miles<span class="o">&lt;</span><span class="m">4.3</span><span class="p">)</span> <span class="o">%&gt;%</span> mutate<span class="p">(</span>direction<span class="o">=</span><span class="kp">ifelse</span><span class="p">(</span>hour<span class="p">(</span>start_time<span class="p">)</span><span class="o">&lt;</span><span class="m">10</span><span class="p">,</span> <span class="s">&#39;north&#39;</span><span class="p">,</span> <span class="s">&#39;south&#39;</span><span class="p">),</span> date<span class="o">=</span><span class="kp">as.Date</span><span class="p">(</span>start_time<span class="p">,</span> tz<span class="o">=</span><span class="s">&#39;US/Alaska&#39;</span><span class="p">),</span> wdoy<span class="o">=</span>yday<span class="p">(</span><span class="kp">date</span><span class="o">-</span>days<span class="p">(</span><span class="m">120</span><span class="p">)),</span> kcal_per_mile<span class="o">=</span>kcal<span class="o">/</span>miles<span class="p">)</span> <span class="o">%&gt;%</span> group_by<span class="p">(</span><span class="kp">date</span><span class="p">)</span> <span class="o">%&gt;%</span> mutate<span class="p">(</span>n<span class="o">=</span>n<span class="p">())</span> <span class="o">%&gt;%</span> ungroup<span class="p">()</span> <span class="o">%&gt;%</span> filter<span class="p">(</span>n<span class="o">&gt;</span><span class="m">1</span><span class="p">)</span> to_abr <span class="o">&lt;-</span> fat_bike_commute <span class="o">%&gt;%</span> filter<span class="p">(</span>direction<span class="o">==</span><span class="s">&#39;north&#39;</span><span class="p">,</span> wdoy<span class="o">&gt;</span><span class="m">210</span><span class="p">)</span> to_home <span class="o">&lt;-</span> fat_bike_commute <span class="o">%&gt;%</span> filter<span class="p">(</span>direction<span class="o">==</span><span class="s">&#39;south&#39;</span><span class="p">,</span> wdoy<span class="o">&gt;</span><span class="m">210</span><span class="p">)</span> kable<span class="p">(</span><span class="kp">head</span><span class="p">(</span>to_home <span class="o">%&gt;%</span> select<span class="p">(</span><span class="o">-</span><span class="kp">date</span><span class="p">,</span> <span class="o">-</span>kcal<span class="p">,</span> <span class="o">-</span>n<span class="p">)))</span> </pre></div> <table border="1" class="docutils"> <colgroup> <col width="17%" /> <col width="7%" /> <col width="8%" /> <col width="7%" /> <col width="6%" /> <col width="7%" /> <col width="10%" /> <col width="10%" /> <col width="10%" /> <col width="6%" /> <col width="14%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">start_time</th> <th class="head">miles</th> <th class="head">time</th> <th class="head">hours</th> <th class="head">mph</th> <th class="head">hr</th> <th class="head">min_temp</th> <th class="head">max_temp</th> <th class="head">direction</th> <th class="head">wdoy</th> <th class="head">kcal_per_mile</th> </tr> </thead> <tbody valign="top"> <tr><td>2013-11-27 15:27:01</td> <td>4.11</td> <td>0:35:49</td> <td>0.60</td> <td>6.89</td> <td>156.0</td> <td>1.1</td> <td>2.2</td> <td>south</td> <td>211</td> <td>124.96350</td> </tr> <tr><td>2013-12-03 15:41:48</td> <td>4.22</td> <td>0:30:56</td> <td>0.52</td> <td>8.19</td> <td>154.6</td> <td>6.0</td> <td>7.9</td> <td>south</td> <td>217</td> <td>102.96209</td> </tr> <tr><td>2013-12-05 15:24:10</td> <td>4.18</td> <td>0:29:07</td> <td>0.49</td> <td>8.60</td> <td>150.7</td> <td>9.7</td> <td>12.0</td> <td>south</td> <td>219</td> <td>94.13876</td> </tr> <tr><td>2013-12-06 16:38:31</td> <td>4.17</td> <td>0:26:04</td> <td>0.43</td> <td>9.60</td> <td>154.3</td> <td>23.3</td> <td>24.7</td> <td>south</td> <td>220</td> <td>88.63309</td> </tr> <tr><td>2013-12-09 13:44:59</td> <td>4.11</td> <td>0:32:06</td> <td>0.54</td> <td>7.69</td> <td>161.3</td> <td>27.5</td> <td>28.5</td> <td>south</td> <td>223</td> <td>119.19708</td> </tr> <tr><td>2013-12-11 16:00:11</td> <td>4.19</td> <td>0:33:48</td> <td>0.56</td> <td>7.44</td> <td>157.6</td> <td>2.1</td> <td>4.5</td> <td>south</td> <td>225</td> <td>118.16229</td> </tr> </tbody> </table> </div> <div class="section" id="analysis"> <h1>Analysis</h1> <p>Here a plot of the data. We’re plotting all trips with winter day of year on the x-axis and energy per mile on the y-axis. The color of the points indicates the minimum temperature and the straight line shows the trend of the relationship.</p> <div class="highlight"><pre>s <span class="o">&lt;-</span> ggplot<span class="p">(</span>data<span class="o">=</span>fat_bike_commute <span class="o">%&gt;%</span> filter<span class="p">(</span>wdoy<span class="o">&gt;</span><span class="m">210</span><span class="p">),</span> aes<span class="p">(</span>x<span class="o">=</span>wdoy<span class="p">,</span> y<span class="o">=</span>kcal_per_mile<span class="p">,</span> colour<span class="o">=</span>min_temp<span class="p">))</span> <span class="o">+</span> geom_smooth<span class="p">(</span>method<span class="o">=</span><span class="s">&quot;lm&quot;</span><span class="p">,</span> se<span class="o">=</span><span class="kc">FALSE</span><span class="p">,</span> colour<span class="o">=</span>mnsl<span class="p">(</span><span class="s">&quot;10B 7/10&quot;</span><span class="p">,</span> fix<span class="o">=</span><span class="kc">TRUE</span><span class="p">))</span> <span class="o">+</span> geom_point<span class="p">(</span>size<span class="o">=</span><span class="m">3</span><span class="p">)</span> <span class="o">+</span> scale_x_continuous<span class="p">(</span>name<span class="o">=</span><span class="kc">NULL</span><span class="p">,</span> breaks<span class="o">=</span><span class="kt">c</span><span class="p">(</span><span class="m">215</span><span class="p">,</span> <span class="m">246</span><span class="p">,</span> <span class="m">277</span><span class="p">,</span> <span class="m">305</span><span class="p">,</span> <span class="m">336</span><span class="p">),</span> labels<span class="o">=</span><span class="kt">c</span><span class="p">(</span><span class="s">&#39;1-Dec&#39;</span><span class="p">,</span> <span class="s">&#39;1-Jan&#39;</span><span class="p">,</span> <span class="s">&#39;1-Feb&#39;</span><span class="p">,</span> <span class="s">&#39;1-Mar&#39;</span><span class="p">,</span> <span class="s">&#39;1-Apr&#39;</span><span class="p">))</span> <span class="o">+</span> scale_y_continuous<span class="p">(</span>name<span class="o">=</span><span class="s">&quot;Energy (kcal)&quot;</span><span class="p">,</span> breaks<span class="o">=</span>pretty_breaks<span class="p">(</span>n<span class="o">=</span><span class="m">10</span><span class="p">))</span> <span class="o">+</span> scale_colour_continuous<span class="p">(</span>low<span class="o">=</span>mnsl<span class="p">(</span><span class="s">&quot;7.5B 5/12&quot;</span><span class="p">,</span> fix<span class="o">=</span><span class="kc">TRUE</span><span class="p">),</span> high<span class="o">=</span>mnsl<span class="p">(</span><span class="s">&quot;7.5R 5/12&quot;</span><span class="p">,</span> fix<span class="o">=</span><span class="kc">TRUE</span><span class="p">),</span> breaks<span class="o">=</span>pretty_breaks<span class="p">(</span>n<span class="o">=</span><span class="m">5</span><span class="p">),</span> guide<span class="o">=</span>guide_colourbar<span class="p">(</span>title<span class="o">=</span><span class="s">&quot;Min temp (°F)&quot;</span><span class="p">,</span> reverse<span class="o">=</span><span class="kc">FALSE</span><span class="p">,</span> barheight<span class="o">=</span><span class="m">8</span><span class="p">))</span> <span class="o">+</span> ggtitle<span class="p">(</span><span class="s">&quot;All fat bike trips&quot;</span><span class="p">)</span> <span class="o">+</span> theme_bw<span class="p">()</span> <span class="kp">print</span><span class="p">(</span>s<span class="p">)</span> </pre></div> <div class="figure"> <object class="img-responsive" data="//media.swingleydev.com/img/blog/2015/12/wdoy_plot.svg" type="image/svg+xml"> //media.swingleydev.com/img/blog/2015/12/wdoy_plot.svg</object> </div> <p>Across all trips, we can see that as the winter progresses, I consume less energy per mile. This is hopefully because my physical condition improves the more I ride, and also because the trail conditions also improve as the snow pack develops and the trail gets harder with use. You can also see a pattern in the color of the dots, with the bluer (and colder) points near the top and the warmer temperature trips near the bottom.</p> <p>Let’s look at the temperature relationship:</p> <div class="highlight"><pre>s <span class="o">&lt;-</span> ggplot<span class="p">(</span>data<span class="o">=</span>fat_bike_commute <span class="o">%&gt;%</span> filter<span class="p">(</span>wdoy<span class="o">&gt;</span><span class="m">210</span><span class="p">),</span> aes<span class="p">(</span>x<span class="o">=</span>min_temp<span class="p">,</span> y<span class="o">=</span>kcal_per_mile<span class="p">,</span> colour<span class="o">=</span>wdoy<span class="p">))</span> <span class="o">+</span> geom_smooth<span class="p">(</span>method<span class="o">=</span><span class="s">&quot;lm&quot;</span><span class="p">,</span> se<span class="o">=</span><span class="kc">FALSE</span><span class="p">,</span> colour<span class="o">=</span>mnsl<span class="p">(</span><span class="s">&quot;10B 7/10&quot;</span><span class="p">,</span> fix<span class="o">=</span><span class="kc">TRUE</span><span class="p">))</span> <span class="o">+</span> geom_point<span class="p">(</span>size<span class="o">=</span><span class="m">3</span><span class="p">)</span> <span class="o">+</span> scale_x_continuous<span class="p">(</span>name<span class="o">=</span><span class="s">&quot;Minimum temperature (degrees F)&quot;</span><span class="p">,</span> breaks<span class="o">=</span>pretty_breaks<span class="p">(</span>n<span class="o">=</span><span class="m">10</span><span class="p">))</span> <span class="o">+</span> scale_y_continuous<span class="p">(</span>name<span class="o">=</span><span class="s">&quot;Energy (kcal)&quot;</span><span class="p">,</span> breaks<span class="o">=</span>pretty_breaks<span class="p">(</span>n<span class="o">=</span><span class="m">10</span><span class="p">))</span> <span class="o">+</span> scale_colour_continuous<span class="p">(</span>low<span class="o">=</span>mnsl<span class="p">(</span><span class="s">&quot;7.5PB 2/12&quot;</span><span class="p">,</span> fix<span class="o">=</span><span class="kc">TRUE</span><span class="p">),</span> high<span class="o">=</span>mnsl<span class="p">(</span><span class="s">&quot;7.5PB 8/12&quot;</span><span class="p">,</span> fix<span class="o">=</span><span class="kc">TRUE</span><span class="p">),</span> breaks<span class="o">=</span><span class="kt">c</span><span class="p">(</span><span class="m">215</span><span class="p">,</span> <span class="m">246</span><span class="p">,</span> <span class="m">277</span><span class="p">,</span> <span class="m">305</span><span class="p">,</span> <span class="m">336</span><span class="p">),</span> labels<span class="o">=</span><span class="kt">c</span><span class="p">(</span><span class="s">&#39;1-Dec&#39;</span><span class="p">,</span> <span class="s">&#39;1-Jan&#39;</span><span class="p">,</span> <span class="s">&#39;1-Feb&#39;</span><span class="p">,</span> <span class="s">&#39;1-Mar&#39;</span><span class="p">,</span> <span class="s">&#39;1-Apr&#39;</span><span class="p">),</span> guide<span class="o">=</span>guide_colourbar<span class="p">(</span>title<span class="o">=</span><span class="kc">NULL</span><span class="p">,</span> reverse<span class="o">=</span><span class="kc">TRUE</span><span class="p">,</span> barheight<span class="o">=</span><span class="m">8</span><span class="p">))</span> <span class="o">+</span> ggtitle<span class="p">(</span><span class="s">&quot;All fat bike trips&quot;</span><span class="p">)</span> <span class="o">+</span> theme_bw<span class="p">()</span> <span class="kp">print</span><span class="p">(</span>s<span class="p">)</span> </pre></div> <div class="figure"> <object class="img-responsive" data="//media.swingleydev.com/img/blog/2015/12/min_temp_plot.svg" type="image/svg+xml"> //media.swingleydev.com/img/blog/2015/12/min_temp_plot.svg</object> </div> <p>A similar pattern. As the temperature drops, it takes more energy to go the same distance. And the color of the points also shows the relationship from the earlier plot where trips taken later in the season require less energy.</p> <p>There is also be a correlation between winter day of year and temperature. Since the winter fat biking season essentially begins in December, it tends to warm up throughout.</p> </div> <div class="section" id="results"> <h1>Results</h1> <p>The relationship between winter day of year and temperature means that we’ve got multicollinearity in any model that includes both of them. This doesn’t mean we shouldn’t include them, nor that the significance or predictive power of the model is reduced. All it means is that we can’t use the individual regression coefficients to make predictions.</p> <p>Here are the linear models for trips to work, and home:</p> <div class="highlight"><pre>to_abr_lm <span class="o">&lt;-</span> lm<span class="p">(</span>data<span class="o">=</span>to_abr<span class="p">,</span> kcal_per_mile <span class="o">~</span> min_temp <span class="o">+</span> wdoy<span class="p">)</span> <span class="kp">print</span><span class="p">(</span><span class="kp">summary</span><span class="p">(</span>to_abr_lm<span class="p">))</span> </pre></div> <pre class="literal-block"> ## ## Call: ## lm(formula = kcal_per_mile ~ min_temp + wdoy, data = to_abr) ## ## Residuals: ## Min 1Q Median 3Q Max ## -27.845 -6.964 -3.186 3.609 53.697 ## ## Coefficients: ## Estimate Std. Error t value Pr(&gt;|t|) ## (Intercept) 170.81359 15.54834 10.986 1.07e-14 *** ## min_temp -0.45694 0.18368 -2.488 0.0164 * ## wdoy -0.29974 0.05913 -5.069 6.36e-06 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 15.9 on 48 degrees of freedom ## Multiple R-squared: 0.4069, Adjusted R-squared: 0.3822 ## F-statistic: 16.46 on 2 and 48 DF, p-value: 3.595e-06 </pre> <div class="highlight"><pre>to_home_lm <span class="o">&lt;-</span> lm<span class="p">(</span>data<span class="o">=</span>to_home<span class="p">,</span> kcal_per_mile <span class="o">~</span> min_temp <span class="o">+</span> wdoy<span class="p">)</span> <span class="kp">print</span><span class="p">(</span><span class="kp">summary</span><span class="p">(</span>to_home_lm<span class="p">))</span> </pre></div> <pre class="literal-block"> ## ## Call: ## lm(formula = kcal_per_mile ~ min_temp + wdoy, data = to_home) ## ## Residuals: ## Min 1Q Median 3Q Max ## -21.615 -10.200 -1.068 3.741 39.005 ## ## Coefficients: ## Estimate Std. Error t value Pr(&gt;|t|) ## (Intercept) 144.16615 18.55826 7.768 4.94e-10 *** ## min_temp -0.47659 0.16466 -2.894 0.00570 ** ## wdoy -0.20581 0.07502 -2.743 0.00852 ** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 13.49 on 48 degrees of freedom ## Multiple R-squared: 0.5637, Adjusted R-squared: 0.5455 ## F-statistic: 31.01 on 2 and 48 DF, p-value: 2.261e-09 </pre> <p>The models confirm what we saw in the plots. Both regression coefficients are negative, which means that as the temperature rises (and as the winter goes on) I consume less energy per mile. The models themselves are significant as are the coefficients, although less so in the trips to work. The amount of variation in kcal/mile explained by minimum temperature and winter day of year is 41% for trips to work and 56% for trips home.</p> <p>What accounts for the rest of the variation? My guess is that trail conditions are the missing factor here; specifically fresh snow, or a trail churned up by snowmachiners. I think that’s also why the results are better on trips home than to work. On days when we get snow overnight, I am almost certainly riding on an pristine snow-covered trail, but by the time I leave work, the trail will be smoother and harder due to all the traffic it’s seen over the course of the day.</p> </div> <div class="section" id="conclusions"> <h1>Conclusions</h1> <p>We didn’t really find anything surprising here: it is significantly harder to ride a fat bike when it’s colder. Because of conditioning, improved trail conditions, as well as the tendency for warmer weather later in the season, it also gets easier to ride as the winter goes on.</p> </div> </div> Sun, 20 Dec 2015 15:16:29 -0900 http://swingleydev.com/blog/p/1990/ exercise fat bike R temperature Bicycling in the 90s http://swingleydev.com/blog/p/1989/ <div class="document"> <div class="figure align-right"> <img alt="1991 contacts" src="//media.swingleydev.com/img/blog/2015/09/1991_email_bitnet_uucp.jpg" style="width: 300px; height: 169px;" /> <p class="caption">1991 contacts</p> </div> <p>Yesterday I was going through my journal books from the early 90s to see if I could get a sense of how much bicycling I did when I lived in Davis California. I came across the list of my network contacts from January&nbsp;1991 shown in the photo. I had an email, bitnet and uucp address on the UC Davis computer system. I don’t have any record of actually using these, but I do remember the old email clients that required lines be less than 80 characters, but which were unable to edit lines already entered.</p> <p>I found the statistics for 109 of my bike rides between April&nbsp;1991 and June&nbsp;1992, and I think that probably represents most of them from that period. I moved to Davis in the fall of 1990 and left in August&nbsp;1993, however, and am a little surprised I didn’t find any rides from those first six months or my last year in California.</p> <p>I rode 2,671 miles in those fifteen months, topping out at 418 miles in June&nbsp;1991. There were long gaps in the record where I didn’t ride at all, but when I rode, my average weekly mileage was 58 miles and maxed out at 186 miles.</p> <p>To put that in perspective, in the last seven years of commuting to work and riding recreationally, my highest monthly mileage was 268 miles (last month!), my average weekly mileage was 38 miles, and the farthest I’ve gone in a week was 81 miles.</p> <p>The road biking season is getting near to the end here in Fairbanks as the chances of significant snowfall on the roads rises dramatically, but I hope that next season I can push my legs (and hip) harder and approach some of the mileage totals I reached more than twenty years ago.</p> </div> Sun, 20 Sep 2015 09:50:00 -0800 http://swingleydev.com/blog/p/1989/ bicycling email bitnet uucp Thirty yards of wood chips http://swingleydev.com/blog/p/1988/ <div class="document"> <div class="figure align-right"> <a class="reference external image-reference" href="//media.swingleydev.com/img/photolog/2015/09/thirty_yards_of_wood_chips_for_the_dog_yard_2015-09.jpg"><img alt="Thirty yards of wood chips" src="//media.swingleydev.com/img/blog/2015/09/thirty_yards_of_wood_chips_for_the_dog_yard_2015-09.jpg" style="width: 300px; height: 169px;" /></a> <p class="caption">Thirty yards of wood chips</p> </div> <p>Every couple years we cover our dog yard with a fresh layer of wood chips from the local sawmill, Northland Wood. This year I decided to keep closer track of how much effort it takes to move all 30 yards of wood chips by counting each wheelbarrow load, recording how much time I spent, and by using a heart rate monitor to keep track of effort.</p> <p>The image below show the tally board. Tick marks indicate wheelbarrow-loads, the numbers under each set of five were the number of minutes since the start of each bout of work, and the numbers on the right are total loads and total minutes. I didn’t keep track of time, or heart rate, for the first set of 36 loads.</p> <div class="figure align-center"> <a class="reference external image-reference" href="//media.swingleydev.com/img/photolog/2015/09/wood_chip_tally.jpg"><img alt="Wood chip tally" src="//media.swingleydev.com/img/blog/2015/09/wood_chip_tally_300.jpg" style="width: 300px; height: 301px;" /></a> </div> <p>It’s not on the chalkboard, but my heart rate averaged 96 beats per minute for the first effort on Saturday morning, and 104, 96, 103, and 103 bpm for the rest. That averages out to 100.9 beats per minute.</p> <p>For the loads where I was keeping track of time, I averaged 3 minutes and 12 seconds per load. Using that average for the 36 loads on Friday afternoon, that means I spent around 795 minutes, or 13 hours and 15 minutes moving and spreading 248 wheelbarrow-loads of chips.</p> <p>Using a formula found in [Keytel LR, et al. 2005. <em>Prediction of energy expenditure from heart rate monitoring during submaximal exercise.</em> J Sports Sci. 23(3):289-97], I calculate that I burned 4,903 calories above the amount I would have if I’d been sitting around all weekend. To put that in perspective, I burned 3,935 calories running the Equinox Marathon in September, 2013.</p> <p>As I was loading the wheelbarrow, I was mentally keeping track of how many pitchfork-loads it took to fill the wheelbarrow, and the number hovered right around 17. That means there are about 4,216 pitchfork loads in 30 yards of wood chips.</p> <p>To summarize: 30 yards of wood chips is equivalent to 248 wheelbarrow loads. Each wheelbarrow-load is 0.1209 yards, or 3.26 cubic feet. Thirty yards of wood chips is also equivalent to 4,216 pitchfork loads, each of which is 0.19 cubic feet. It took me 13.25 hours to move and spread it all, or 3.2 minutes per wheelbarrow-load, or 11 seconds per pitchfork-load.</p> <p>One final note: this amount completely covered all but a few square feet of the dog yard. In some places the chips were at least six inches deep, and in others there’s just a light covering of new over old. I don’t have a good measure of the yard, but if I did, I’d be able to calculate the average depth of the chips. My guess is that it is around 2,500 square feet, which is what 30 yards would cover to an average depth of 4 inches.</p> </div> Mon, 07 Sep 2015 14:12:06 -0800 http://swingleydev.com/blog/p/1988/ wood chips dog yard dogs heart rate calories