Dear Nimble users,
I try to fit a model for the system x environment x variety data. The model can be written as
y[i]~dnorm(mu[i],tau[sys[i]]) , where mu[i] is equal to:
mu[i]<-b[env[i],rep[i],sys[i]]+va[v[i],sys[i]]+ge[env[i],v[i],sys[i]].
I fitted the model in WinBUGS (see the code below)
library(R2WinBUGS)
library(coda)
trial<-read.table("D:/starykomp/programy/old/convvsorgZIK/proba/proba.txt",h=TRUE)
#trial
#length(trial$yi)
N<-length(trial$yield)
n<-length(trial$yield)/2
ne<-6
nv<-11
nr<-3
#### organic
y1<-trial$yi[1:n]/10
rep1<-trial$rep[1:n]
si1<-trial$si[1:n]
v1<-trial$var[1:n]
va1<-var(y1);va1
#### conventional
y2<-trial$yi[(n+1):N]/10
rep2<-trial$rep[(n+1):N]
si2<-trial$si[(n+1):N]
v2<-trial$var[(n+1):N]
va2<-var(y2);va2
R<-matrix(nrow=2,ncol=2)
R[1,1]<-va1/2
R[1,2]<-0
R[2,1]<-0
R[2,2]<-va2/2
Rinv<-solve(R)
R2<-diag(1,2)
y<-trial$yield/10
sys<-trial$sys
env<-trial$env
rep<-trial$rep
v<-trial$var
model<-function(){
for(i in 1:N){
y[i]~dnorm(mu[i],tau[sys[i]])
mu[i]<-b[env[i],rep[i],sys[i]]+va[v[i],sys[i]]+ge[env[i],v[i],sys[i]]
}
for(l in 1:2){a[l]~dnorm(0,0.00001)}
for(l in 1:2){tau[l]~dgamma(2,10)
sig2[l]<-1/tau[l]}
taur[1:2,1:2]~dwish(R2[1:2,1:2],2)
tauv[1:2,1:2]~dwish(R2[1:2,1:2],4)
tauge[1:2,1:2]~dwish(R2[1:2,1:2],2)
mu1[1]<-0
mu1[2]<-0
for(j in 1:ne){
for(r in 1:nr){
b[j,r,1:2]~dmnorm(a[1:2],taur[1:2,1:2])#taur[1:2,1:2]
}
}
for(k in 1:nv){
va[k,1:2]~dmnorm(mu1[1:2],tauv[1:2,1:2])
}
covV[1:2,1:2]<-inverse(tauv[1:2,1:2])
for(j in 1:ne){
for(k in 1:nv){
ge[j,k,1:2]~dmnorm(mu1[1:2],tauge[1:2,1:2])
}
}
covGE[1:2,1:2]<-inverse(tauge[1:2,1:2])
covRep[1:2,1:2]<-inverse(taur[1:2,1:2])
}
model.file<-file.path("D:/starykomp/programy/old/convvsorgZIK","model.txt")
write.model(model,model.file)
data<-list("ne","nv","nr","N","rep","sys","env","v","y","R2")#"Rinv",","T"
parameters<-c("a","covV","covRep","covGE","sig2")#"b","v",
model<-bugs(data,inits=NULL,parameters,model.file,
n.chains=3,n.iter=1000000,n.burnin=500000,n.thin=25,
codaPkg=FALSE,debug=TRUE,DIC=TRUE,
bugs.directory = "D:/Programy/winbugs143/winbugs14_full_patched/WinBUGS14/",
working.directory="D:/starykomp/programy/old/convvsorgZIK")#,bugs.seed=1231
print(model,digits=3)
I tried to fit the same model in nimble (the code below)
library(nimble)
library(coda)
library(R2WinBUGS)
trial<-read.table("D:/starykomp/programy/old/convvsorgZIK/proba/proba.txt",h=TRUE)
trial
#length(trial$yi)
### definition of factor in the data set
trial$sys<-as.factor(trial$sys)
#trial$system<-as.factor(trial$system)
#trial$year<-as.factor(trial$year)
#trial$site<-as.factor(trial$site)
trial$var<-as.factor(trial$var)
trial$rep<-as.factor(trial$rep)
trial$env<-as.factor(trial$env)
#trial$s<-as.factor(trial$s)
N<-length(trial$yield)
n<-N/2
ne<-6
nv<-11
nr<-3
me1<-aggregate(yield/10~var,data=trial[1:n,],na.rm=TRUE,FUN="mean")
va1<-var(me1[,2])
me2<-aggregate(yield/10~var,data=trial[(n+1):N,],na.rm=TRUE,FUN="mean")
va2<-var(me2[,2])
R<-matrix(nrow=2,ncol=2)
R[1,1]<-va1/2
R[1,2]<-0
R[2,1]<-0
R[2,2]<-va2/2
Rinv<-solve(R)
R2<-diag(1,2)
y<-trial$yield/10;y
sys<-trial$sys;sys
env<-trial$env
rep<-trial$rep
v<-trial$var;v
model<-nimbleCode({
for(i in 1:N){
y[i]~dnorm(mu[i],tau[sys[i]])
mu[i]<-b[env[i],rep[i],sys[i]]+va[v[i],sys[i]]+ge[env[i],v[i],sys[i]]
}
for(l in 1:2){a[l]~dnorm(0,0.00001)}
for(l in 1:2){tau[l]~dgamma(2,10)
sig2[l]<-1/tau[l]}
cc[1]<-1
cc[2]<-2
R2[1:2,1:2]<-diag(cc[1:2])
taur[1:2,1:2]~dwish(R=R2[1:2,1:2],df=3)
tauv[1:2,1:2]~dwish(R=R2[1:2,1:2],df=4)
tauge[1:2,1:2]~dwish(R=R2[1:2,1:2],df=4)
mu1[1]<-0
mu1[2]<-0
for(j in 1:ne){
for(r in 1:nr){
b[j,r,1:2]~dmnorm(m=a[1:2],prec=taur[1:2,1:2])#taur[1:2,1:2]
}
}
for(k in 1:nv){
va[k,1:2]~dmnorm(mu1[1:2],prec=tauv[1:2,1:2])
}
covV[1:2,1:2]<-inverse(tauv[1:2,1:2])
for(j in 1:ne){
for(k in 1:nv){
ge[j,k,1:2]~dmnorm(mu1[1:2],tauge[1:2,1:2])
}
}
covGE[1:2,1:2]<-inverse(tauge[1:2,1:2])
covRep[1:2,1:2]<-inverse(taur[1:2,1:2])
}
)
modelConsts<-list(N=N,ne=ne,nv=nv,nr=nr,rep=c(rep),env=c(env),sys=c(sys),v=c(v))
modelData<-list(y=c(y))
mod <- nimbleModel(code = model, name = "model", constants = modelConsts,
data = modelData)
myMCMC <- buildMCMC(mod)
myModel <- compileNimble(mod)
mcmc.out <- nimbleMCMC(code = model, constants = modelConsts,
data = modelData,
nchains = 2, niter = 10000,nburnin=2000,
summary = TRUE, WAIC = TRUE,
monitors = c('a','sig2'),
samples=TRUE,samplesAsCodaMCMC=TRUE).
When I've run the model I've obtained the following error:
"> mod <- nimbleModel(code = model, name = "model", constants = modelConsts,
+ data = modelData)
defining model...
building model...
setting data and initial values...
running calculate on model (any error reports that follow may simply reflect missing values in model variables) ... Error in chol.default(model$taur[1:2, 1:2]) :
the leading minor of order 1 is not positive definite
checking model sizes and dimensions... This model is not fully initialized. This is not an error. To see which variables are not initialized, use model$initializeInfo(). For more information on model initialization, see help(modelInitialization).
model building finished."
I assumed Wishart distribution for all precision matrices in my model. Could You tell me where I've made a mistake or what is wrong in my model?
I enclosed my toy data set.
I would be grateful for any help in solving my problem.
Best regards
Marcin