Nonlinear Regression - Optimization

Often a regression problem is embedded in a larger model of interest; Linear predictors are a flexible way to incorporate covariate information. In such settings, however, we generally rely on numerical optimization techniques, rather than closed form solutions.

In this challenge, we’ll be performing a regression embedded in a growth curve called the `Gompertz Function’. You can read more about this function here, but it has the form:

\(y(t) = \alpha e^{-b e^{-ct}}\), where \(a\) is the asymptote of the growth curve, \(b\) determines how the curve is shifted/stretched over the time axis, \(c\) gives the growth rate, and \(t\) is time.

The problem we’re interested in concerns a regression with repeated measures on subjects in which the mean is assumed to follow such a function: \(Y_{ij} \sim N(\mu_{ij}, \sigma^2)\), where \(\mu_{ij} = a_iexp\left\{-be^{-c_it_i} \right\}\). The \(i\) index refers to subjects, and the \(j\) index refers to observations within subejcts. The \(a_i\) and \(c_i\) terms also have additional structure:

As you can see, both \(a_i\) and \(c_i\) depend on regression coeffficients (\(\boldsymbol{\beta},\ \boldsymbol{\gamma}\)), and a scalar coefficient \(x_i\). In a future challenge, we’ll add a person specific random effect \(\delta_i\) which impacts the asymptote.

Simulating an example of such data is pretty straightforward (especially if, like me, you don’t feel like optimizing the code):

GenerateGompertz <- function(N=1000, 
                             P=15, 
                             beta = c(1, 2),
                             gamma = c(0.5,0.25),
                             b = 2, sigma = 0.25){
  X <- cbind(1, rnorm(N))
  
  a <- exp(X%*%beta)
  c <- exp(X%*%gamma)
  b <- exp(b)
  dat <- lapply(1:N, function(i){
    # Generate time points
    tpts <- 1:P
    # Compute means
    mu <- a[i]*exp(-b*exp(-c[i]*tpts))
    # Generate Y
    Y <- rnorm(rep(1, length(mu)), mu, sd = sigma)
    
    # Output data set
    list(ID = rep(i,P),
          X = rep(X[i,2],P),
          tpts = tpts,
          Y = Y)
  })
  out <- data.frame(ID = unlist(lapply(dat, function(x){x$ID})),
                    X = unlist(lapply(dat, function(x){x$X})),
                    tpts = unlist(lapply(dat, function(x){x$tpts})),
                    Y = unlist(lapply(dat, function(x){x$Y})))
  structure(out, class = c(class(out), "GompertzData"))
}

dat <- GenerateGompertz(N = 100)

# Plot several example curves:

par(mfrow = c(2,2))
for (i in sample(1:100, size = 4)){
  keep <- dat$ID == i
  plot(dat$tpts[keep], dat$Y[keep], type = "b", 
       main = paste0("Curve for Subject ", i), xlab = "Time", ylab = "Y")
}

Your task is to write a function which can accept data of the form generated by the ‘GenerateGompertz’ function above, and return reasonable estimates of \(\beta\) and \(\gamma\). Your function should be named: ‘FitGompertz’, and it should accept a “GompertzData” object as follows:

FitGompertz <- function(gdat){
  if (!("GompertzData" %in% class(gdat))){
    stop("This function should be used only on specially formatted GompertzData objects")
  }
  # fit model
  # return results
  beta <- c(-1, 2)
  gamma = c(-1,-1)
  return(c(beta, gamma))
}

Note: \(\beta\ \gamma\ \mbox{b, and } \sigma^2\) can all be assumed to be >= 0 for this problem.