I have repeated measures data examining forest Biomass
across forest Age
for multiple Plots
:
In other words: Biomass ~ Age | Plot
#Reproducible Example data:
biom.dat <- 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 <- data.frame(Plot = rep(1:5,each = 15), Age = rep(c((1:75)[(1:5)%%5 == 0])+8,5), Biomass = biom.dat)
> 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
I tried using nlmer
to examine a non-linear trend in this data. I use nlmer
so I incorporate the Plot
structure as a random effect.
I'm trying to run an extension of the model seen here that uses this custom function:
which I've coded in R as:
function(Age,C,Ao,s,wd,ph) C + Ao * exp(-s*Age) * cos(wd*Age + ph)
However, when I run the nlmer
code (see below), I get the following error:
Error in initializePtr() : Downdated VtV is not positive definite
This discussion suggested to me that I might have a scaling problem? (which was hinted at here).
So I tried scaling the variables (1st just the Age
predictor, but then also the Biomass
response). Even after scaling both variables, I still get the same error...
Questions:
What else could be going wrong? (What else should I check?)
Where do I go from here?
My Code:
Update: Based on deleted @42 comments, I've adjusted the code (originally inspired by here) to more closely match code from the nlmer
help page (Example #3): [Note: the errors still occur]
#Create equation:
LTI.eqn <- ~ C + Ao * exp(-s*Age) * cos(wd * Age + ph)
#Build function (incorporating a "gradient" attribute in the formula using deriv():
LTI.func <- 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 <- 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 <- 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