model {
  for(i in 1:N) {
  #measurement model
    for(j in 1:P) {
      y[i,j]~dnorm(mu[i,j],errorprec[j])
    }
    mu[i,1]<-lam[1]*xi[i,1]
    mu[i,2]<-lam[2]*xi[i,1]
    mu[i,3]<-lam[3]*xi[i,1]
    mu[i,4]<-lam[4]*xi[i,2]
    mu[i,5]<-lam[5]*xi[i,2]
    mu[i,6]<-lam[6]*xi[i,2]
  #structural model
    xi[i,1:2]~dmnorm(u[1:2],latprec[1:2,1:2])
  } #end of i
#priors on loadings
  lam[1]~dnorm(0,0.0001)
  lam[2]~dnorm(0,0.0001)
  lam[3]~dnorm(0,0.0001)
  lam[4]~dnorm(0,0.0001)
  lam[5]~dnorm(0,0.0001)
  lam[6]~dnorm(0,0.0001)
#priors on errors
  for(j in 1:P) {
    errorprec[j] ~ dgamma(1, 1)
    errorvar[j]<-1/errorprec[j]
  }
#priors on latent (co-)variances
  latprec[1:2,1:2] ~ dwish(V[,], 4)
  latcov[1:2,1:2] <- inverse(latprec[,])
  latcor <- latcov[1,2]/(sqrt(latcov[1,1])*sqrt(latcov[2,2]))
  V[1,1] <- 1
  V[1,2] <- 0
  V[2,1] <- 0
  V[2,2] <- 1
  u[1] <- 0
  u[2] <- 0
} #end of model

