rdplyrmodelr

Group data by rep, fit model, and integrate curve


I have a series of points over time and I want to fit a curve to these points and then find the area under the curve. I can do it like this:

fit<-loess(mass ~ hour, df)
f <- function(x) predict(fit,newdata=x)
answer<-integrate(f, 0, 60)$value  

So far, so good, but my problem is that I have replications of the points. I want a fit and a value for each replication.

trt    rep    hour    mass
y      a      1       3
y      a      2       5
y      a      3       8
y      a      4       6
y      b      1       2
y      b      2       3
y      b      3       5
y      b      4       4
n      c      1       4
n      c      2       6
n      c      3       8
n      c      4       7
n      d      1       5
n      d      2       7
n      d      3       8
n      d      4       7 

I want to dplyr::group_by(rep), but the grouping doesn't come through to fit. I'm could also do grouping in nlme and use nlsList, but the package doesn't work well with loess or polynomials in general.


Solution

  • Seems like case for do(). With your sample data

    dd <- read.table(text="trt    rep    hour    mass
    y      a      1       3
    y      a      2       5
    y      a      3       8
    y      a      4       6
    y      b      1       2
    y      b      2       3
    y      b      3       5
    y      b      4       4
    n      c      1       4
    n      c      2       6
    n      c      3       8
    n      c      4       7
    n      d      1       5
    n      d      2       7
    n      d      3       8
    n      d      4       7 ", header=T)
    

    and your area calculation

    get_area <- function(df) {
        fit <- loess(mass ~ hour, df)
        f <- function(x) predict(fit,newdata=x)
        integrate(f, 0, 60)$value  
    }
    

    you can just run

    dd %>% group_by(rep) %>% do(area=get_area(.))