Curve fitting on batches in the tidyverse: R, dplyr, and broom

[This article was first published on R on Programming notes, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I recently needed to fit curves on several sets of similar data, measured from different sensors. I found how to achieve this with dplyr, without needing to define outside functions or use for-loops. This approach integrates perfectly with my usual dplyr and ggplot2 workflows, which means it adapts to new data or new experimental conditions with no changes. Here are the ready-made recipes for any one else who may run into a similar problem.

First we’ll generate some artificial data so that you can follow along at home. Here’s an exponentially decaying value measured by two sensors at different locations:

# if needed
install.packages(c("tidyverse", "broom"))

library(tidyverse)
library(broom)

t = 1:100
y1 = 22 + (53 - 22) * exp(-0.02 * t) %>% jitter(10)
y2 = 24 + (60 - 22) * exp(-0.01 * t) %>% jitter(10)


df <- data.frame(t = t, y = y1, sensor = 'sensor1') %>% 
  rbind(. , data.frame(t = t, y = y2, sensor = 'sensor2'))


Our data frame looks like this:

df
tysensor
152.25302sensor1
251.71440sensor1
351.13971sensor1
9838.13328sensor2
9938.17017sensor2
10037.72184sensor2

And our data looks like this:

qplot(t, y, data = df, colour = sensor)

raw_data

I created the data in long format, as it works best with dplyr pipelines. You can read more about long format vs wide format data here.

TL;DR

I want to plot the fitted curve over my data points

augmented <- df %>% 
  group_by(sensor) %>% 
  do(fit = nls(y ~ a * t + b, data = .)) %>% 
  augment(fit)

qplot(t, y, data = augmented, geom = 'point', colour = sensor) +
  geom_line(aes(y=.fitted))

raw_data

I want to plot the residuals

qplot(t, .resid, data = augmented, colour = sensor)
qplot(.fitted, .resid, data = augmented, colour = sensor)

raw_data

raw_data

I want a table of my fit parameters for each condition:

df %>% 
  group_by(sensor) %>% 
  do(fit = nls(y ~ a * t + b, data = .)) %>% 
  tidy(fit) %>% 
  select(sensor, term, estimate) %>% 
  spread(term, estimate)
sensorab
1sensor1-0.249534747.87376
2sensor2-0.235089859.77793

I want a less horrendous fit. How can I do better?

Try with an actual exponential. You’ll probably want a self-starting function to avoid the “singular gradient” error — read more in my post on the subject.

Explanation: intro to curve fitting in R

The goal of our fitting example was to find an estimate $\hat{y}(t) = a t + b$ that approximates our measured data $y$. In R, we can directly write that we want to approximate $y$ as a function $a \cdot t + b$, using the very intuitive built-in formula syntax:

y ~ a * t + b

In R’s documentation, $a$ and $b$ are the coefficients, $t$ is the independent variable and $y$ is the dependent variable — $t$ can go about its day without every knowing $y$ exists, but $y$ can’t progress without knowing the latest trend in $t$.

We fit these models using various fitting functions: lm or glm for linear models or nls for non-linear least squares for example. I’m using nls here because I can specify the names of my coefficients, which I find clearer to explain:

# Select the data from our first sensor:
sensor1 <- df %>% filter(sensor == "sensor1")

# Fit our model:
fit <- nls(y ~ a * t + b, data = sensor1)

# nls works best if we specify an initial guess for the coefficients, 
# the 'start' point for the optimisation:
fit <- nls(y ~ a * t + b, data = sensor1, start = list(a = 1, b = 10))

The fitting functions all return a fit object:

> summary(fit)
Formula: y ~ a * t + b

Parameters:
   Estimate Std. Error t value Pr(>|t|)    
a -0.249535   0.006377  -39.13   <2e-16 ***
b 47.873759   0.370947  129.06   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 1.841 on 98 degrees of freedom

Number of iterations to convergence: 1 
Achieved convergence tolerance: 1.571e-09

You can use one of these base R functions to extract useful data from the fit object:

  • coef(fit) returns the values of the coefficients $a$ and $b$.
  • predict(fit) returns $\hat{y}(t_i)$, i.e. it applies the fitted model to each of the original data points $t_i$. It can also be applied on new values of $t$.
  • resid(fit) returns the residuals $y_i – \hat{y}(t_i)$ at each point of the original data.

Try them out in the console:

> coef(fit)
         a          b 
-0.2495347 47.8737585

> predict(fit)
[1] 47.62422 47.37469 47.12515 46.87562 46.62609 46.37655 ...

> predict(fit, newdata = data.frame(t = 101:200))
[1] 22.67076 22.42122 22.17169 21.92215 21.67262 21.42309 ...

> resid(fit)
[1] 4.628798 4.339712 4.014558 3.576258 3.528138 3.001447 ...

Now these functions all return vectors, which work best with R’s native plotting functions. To integrate with dplyr and ggplot, we’d rather have data frames. This is where the broom package comes in.

Introducing broom

Broom is a separate R package that feeds on fit results and produces useful data frames. Install it directly within the R console if you haven’t already:

install.packages('broom')
library(broom)

It provides three useful functions, which are easier to demonstrate than to explain. All of them return data frames:

  • tidy extracts the fit coefficients and related information. The term column contains the name of your coefficient, and estimate contains its fitted value:
tidy(fit)
termestimatestd.errorstatisticp.value
a-0.24953470.006377179-39.129321.267667e-61
b47.87375850.370946838129.058273.123848e-111
  • augment is equivalent to predict and resid combined. The returned data frame contains columns t and y with your original data, as well as .fitted, the fitted curve, and .resid, the residuals:
augment(fit)
ty.fitted.resid
152.2530247.624224.62879752
251.7144047.374694.33971163
351.1397147.125154.01455804
450.4518846.875623.57625801

You can plot the ‘augmented’ fit with qplot:

qplot(t, y, data = augment(fit)) + geom_line(aes(y = .fitted))
qplot(t, .resid, data = augment(fit))

raw_data

raw_data

  • glance shows fit statistics:
glance(fit)
sigmaisConvfinTollogLikAICBICdeviancedf.residual
1.840841TRUE1.571375e-09-201.906409.8119417.6274332.092198

Using broom with dplyr

Since broom’s functions return data frames, they integrate naturally with magrittr pipelines (%>%) and dplyr. Let’s illustrate by grouping the data by sensor id, then producing a fit object for each group. The do function calls nls once for each group, then stores the return value in a column we named fit:

fitted <- df %>% 
  group_by(sensor) %>% 
  do(fit = nls(y ~ a * t + b, data = ., start = list(a = 1, b = 1)))
fitted
sensorfit
1sensor1
2sensor2

We now have one nls object per group, which can be fed into either tidy or augment. These functions act on each group separately:

fitted %>% 
  tidy(fit)
sensortermestimatestd.errorstatisticp.value
sensor1a-0.24953470.006377179-39.129321.267667e-61
sensor1b47.87375850.370946839129.058273.123849e-111
sensor2a-0.23508980.003088217-76.124785.400611e-89
sensor2b59.77792910.179634964332.774461.931798e-151

From here, we can use select and spread to generate a more succinct table, as in the TL;DR example, or keep the table as-is to inspect the quality of the fit.

The result of augment is a long data frame that can be used immediately with qplot, also shown in the TL;DR section above:

fitted %>% 
  augment(fit)
sensorty.fitted.resid
sensor1152.4168447.582204.834645067
sensor1251.6432347.333474.309757950
sensor1351.0959547.084754.011196179
sensor29838.0602936.724281.336007642
sensor29937.8689136.489261.379642201
sensor210037.8993636.254251.645117019

That’s it for broom and dplyr. The magic of this approach is that you can add a sensor to your input data then re-run all your code; your new curves will just appear on the plots. Similarly, you can change the formula of your fit, and your new coefficients will appear as a new column in the coefficients table. No need to change your code.

References

To leave a comment for the author, please follow the link and comment on their blog: R on Programming notes.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)