# [R-sig-ME] Error message from nlmer

Douglas Bates bates at stat.wisc.edu
Tue Oct 30 13:31:29 CET 2007

```On 10/30/07, Kari Ruohonen <kari.ruohonen at utu.fi> wrote:
> I apologise that I re-post this question but I just learnt about this
> specific mixed model list, and since I got no replies from r-help I
> thought there may be people able to help on this list that are not
> reading the main r-help.
>
> Once more my sincere apologies if someone feels this is misuse of the
> list. I would appreciate very much any hints to solve the problem
> described below since I am totally puzzled.

No apology needed.  This type of message is entirely appropriate for this list.

I didn't notice your message on R-help or I would have responded there.

The answer to your question is that nlmer currently requires an
analytic gradient of the nonlinear model function.  You could use the
deriv function to create a version of your grModel that returns the

> grModel <- deriv(expression(((x-Tmin)*(x-Tmax))/((x-Tmin)*(x-Tmax)-(x-Topt)^2)*kopt*w^m),
+        c("Tmin","Tmax","Topt","kopt","m"), function(x, Tmin, Tmax,
Topt, kopt, m){})
> grModel <- function(x,w,Tmin,Tmax,Topt,kopt,m)((x-Tmin)*(x-Tmax))/((x-Tmin)*(x-Tmax)-(x-Topt)^2)*kopt*w^m
> str(body(grModel))
language ((x - Tmin) * (x - Tmax))/((x - Tmin) * (x - Tmax) - (x -
Topt)^2) *      kopt * w^m
> grModg <- deriv(body(grModel), namevec = c("Tmin","Tmax","Topt","kopt","m"), func = grModel)
> grModg
function (x, w, Tmin, Tmax, Topt, kopt, m)
{
.expr1 <- x - Tmin
.expr2 <- x - Tmax
.expr3 <- .expr1 * .expr2
.expr4 <- x - Topt
.expr6 <- .expr3 - .expr4^2
.expr7 <- .expr3/.expr6
.expr8 <- .expr7 * kopt
.expr9 <- w^m
.expr13 <- .expr6^2
.value <- .expr8 * .expr9
.grad <- array(0, c(length(.value), 5L), list(NULL, c("Tmin",
"Tmax", "Topt", "kopt", "m")))
.grad[, "Tmin"] <- -((.expr2/.expr6 - .expr3 * .expr2/.expr13) *
kopt * .expr9)
.grad[, "Tmax"] <- -((.expr1/.expr6 - .expr3 * .expr1/.expr13) *
kopt * .expr9)
.grad[, "Topt"] <- -(.expr3 * (2 * .expr4)/.expr13 * kopt *
.expr9)
.grad[, "kopt"] <- .expr7 * .expr9
.grad[, "m"] <- .expr8 * (.expr9 * log(w))
.value
}

Now use grModg instead of grModel.
> -------- Forwarded Message --------
> From: Kari Ruohonen <kari.ruohonen at utu.fi>
> To: r-help at r-project.org
> Subject: Error message from nlmer
> Date: Thu, 25 Oct 2007 15:31:46 +0300
>
> Hi,
>
> I have R 2.6.0 with updated lme4 and Matrix packages, and I am trying to
> fit a nonlinear multilevel model. I get the following error message:
>
> Error in nlmer(f ~ grModel(x, w, Tmin, Tmax, Topt, kopt, m) ~ kopt |
> flat,  :
>   gradient attribute of evaluated model must be a numeric matrix
>
> and I wonder what this may indicate.
>
> The nonlinear model I try to fit is as follows:
>
> > grModel
> function(x,w,Tmin,Tmax,Topt,kopt,m)((x-Tmin)*(x-Tmax))/((x-Tmin)*(x-Tmax)-(x-Topt)^2)*kopt*w^m
>
> and my call of nlmer is as follows:
>
> nlmr1<-nlmer(feed~grModel(temp,MW,Tmin,Tmax,Topt,kopt,m)~kopt|
> flat,data=egi,start=c(Tmin=0.2,Tmax=21.1,Topt=14.6,kopt=2.5,m=.51))
>
> 'flat' is a factor with 11 levels.
>
> My start values are from nls that fits the model with no errors. The
> call I have used is as follows:
>
> nls1<-nls(feed~grModel(temp,MW,Tmin,Tmax,Topt,kopt,m),data=egi,start=c(Tmin=1,Tmax=20,Topt=13,kopt=3,m=.7))
>
> The examples given on the help page of nlmer run with no errors.
>
> I would appreciate any hints and help to track the problem indicated by
> the error message.
>
> Thanks,
>
> Kari
>
> _______________________________________________
> R-sig-mixed-models at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
>

```