title: User-defined link function for glmer for known-fate survival modeling
body: <p>A common situation in ecology is a survival model with a binary outcome (0=die, 1=survive) where individuals (for this example think of individual nesting attempts by birds) differ in the number of days they are exposed to risk of mortality.  To account for this, we use a modified logistic regression which incorporates the # of exposure days into the link function.<br />
  As described by Shaffer (2004):</p>
  <p>“The daily survival rate is modeled in terms of x through the choice of an appropriate predictor function, which in our case should yield values between zero and one. As is done in logistic regression, we use the S-shaped logistic function:</p>
  <p><img src="https://i.stack.imgur.com/2R0nz.jpg" alt="enter image description here" /></p>
  <p>The systematic component of our generalized linear model is then [s(x)]t.  Next, we consider the function:</p>
  <p><img src="https://i.stack.imgur.com/hV9Vw.jpg" alt="enter image description here" /></p>
  <p>The above function is monotonic and differentiable with respect to θ, and it can be shown that g(θ) = β0 + β1x, which satisfies the criteria for a link function in a generalized linear model. Those three components: the binomial response distribution, the predictor function given in Expression 1, and the link function given in Expression 2, completely specify our generalized linear model. The model (hereafter “the logistic-exposure model”) is similar to the logistic regression model but differs in the form of the link function. The logistic-exposure link function contains an exponent (1/t) in the numerator and denominator that is not present in the logistic-regression link function. The exponent is necessary to account for the fact that probability of surviving an interval depends on interval length.”</p>
  <p>Code for this link function is available on the web, and is also one of the example link functions described in R if you type &quot;help(family)&quot;:</p>
  <pre><code>logexp &lt;- function(days = 1)
  {
  linkfun &lt;- function(mu) qlogis(mu^(1/days))
  linkinv &lt;- function(eta) plogis(eta)^days
  mu.eta &lt;- function(eta) days * plogis(eta)^(days-1) *
    .Call(&quot;logit_mu_eta&quot;, eta, PACKAGE = &quot;stats&quot;)
  valideta &lt;- function(eta) TRUE
  link &lt;- paste(&quot;logexp(&quot;, days, &quot;)&quot;, sep=&quot;&quot;)
  structure(list(linkfun = linkfun, linkinv = linkinv,
                 mu.eta = mu.eta, valideta = valideta, name = link),
            class = &quot;link-glm&quot;)
  }
  </code></pre>
  <p>It works just fine in a model such as this:</p>
  <pre><code>glm(survive ~ date, family=binomial(link=logexp(days=dat$Days)),data=dat)
  </code></pre>
  <p>The problem I run into is when trying to use this custom link function in a GLMER model with the addition of a random effect (I have found one example online of this method being implemented here: <a href="http://rstudio-pubs-static.s3.amazonaws.com/4082_51aa699bd9f041c7b3f7cf7b9252f60c.html" rel="nofollow noreferrer">http://rstudio-pubs-static.s3.amazonaws.com/4082_51aa699bd9f041c7b3f7cf7b9252f60c.html</a>).</p>
  <p>In our case we would like to include site as a random effect. Models are formulated the same way as the GLMs from before:</p>
  <pre><code>glmer(survive ~ date + (1|site), family=binomial(link=logexp(days=dat$Days)),data=dat)
  </code></pre>
  <p>However, now I get an error message:</p>
  <p>Error in famType(glmFit$family) :
  unknown link: ‘logexp(3)’unknown link: ‘logexp(4)’unknown link: ‘logexp(3)’unknown link: ‘logexp(2)’unknown link: ‘logexp(3)’unknown link: ‘logexp(3)’unknown link: ‘logexp(4)’unknown link: ‘logexp(3)’unknown link: ‘logexp(2)’unknown link: ‘logexp(1)’unknown link: ‘logexp(4)’unknown link: ‘logexp(5)’unknown link: ‘logexp(4)’unknown link: ‘logexp(3)’unknown link: ‘logexp(4)’unknown link: ‘logexp(5)’unknown link: ‘logexp(3)’unknown link: ‘logexp(4)’unknown link: ‘logexp(3)’unknown link: ‘logexp(3)’unknown link: ‘logexp(3)’unknown link: ‘logexp(3)’unknown link: ‘logexp(3)’unknown link: ‘logexp(3)’unknown link: ‘logexp(3)’unknown link: ‘logexp(3)’unknown link: ‘logexp(3)’unknown link: ‘logexp(2)’unknown link: ‘logexp(1)’unknown link: ‘logexp(3)’unknown link: ‘logexp(1)’unknown link: ‘logexp(1)’unknown link: ‘logexp(1)’unknown link: ‘logexp(1)’unkn
  In addition: Warning message:
  In if (!(lTyp &lt;- match(family$link, linkNms, nomatch = 0))) stop(gettextf(&quot;unknown link: %s&quot;,  :
  the condition has length &gt; 1 and only the first element will be used</p>
  <p>The error message lists an unknown link for every row of data with the numbers corresponding to the exposure days for that nest visit (or row of data).</p>
  <p>ex: The first 'logexp(3)' corresponds to the first row of data which has 3 exposure days.</p>
  <p>Has anyone else been able to use this custom link function in a GLMER model? Or if not does anyone have an idea about what’s causing the error?</p>
  <p>######UPDATE######</p>
  <p>Many thanks to Ben Bolker for solving this problem.  I updated to 3.0.2 and the latest version of lme4 and used the link function Ben's R related post (<a href="https://www.rpubs.com/bbolker/logregexp" rel="nofollow noreferrer">https://www.rpubs.com/bbolker/logregexp</a>), which is this one:</p>
  <pre><code>library(MASS)
  logexp &lt;- function(exposure = 1)
  {
  linkfun &lt;- function(mu) qlogis(mu^(1/exposure))
  ## FIXME: is there some trick we can play here to allow
  ##   evaluation in the context of the 'data' argument?
  linkinv &lt;- function(eta)  plogis(eta)^exposure
  mu.eta &lt;- function(eta) exposure * plogis(eta)^(exposure-1) *
    .Call(stats:::C_logit_mu_eta, eta, PACKAGE = &quot;stats&quot;)
  valideta &lt;- function(eta) TRUE
  link &lt;- paste(&quot;logexp(&quot;, deparse(substitute(exposure)), &quot;)&quot;,
                 sep=&quot;&quot;)
  structure(list(linkfun = linkfun, linkinv = linkinv,
                 mu.eta = mu.eta, valideta = valideta, 
                 name = link),
            class = &quot;link-glm&quot;)
  }
  </code></pre>