我已经针对多个Biomass
重复测量森林Age
中的林Plots
的度量数据:
换句话说: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
我尝试使用nlmer
检查此数据中的非线性趋势。我使用nlmer
,因此我将Plot
结构作为随机效果加入。
我试图运行使用此自定义函数的here模型的扩展名:
我在R中编码为:
function(Age,C,Ao,s,wd,ph) C + Ao * exp(-s*Age) * cos(wd*Age + ph)
但是,当我运行nlmer
代码(见下文)时,我收到以下错误:
Error in initializePtr() : Downdated VtV is not positive definite
This discussion向我建议我可能会出现缩放问题? (暗示here)。
所以我尝试缩放变量(第一个只是Age
预测变量,然后是Biomass
响应。即使在缩放了两个变量之后,我仍然会得到相同的错误......
问题:
还有什么可能出错? (我还应该检查什么?)
我从哪里开始?
我的代码:
更新:根据已删除的@ 42条评论,我调整了代码(最初受here启发)以更接近地匹配nlmer
{的代码{3}}(示例#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