Regression in the strangest places

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(-1,-1), b = 2){
  X <- cbind(1, rnorm(N))
  
  a <- exp(X%*%beta)
  c <- exp(X%*%gamma)
  out <- Reduce("rbind", lapply(1:N, function(i){
    # Generate time points
    tpts <- cumsum(1+rpois(P, abs(rnorm(P, 1, 3))))
    # Compute means
    mu <- a[i]*exp(-b*exp(-c[i]*tpts))
    # Generate Y
    Y <- rnorm(rep(1, length(mu)), mu, sd = 0.05)
    # Output data set
    data.frame(ID = i,
               X = X[i,2],
               T = tpts,
               Y = Y)
  }))
  structure(out, class = c(class(out), "GompertzData"))
}


dat <- GenerateGompertz(N = 100)

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))
}