[R] rjags error. Error parsing model file:,syntax error on line 5 near ""

David Kaplan dkaplan at education.wisc.edu
Mon Aug 13 16:11:28 CEST 2012


Greetings,  I am running a factor analysis model in rjags and have 
received the following error

Error parsing model file: syntax error on line 5 near ""

Line 5 is model {

Here is my syntax.  Any insight would be greatly appreciated.

require(rjags)


modelstring = "
model {
for (i in 1 : nData) {
     for (j in 1 : nIndicators) {
y[i,j] ~ dnorm(mu[i,j],psi[j])
ephat[i,j] <- y[i,j] - mu[i,j]
}

mu[i,1] <-  alpha[1]+xi[i,1]                     # Factor 1
mu[i,2] <- alpha[2]+lam[2]*xi[i,1]
mu[i,3] <- alpha[3]+lam[3]*xi[i,1]
mu[i,4] <- alpha[4]+lam[4]*xi[i,1]
mu[i,5] <- alpha[5]+lam[5]*xi[i,1]
mu[i,6] <- alpha[6]+lam[6]*xi[i,1]
mu[i,7] <- alpha[7]+lam[7]*xi[i,1]
mu[i,8] <- alpha[8]+lam[8]*xi[i,1]

mu[i,9]  <- alpha[9]+xi[i,2]                    # Factor 2
mu[i,10] <- alpha[10]+lam[10]*xi[i,2]
mu[i,11] <- alpha[11]+lam[11]*xi[i,2]
mu[i,12] <- alpha[12]+lam[12]*xi[i,2]
mu[i,13] <- alpha[13]+lam[13]*xi[i,2]]
mu[i,14] <- alpha[14]+lam[14]*xi[i,2]
mu[i,15] <- alpha[15]+lam[15]*xi[i,2]

xi[i,1:2] ~ dmnorm(u[1:2],phi[1:2,1:2])
}
for (j in 1:nIndicators) {
alpha[j] ~ dnorm(0.0, 1.0)
}


#---------------------------------------
# Distributions and Priors


#Priors on Loadings
lam[2] ~ dnorm(0,psi[2])
lam[3] ~ dnorm(0,psi[3])
lam[4] ~ dnorm(0,psi[4])
lam[5] ~ dnorm(0,psi[5])
lam[6] ~ dnorm(0,psi[6])
lam[7] ~ dnorm(0,psi[7])
lam[8] ~ dnorm(0,psi[8])
lam[10] ~ dnorm(0,psi[10])
lam[11] ~ dnorm(0,psi[11])
lam[12] ~ dnorm(0,psi[12])
lam[13] ~ dnorm(0,psi[13])
lam[14] ~ dnorm(0,psi[14])
lam[15] ~ dnorm(0,psi[15])
#---------------------------------------
#Priors on Precisions

for(j in 1:nIndicators) {
psi[j] ~ dgamma(9.0, 4.0) # Error variances
sgm[j] <- 1/psi[j]
}


phi[1:2,1:2] ~ dwish(R[1:2,1:2], 5) # Precision matrix
phx[1:2,1:2] <- inverse(phi[1:2,1:2]) # Variance/Covariance matrix

}
"
writeLines(modelstring,con="model.txt")
#--------------------------------------------------------
# READ IN DATA AND PREPARE FOR JAGS

cfadata = read.csv("~/Desktop/rjags stuff/nelsdata.csv",header=T)
nData = NROW(cfadata)
yname = c("getalong", "spirit", "discfair", "friends", "tchgood", 
"tcherint", "tchprais", "tlisten",
             "studisr", "tputdown", "strict", "sputdown", "notsafe", 
"dislrn", "getaway")
y = as.matrix(cfadata)
nData=NROW(y)
nIndicators = NCOL(y)
cfadata <- list(y=y, nData=nData)

#-----------------------------------------------------------
# A set of init statements goes here, but I haven't done that.

#-----------------------------------------------------------
# RUN CHAIN

parameters = c("lam[2]","lam[3]","lam[4]")
adaptSteps = 500
burnInSteps = 500
nChains = 3
numSavedSteps = 50000
thinSteps = 1

cfaModel = jags.model("model.txt",data=cfadata,
                         n.chains=nChains, n.adapt=adaptSteps)

# Burn-in:
cat("Burning in the MCMC chain ...\n")
update(cfaModel, n.iter=burnInSteps)
cat("Sampling from the final MCMC chain ... \n")
codaSamples = coda.samples(cfaModel, variable.names=parameters,
                 n.iter=nPerChain, thin=thinSteps)


############################################################


-- 

==============================================================================
David Kaplan, Ph.D.
Professor and Chair
Department of Educational Psychology
University of Wisconsin - Madison
Educational Sciences, Room, 880B
1025 W. Johnson Street
Madison, WI 53706

email: dkaplan at education.wisc.edu
homepage: edpsych.education.wisc.edu/people/faculty-staff/david-kaplan
Project page: bise.wceruw.org
Phone: 608-262-0836



More information about the R-help mailing list