Skip to content

Instantly share code, notes, and snippets.

@ljcolling
Created August 23, 2017 12:35
Show Gist options
  • Select an option

  • Save ljcolling/3dcf2f400ec74d5cce5ee22ac429f4bb to your computer and use it in GitHub Desktop.

Select an option

Save ljcolling/3dcf2f400ec74d5cce5ee22ac429f4bb to your computer and use it in GitHub Desktop.
rjags model string for a multivariate normal distribution with moral
## Takes the standarded data matrix as it's input
## model available from:
## http://doingbayesiandataanalysis.blogspot.co.uk/2017/06/bayesian-estimation-of-correlations-and.html
model {
for ( i in 1:Ntotal ) {
zy[i,1:Nvar] ~ dmnorm( zMu[1:Nvar] , zInvCovMat[1:Nvar,1:Nvar] )
}
for ( varIdx in 1:Nvar ) { zMu[varIdx] ~ dnorm( 0 , 1/2^2 ) }
zInvCovMat ~ dwish( zRmat[1:Nvar,1:Nvar] , zRscal )
# Convert invCovMat to sd and correlation:
zCovMat <- inverse( zInvCovMat )
for ( varIdx in 1:Nvar ) { zSigma[varIdx] <- sqrt(zCovMat[varIdx,varIdx]) }
for ( varIdx1 in 1:Nvar ) { for ( varIdx2 in 1:Nvar ) {
zRho[varIdx1,varIdx2] <- ( zCovMat[varIdx1,varIdx2]
/ (zSigma[varIdx1]*zSigma[varIdx2]) )
} }
# Convert to original scale:
for ( varIdx in 1:Nvar ) {
sigma[varIdx] <- zSigma[varIdx] * sdOrig[varIdx]
mu[varIdx] <- zMu[varIdx] * sdOrig[varIdx] + meanOrig[varIdx]
}
for ( varIdx1 in 1:Nvar ) { for ( varIdx2 in 1:Nvar ) {
rho[varIdx1,varIdx2] <- zRho[varIdx1,varIdx2]
} }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment