rnon-linear-regressionnlsnlmedrc

Adding a covariate to an asymptotic regression model in R with nlme and SSasympOrig


I have two groups of data and reason to believe that they follow the same asymptotic curve with offset upper limits. All the observations are assumed to be independent. I fit a simple model with the drc library as such:

library(drc)
library(nlme)
library(dplyr)

df <- structure(list(iv = c(1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 
2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 
5, 6), dv = c(9.2, 8.5, 13.5, 15.8, 18.3, 17.7, 8.7, 10.8, 14.3, 
15, 18, 15.3, 8.7, 14.6, 14.8, 16.8, 15.8, 15.8, 14.2, 13.5, 
18.5, 20.8, 23.3, 22.7, 13.7, 15.8, 19.3, 20, 23, 20.3, 13.7, 
19.6, 19.8, 21.8, 20.8, 20.8), group = c("1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", 
"2", "2", "2", "2", "2", "2")), row.names = c(NA, -36L), class = c("tbl_df", 
"tbl", "data.frame"))

drm_mod <- drm(
  dv ~ iv,
  curveid = group,
  data = df,
  fct = AR.2(names = c("Upper Limit", "Steepness"))
)

enter image description here

And so I attempt to replicate with nlme and fail:

nlme_mod <- nlme(dv ~ SSasympOrig(iv, Asm, lrc), data=df, 
                    fixed= list(Asym + lrc ~ group),
                    groups = group,
                    random = ~ 1)
Error: object 'group' not found
2.
nlme.formula(dv ~ SSasympOrig(iv, Asm, lrc), data = df, fixed = list(Asym +
lrc ~ group), groups = group, random = ~1)
1.
nlme(dv ~ SSasympOrig(iv, Asm, lrc), data = df, fixed = list(Asym +
lrc ~ group), groups = group, random = ~1)

If I remove the group specification I get the following:

nlme_mod <- nlme(dv ~ SSasympOrig(iv, Asm, lrc), data=df, 
                    fixed= list(Asym + lrc ~ group),
                #    groups = group,
                    random = ~ 1)

Error in nlsList.formula(model = dv ~ SSasympOrig(iv, Asm, lrc), data = df) : 
  'data' must be a "groupedData" object if 'formula' does not include groups
8.
stop("'data' must be a \"groupedData\" object if 'formula' does not include groups")
7.
nlsList.formula(model = dv ~ SSasympOrig(iv, Asm, lrc), data = df)
6.
nlme::nlsList(model = dv ~ SSasympOrig(iv, Asm, lrc), data = df)
5.
eval(expr, p)
4.
eval(expr, p)
3.
eval.parent(nlsLCall)
2.
nlme.formula(dv ~ SSasympOrig(iv, Asm, lrc), data = df, fixed = list(Asym +
lrc ~ group), random = ~1)
1.
nlme(dv ~ SSasympOrig(iv, Asm, lrc), data = df, fixed = list(Asym +
lrc ~ group), random = ~1)

Solution

  • All the observations are assumed to be independent.

    Why are you trying to fit a mixed-effects model then?

    df$group <- factor(df$group)
    
    library(nlme)
    
    #get starting values
    mod0 <- nlsList(dv ~ SSasympOrig(iv, Asym, lrc) | group, data=df)
    coef <- coef(mod0)
    
    #due to treatment contrasts
    start <- rbind(coef[1,], apply(coef, 2, diff))
    
    mod <- gnls(dv ~ SSasympOrig(iv, Asym, lrc), data=df, 
                params = list(Asym + lrc ~ group), start = start)
    
    plot(dv ~ iv, data = df, pch = as.integer(df$group))
    curve(predict(mod, newdata = data.frame(iv = x, group = "1")), add = TRUE)
    curve(predict(mod, newdata = data.frame(iv = x, group = "2")), add = TRUE, lty = 2)
    

    resulting plot showing the data and two fitted curves