hi guys, there are some issues in my code, the normal distribution with mean as matrix and variance as vactor
dat<-list(rt=c( 1, 9, 2, 1, 10, 1, 1, 90,2216),
nt=c(40,135,200,48,150,59,25,1159,29011),
rc=c( 2, 23, 7, 1, 8, 9, 3, 118,2103),
nc=c(36,135,200,46,148,56,23,1157,29039)
)
options(mc.cores = parallel::detectCores())
dataList = list(
Ntotal = 9,
y = log( ((dat$rt+.5) / (dat$nt-dat$rt+.5) ) / ((dat$rc+.5) / (dat$nc-dat$rc+.5) ) ), # log -odds-ratios
sigma2 = 1/(dat$rt+.5) + 1/(dat$nt-dat$rt+.5) + 1/(dat$rc+.5) + 1/ (dat$nc-dat$rc+.5), # variances
s02 = 1/mean( (1/( 1/(dat$rt+.5) + 1/(dat$nt-dat$rt+.5) + 1/(dat$rc+.5) + 1/ (dat$nc-dat$rc+.5)))[1:8]),
yy1= log( ((dat$rt+.5) / (dat$nt-dat$rt+.5) ) / ((dat$rc+.5) / (dat$nc-dat$rc+.5) ) ),
yy2= log( ((dat$rt+.5) / (dat$nt-dat$rt+.5) ) / ((dat$rc+.5) / (dat$nc-dat$rc+.5) ) ),
yy3= log( ((dat$rt+.5) / (dat$nt-dat$rt+.5) ) / ((dat$rc+.5) / (dat$nc-dat$rc+.5) ) ),
yy4= log( ((dat$rt+.5) / (dat$nt-dat$rt+.5) ) / ((dat$rc+.5) / (dat$nc-dat$rc+.5) ) ),
yy5= log( ((dat$rt+.5) / (dat$nt-dat$rt+.5) ) / ((dat$rc+.5) / (dat$nc-dat$rc+.5) ) ),
yy6= log( ((dat$rt+.5) / (dat$nt-dat$rt+.5) ) / ((dat$rc+.5) / (dat$nc-dat$rc+.5) ) ),
sigma29=(1/(dat$rt+.5) + 1/(dat$nt-dat$rt+.5) + 1/(dat$rc+.5) + 1/ (dat$nc-dat$rc+.5))[9]
)
modelString1 = "
data{
int<lower=0> Ntotal;
vector[Ntotal] y;
real<lower=0> sigma2[Ntotal];
real<lower=0> s02;
real<lower=0> sigma29;
}
parameters {
real<lower=0,upper=1> pfix[Ntotal];
real<lower=-10,upper=10> mu[6];
matrix[6, 8] theta;
vector[6] thetanew_raw;
vector[6] ynew_raw;
real<lower=0> tau21;
real<lower=0,upper=50> tau22;
real<lower=0,upper=50> tau23;
real<lower=0,upper=1> B0;
real<lower=0,upper=1> D0;
real<lower=0> tau26;
}
transformed parameters {
real<lower=0> tau[6];
matrix[6, 8] yy;
vector[6] thetanew;
vector[6] ynew;
tau[1] = 1/sqrt(tau21);
tau[2] = sqrt(tau22);
tau[3] = tau23;
tau[4] = sqrt(s02 * (1-B0)/B0);
tau[5] = sqrt(s02) * (1-D0) / D0;
tau[6] = tau26;
for(j in 1:6){
for( k in 1 : 8 ) {
yy[j,k]=y[k];
}
thetanew = mu[j] + thetanew_raw*tau[j] ;
}
ynew = thetanew + ynew_raw*sqrt(sigma29);
}
model
{
tau21 ~ gamma(0.001,0.001);
tau22 ~ uniform(0,50);
tau23 ~ uniform(0,50);
B0 ~ uniform(0,1);
D0 ~ uniform(0,1);
tau26 ~ normal(0,sqrt(3.84))T[0,];
thetanew_raw ~ std_normal();
ynew_raw ~ std_normal();
for(j in 1:6){
for( k in 1 : 8 ) {
target += normal_lpdf(theta[j,k] | mu[j],tau[j]);
target += normal_lpdf(yy[j,k] | theta[j,k],sigma2[k]) ;
}
target += normal_lpdf( thetanew[j]|mu[j],tau[j]);
target += normal_lpdf( ynew[j] |thetanew[j],sqrt(sigma29));
}
}
generated quantities {
real<lower=0> OR[6];
for (j in 1:6) {
OR[j]=exp(mu[j]);
}
}
"
writeLines( modelString1 , con="TEMPmodel.stan" )
stanModel <- stan(
data = dataList ,
file = "TEMPmodel.stan",
chains = 4,
warmup = 5000,
iter = 25000,
control = list(adapt_delta = 0.99)
)
There are several variance divergent transitions issues which I have no idea how to fix, I think the manual only mention the variance as matrix not the mean as matirx
2: There were 9 divergent transitions after warmup. See
http://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
to find out why this is a problem and how to eliminate them.
3: Examine the pairs() plot to diagnose sampling problems