title: nlmer error: Error in initializePtr() : Downdated VtV is not positive definite
body: <p>I have repeated measures data examining forest <code>Biomass</code> across forest <code>Age</code> for multiple <code>Plots</code>: </p>
  
  <p>In other words: <code>Biomass ~ Age | Plot</code> </p>
  
  <pre><code>#Reproducible Example data:
  
  biom.dat &lt;- c(unlist(lapply(1:7, function(x){ set.seed(x) ; sort(sample(2:500,15))})),
    unlist(lapply(8:15, function(x){ set.seed(x) ;  c(sort(sample(2:500,12)),(sample(90:500,3)))})))
  dat &lt;- data.frame(Plot = rep(1:5,each = 15), Age = rep(c((1:75)[(1:5)%%5 == 0])+8,5), Biomass = biom.dat)
  
  &gt; head(dat)
    Plot Age Biomass
  1    1  13      32
  2    1  18      88
  3    1  23     101
  4    1  28     102
  5    1  33     134
  6    1  38     187
  </code></pre>
  
  <p>I tried using <code>nlmer</code> to examine a non-linear trend in this data. I use <code>nlmer</code>  so I incorporate the <code>Plot</code> structure as a random effect.</p>
  
  <p>I'm trying to run an extension of the model seen <a href="https://stackoverflow.com/q/50182884/4581200">here</a> that uses this custom function: </p>
  
  <p><a href="https://i.stack.imgur.com/WcjCd.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WcjCd.gif" alt="enter image description here"></a></p>
  
  <p>which I've coded in R as:</p>
  
  <pre><code>function(Age,C,Ao,s,wd,ph) C + Ao * exp(-s*Age) * cos(wd*Age + ph)
  </code></pre>
  
  <p>However, when I run the <code>nlmer</code> code (see below), <strong>I get the following error</strong>:</p>
  
  <pre><code>Error in initializePtr() : Downdated VtV is not positive definite
  </code></pre>
  
  <p><a href="https://github.com/lme4/lme4/issues/173" rel="nofollow noreferrer">This discussion</a> suggested to me that I might have a scaling problem? (which was hinted at <a href="https://stackoverflow.com/a/50184567/4581200">here</a>).</p>
  
  <p>So I tried scaling the variables (1st just the <code>Age</code> predictor, but then also the <code>Biomass</code> response). Even after scaling both variables, I still get the same error... </p>
  
  <p><strong>Questions:</strong></p>
  
  <ol>
  <li><p>What else could be going wrong?  (What else should I check?)</p></li>
  <li><p>Where do I go from here?</p></li>
  </ol>
  
  <hr>
  
  <p><strong>My Code:</strong></p>
  
  <p><strong>Update:</strong> Based on deleted @42 comments, I've adjusted the code (originally inspired by <a href="https://stackoverflow.com/a/16868452/4581200">here</a>) to more closely match code from the <code>nlmer</code> <a href="https://www.rdocumentation.org/packages/lme4/versions/1.1-17/topics/nlmer" rel="nofollow noreferrer">help page</a> (Example #3):  <code>[Note: the errors still occur]</code></p>
  
  <pre><code>  #Create equation:
      LTI.eqn &lt;- ~ C + Ao * exp(-s*Age) * cos(wd * Age + ph)
  
    #Build function (incorporating a "gradient" attribute in the formula using deriv():
      LTI.func &lt;- deriv(LTI.eqn, namevec = c('C', 'Ao', 's', 'wd', 'ph'),
                    function.arg = c('Age', 'C', 'Ao', 's', 'wd', 'ph'))
  
    #Load package:  
      library(lme4)
  
    #Create named vector of starting values for my constants:
      nlmer.start &lt;- c(C = 0,Ao = -1,s = 1,wd = 1,ph = 0)
  
    #Run model:
      nlmer(Biomass ~ LTI.func(Age, C, Ao, s, wd, ph) ~ (Age | Plot), 
            data = dat, start=nlmer.start)
  
       #Error in initializePtr() : Downdated VtV is not positive definite
  
    #So let's rescale the continuous predictor variables (in this case we'll center Age):
      nlmer(Biomass ~ LTI.func(Age, C, Ao, s, wd, ph) ~ (Age | Plot), 
            data = transform(dat,Age = Age - 51), start = nlmer.start)
  
       #Error in initializePtr() : Downdated VtV is not positive definite
  
    #Still a problem. What if I scale Biomass, too???
      datt &lt;- transform(dat,Age = (Age - 51), Biomass = scale(Biomass, center = F))
      nlmer(Biomass ~ LTI.func(Age, C, Ao, s, wd, ph) ~ (Age | Plot),
            data = datt, start = nlmer.start)
  
       #Error in initializePtr() : Downdated VtV is not positive definite
  </code></pre>