mon, 21-dec-2015, 16:58








diffs <- tbl(exercise_db,
   "WITH all_to_work AS (
      SELECT *,
            CASE WHEN extract(hour from start_time) < 11
                 THEN 'north' ELSE 'south' END AS direction
      FROM track_stats
      WHERE type = 'Fat Biking'
            AND miles between 4 and 4.3
   ), with_next AS (
            lead(direction) OVER w AS next_direction,
            lead(start_time) OVER w AS next_start_time,
            lead(kcal) OVER w AS next_kcal,
            lead(miles) OVER w AS next_miles,
            lead(min_temp) OVER w AS next_min_temp
      FROM all_to_work
      WINDOW w AS (PARTITION BY direction ORDER BY start_time)
   SELECT start_time, next_start_time, direction,
      min_temp, next_min_temp,
      kcal / miles AS kcal_per_mile,
      next_kcal / next_miles as next_kcal_per_mile,
      next_min_temp - min_temp AS temp_diff,
   FROM with_next
   WHERE next_start_time - start_time < '60 hours'
   ORDER BY start_time")) %>% collect()

start time next start time temp diff kcal / mile diff


Let’s look at a plot of the data.

s = ggplot(data=diffs,
         aes(x=temp_diff, y=kcal_per_mile_diff)) +
   geom_point() +
                     breaks=pretty_breaks(n=10)) +
                     breaks=pretty_breaks(n=10)) +
   theme_bw() +





Here’s the linear regression results for this data.

## Call:
## Residuals:
##     Min      1Q  Median      3Q     Max
## -40.839  -4.584  -0.169   3.740  47.063
## Coefficients:
## (Intercept)  -2.1696     1.5253  -1.422    0.159
## ---
## Residual standard error: 12.76 on 68 degrees of freedom


tags: exercise  fat bike  R  temperature 
sun, 20-dec-2015, 15:16


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.

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).

The code and data for this post is available on GitHub.


I log all my commutes (and other exercise) using the RunKeeper 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.

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 blog post.

Let’s take a look at the data:


fat_bike <- read.csv("fat_bike.csv", stringsAsFactors=FALSE, header=TRUE) %>%
    tbl_df() %>%
    mutate(start_time=ymd_hms(start_time, tz="US/Alaska"))

start_time miles time hours mph hr kcal min_temp max_temp
2013-11-27 06:22:13 4.17 0:35:11 0.59 7.12 157.8 518.4 -1.1 1.0
2013-11-27 15:27:01 4.11 0:35:49 0.60 6.89 156.0 513.6 1.1 2.2
2013-12-01 12:29:27 4.79 0:55:08 0.92 5.21 172.6 951.5 -25.9 -23.9
2013-12-03 06:21:49 4.19 0:39:16 0.65 6.40 148.4 526.8 -4.6 -2.1
2013-12-03 15:41:48 4.22 0:30:56 0.52 8.19 154.6 434.5 6.0 7.9
2013-12-05 06:31:54 4.14 0:32:14 0.54 7.71 155.8 463.2 -1.6 2.9

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.

I’m also calculating energy per mile, and adding a “winter day of year” variable (wdoy), 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.

I’m also excluding the really early season data from 2015 because the trail was in really poor condition.

fat_bike_commute <- fat_bike %>%
    filter(miles>4, miles<4.3) %>%
    mutate(direction=ifelse(hour(start_time)<10, 'north', 'south'),
           date=as.Date(start_time, tz='US/Alaska'),
           kcal_per_mile=kcal/miles) %>%
    group_by(date) %>%
    mutate(n=n()) %>%
    ungroup() %>%

to_abr <- fat_bike_commute %>% filter(direction=='north',
to_home <- fat_bike_commute %>% filter(direction=='south',
kable(head(to_home %>% select(-date, -kcal, -n)))
start_time miles time hours mph hr min_temp max_temp direction wdoy kcal_per_mile
2013-11-27 15:27:01 4.11 0:35:49 0.60 6.89 156.0 1.1 2.2 south 211 124.96350
2013-12-03 15:41:48 4.22 0:30:56 0.52 8.19 154.6 6.0 7.9 south 217 102.96209
2013-12-05 15:24:10 4.18 0:29:07 0.49 8.60 150.7 9.7 12.0 south 219 94.13876
2013-12-06 16:38:31 4.17 0:26:04 0.43 9.60 154.3 23.3 24.7 south 220 88.63309
2013-12-09 13:44:59 4.11 0:32:06 0.54 7.69 161.3 27.5 28.5 south 223 119.19708
2013-12-11 16:00:11 4.19 0:33:48 0.56 7.44 157.6 2.1 4.5 south 225 118.16229


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.

s <- ggplot(data=fat_bike_commute %>% filter(wdoy>210), aes(x=wdoy, y=kcal_per_mile, colour=min_temp)) +
    geom_smooth(method="lm", se=FALSE, colour=mnsl("10B 7/10", fix=TRUE)) +
    geom_point(size=3) +
                       breaks=c(215, 246, 277, 305, 336),
                       labels=c('1-Dec', '1-Jan', '1-Feb', '1-Mar', '1-Apr')) +
    scale_y_continuous(name="Energy (kcal)", breaks=pretty_breaks(n=10)) +
    scale_colour_continuous(low=mnsl("7.5B 5/12", fix=TRUE), high=mnsl("7.5R 5/12", fix=TRUE),
                            guide=guide_colourbar(title="Min temp (°F)", reverse=FALSE, barheight=8)) +
    ggtitle("All fat bike trips") +

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.

Let’s look at the temperature relationship:

s <- ggplot(data=fat_bike_commute %>% filter(wdoy>210), aes(x=min_temp, y=kcal_per_mile, colour=wdoy)) +
    geom_point(size=3) +
    scale_colour_continuous(low=mnsl("7.5PB 2/12", fix=TRUE), high=mnsl("7.5PB 8/12", fix=TRUE),
                            guide=guide_colourbar(title=NULL, reverse=TRUE, barheight=8)) +
    ggtitle("All fat bike trips") +

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.

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.


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.

Here are the linear models for trips to work, and home:

to_abr_lm <- lm(data=to_abr, kcal_per_mile ~ min_temp + wdoy)
## 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:
## (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 ***
## ---
## 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
to_home_lm <- lm(data=to_home, kcal_per_mile ~ min_temp + wdoy)
## 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:
## (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 **
## ---
## 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

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.

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.


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.

tags: exercise  fat bike  R  temperature 
tue, 07-jan-2014, 15:27
Equinox Marathon finish

Equinox Marathon finish




Here are my totals in tabular form:

2013 Exercise Totals
type miles hours calories
Running 529 89 61,831
Bicycling 1,018 82 54,677
Skiing 475 81 49,815
Hiking 90 43 18,208
TOTAL 2,113 296 184,531






  • hr = Heart rate (in beats/minute)
  • w = Weight (in pounds)
  • a = Age (in years)
  • t = Exercise duration time (in hours)


--- Kcalories burned based on average heart rate and number
--- of hours at that rate.
CREATE OR REPLACE FUNCTION kcal_from_hr(hr numeric, hours numeric)
RETURNS numeric
LANGUAGE plpgsql
AS $$
    weight_lb numeric := nnn;
    resting_hr numeric := nn;
    birthday date := 'yyyy-mm-dd';
    resting_kcal numeric;
    exercise_kcal numeric;
    RETURN exercise_kcal - resting_kcal;













Dog Barn






It’ll be an interesting data set.




Yearly electricity use
year average watts total KWH
2010 551 4822
2011 493 4318
2012 433 3792
2013 418 3661


Here’s the pattern by month, and year.


That wraps it up. Have a happy and productive 2014!

Meta Photolog Archives