#Fitcoach: a personal coach for Fitbit
Project
Fitcoach was our class project for STATS 290 at Stanford, a course on programming in R, written with my classmate Niraj in early 2016. A Fitbit records what has already happened: steps so far, calories so far. We wanted to look forward instead. Given a goal (steps, calories, distance or floors), which habits actually move it, and what is left to do today to reach it?
The answer took the form of an R package, accepted on CRAN as version 1.0 in April 2016. Its code is still readable on CRAN's GitHub mirror.
The course
STATS 290, Paradigms for Computing with Data, was part of the graduate certificate in Data Mining & Applications I was taking through the Stanford Center for Professional Development (SCPD). I followed it from Paris during the winter quarter of 2016, watching the lectures on video a few hours after they were given on campus, and it remains a course I enjoyed very much.
It was taught by Balasubramanian Narasimhan, and treated R as a real programming language rather than a statistics calculator: functional programming and objects, package design, databases and SQL, interfaces with C, Python and Java, graphics, debugging, large data and cluster computing. There was no final exam. The grade came from four assignments and a final project: "an interesting R package on a topic you propose and we approve", in the words of the syllabus.
Niraj and I submitted our proposal in January, and the approval came back in February in two lines: "This sounds like an interesting project. Please proceed." The package was due on March 15, 2016, at noon, which the instructor reminded us was the Ides of March.
How it works
The package has three parts:
DataLoaderconnects to the Fitbit API through OAuth 2.0 and downloads the data as JSON files: one point per day for daily series, one point every 15 minutes for intraday series. Intraday data was only available to the owner of the Fitbit app, so each user had to register their own.FitAnalyzerturns those files into a clean data frame, adds context (weekday, weekend, holidays, cumulative totals during the day), then fits a model on the chosen goal and ranks the variables that drive it.FitUtilexposes the underlying functions, for anyone who wanted a different workflow than ours.
The model depends on the granularity. With one row per day, six months of data give fewer than 200 points, so a plain linear model (GLM) did the job and its coefficients were easy to read. With 96 slots per day, the intraday data was large enough for gradient boosting (GBM), ranked by relative influence. From the fitted model, predictGoal estimated where the day would end, which was the base of the recommendation: keep going at this pace, or walk more this evening.
Results
On Niraj's data, from August 2015 to February 2016, the daily analysis of steps put distance far ahead of everything else, then minutes of light activity, floors and elevation:


The intraday analysis of calories gave the shape of a typical day. Almost nothing at night, a slow start in the morning, a plateau during the day, then a clear peak in the late afternoon:


The grade
A week after the deadline, the feedback arrived by email. The project was graded on four criteria: the package passing R CMD check, its file naming, the code we wrote, and the summary vignette. Fitcoach got full marks on each, 35 out of 35, with one line of comment: "Good project, makes use of API calls and useful modelling." With the assignments, I finished the course with an A, at 97.9 out of 100.
The CRAN submission took one more round. The first attempt came back from Kurt Hornik, one of CRAN's maintainers, with a list of small fixes: a title not in title case, missing imports in the package namespace. We fixed them, and fitcoach was accepted on April 19, 2016.
Looking back
The weak point is visible in the results. Distance explains steps because Fitbit computes distance from steps, and the same goes for calories and METs in the intraday model. The models mostly rediscovered how the tracker derives its own metrics. A real coach would have needed to keep only the variables the wearer controls (time of day, weekday, activity earlier in the day) and to test the recommendations against what actually happened.
newsletter
Get each new field note by email, nothing else.
Subscribed, thanks. Your address is only used for this newsletter.