winbugs14 - How to code a matrix in WinBUGS? -
i trying code 2x2 matrix sigma 4 elements. not sure how code in winbugs. goal posterior p's, means , variances , create ellipse region covered 2 posterior p's. heres code below:
model{ #likelihood for(j in 1 : nf){ p1[j, 1:2 ] ~ dmnorm(gamma[1:2], t[1:2 ,1:2]) (i in 1:2){ logit(p[j,i]) <- p1[j,i] y[j,i] ~ dbin(p[j,i],n) } x_mu[j,1]<-p[j,1]-mean(p[,1]) x_mu[j,2]<-p[j,2]-mean(p[,2]) v1<-sd(p[,1])*sd(p[,1]) v2<-sd(p[,2])*sd(p[,2]) v12<-(inprod(x_mu[j,1],x_mu[j,2]))/(sd(p[,1])*sd(p[,2])) sigma[1,1]<-v1 sigma[1,2]<-v12 sigma[2,1]<-v12 sigma[2,2]<-v2 sigmainv[1:2, 1:2] <- inverse(sigma[,]) t1[j,1]<-inprod(sigmainv[1,],x_mu[j,1]) t1[j,2]<-inprod(sigmainv[2,],x_mu[j,2]) ell[j,1]<-inprod(x_mu[j,1],t1[j,1]) ell[j,2]<-inprod(x_mu[j,2],t1[j,2]) } #priors gamma[1:2] ~ dmnorm(mn[1:2],prec[1:2 ,1:2]) expit[1] <- exp(gamma[1])/(1+exp(gamma[1])) expit[2] <- exp(gamma[2])/(1+exp(gamma[2])) t[1:2 ,1:2] ~ dwish(r[1:2 ,1:2], 2) sigma2[1:2, 1:2] <- inverse(t[,]) rho <- sigma2[1,2]/sqrt(sigma2[1,1]*sigma2[2,2]) } # data list(nf =20, mn=c(-0.69, -1.06), n=60, prec = structure(.data = c(.001, 0, 0, .001),.dim = c(2, 2)), r = structure(.data = c(.001, 0, 0, .001),.dim = c(2, 2)), y= structure(.data=c(32,13, 32,12, 10,4, 28,11, 10,5, 25,10, 4,1, 16,5, 28,10, 21,7, 19,9, 18,12, 31,12, 13,3, 10,4, 18,7, 3,2, 27,5, 8,1, 8,4),.dim = c(20, 2))
you have specify each element in turn. can use inverse
function (rather solve
) invert matrix.
model{ sigma[1,1]<-v1 sigma[1,2]<-v12 sigma[2,1]<-v21 sigma[2,2]<-v2 sigmainv[1:2, 1:2] <- inverse(sigma[,]) }
Comments
Post a Comment