cholInvArray: Precision matrices to variance matrices for Winbugs output

Description Usage Arguments Details Value Examples

View source: R/cholInvArray.R

Description

Given an array containing simulations from the posterior of a precision matrix, each individual precision matrix is converted to variances, covariances, and correlations.

Usage

1
cholInvArray(x, prefix = "T", chol=FALSE)

Arguments

x

An array of winbugs output, with precision matrix entries of the form "T[1,3]"

prefix

The name of the precision matrix in winbugs, the "T" in "T[1,2 ]"

chol

If TRUE, the cholesky decomposition is returned instead of the inverse

Details

Inverts the matrices with the cholesky decomposition, but operating on all matrices simultaneously using array arithmetic.

Value

An array with the third dimension's precision matrix entries changed to

"sdT[i,i]"

for the standard deviation of component i

"covT[i,j]"

for the covariance between i and j

"corrT[i,j]"

for the correlations between i and j

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# create a random positive definite matrix by 
# generating a lower triangle
  N=4
  lmat = diag(runif(N, 1, 10))
  thetri = lower.tri(lmat)
  lmat[thetri] = rnorm(sum(thetri), 0, 2)
#  precmat = solve(lmat %*% t(lmat))
  precmat = solve(lmat %*% t(lmat))
 
# put this matrix into an array 
  precarray = array(c(precmat), dim=c(1,1,length(precmat)))
  dimnames(precarray) = list(NULL, NULL, 
    paste("T[", rep(1:N, N), ",", rep(1:N, rep(N,N)), "]",sep="") )

# invert it with cholInvArray and the solve function
  cholInvArray(precarray)[1,1,]
  # the off diagonals of solve(precmat) should be 
  # the covT elements of cholInvArray(precarray)
  solve(precmat)
  # the standard deviations in cholInvArray(precarray) should be the 
  # root of the diagonals of solve(precmat)
  sqrt(diag(solve(precmat)))

glmmBUGS documentation built on May 2, 2019, 6:08 p.m.