2

I have a nonlinear mixed-effects model that I have implemented in nlme that I would like to implement in TMB to decrease computation times and hopefully some occasional convergence issues.

In its simplest form, the model is given by

y_ij = f((1 - b_i) * t_ij, knot_values) + e_ij

where y_ij is the observation at time t_ij, f is a natural cubic spline interpolation function with values knot_values at a set of given knot points, b_i is a parameter that decides how fast subject i progresses through the trajectory and e_ij is a normally distributed error terms.

An example of an implementation of this model in nlme is

# Mean function, v0, v1 and v2 are knot values
f <- function(t, v0, v1, v2, b) {
  months <- c(0, 6, 12) # Predefined observation times
  b <- cbind(0, b, b)
  t_out <- (1 - b[cbind(1:length(t), match(t, months))]) * t 
  
  spline(x = months,
         y = c(v0[1], v1[1], v2[1]),
         method = 'natural',
         xout = t_out)$y
}

# Fit model
gnls(model = y ~ f(M, v0, v1, v2, b),
     data = dat,
     params = list(v0 + v1 + v2 ~ 1,
                   b ~ group + 0),
     start = start_vec)

While it is straightforward to implement conventional spline models in TMB that are linear in the interpolation values (knot_values), my problem with porting this model is that the model is nonlinear in the parameter b_i. Is there a nice way to implement such a model in TMB?

Lars Lau Raket
  • 1,905
  • 20
  • 35
  • I would probably try passing the spline basis to TMB as data and solving for the spline coefficients internally ... ?? – Ben Bolker Apr 09 '23 at 16:03
  • @BenBolker That is how I would handle the spline coefficients v0, v1, v2, but for b_i, I believe I need to access the functional form of the spline from C++ so that the automatic differentiation magic can happen. – Lars Lau Raket Apr 09 '23 at 19:05

0 Answers0