Roger Levy
2010-Mar-26 05:57 UTC
[R] cacheSweave fails when used in conjunction with rjags
Hi all, I use the excellent packages rjags and cacheSweave, and unfortunately seem to have found an incompatibility between them. Below are a minimal .Rnw file and corresponding JAGS model file which illustrate the problem: *** JAGS model file; name=j.bug *** model { mu ~ dnorm(0,1.0E-5) sigma ~ dunif(0,100) for(i in 1:length(y)) { y[i] ~ dnorm(mu,sigma) } } *** *** .Rnw file; name=testCacheSweave-jags.Rnw *** \documentclass[12pt]{article} \usepackage{Sweave} \begin{document} <<loadLib>>library(rjags) @ <<testJags,cache=T>>y <- rnorm(100) m <- jags.model("j.bug") update(m,100) res <- coda.samples(m,c("mu","sigma"),n.iter=1000) @ <<plotResults,fig=T>>plot(res) @ \end{document} *** When I run Sweave with cacheSweaveDriver, I get the following error (note that this happens on the first invocation, not just on repeat invocations): > Sweave("testCacheSweave-jags.Rnw",driver=cacheSweaveDriver) Writing to file testCacheSweave-jags.tex Processing code chunks ... 1 : echo term verbatim (label=loadLib) 2 : echo term verbatim (label=testJags) Error: chunk 2 (label=testJags) Error in jags.model("j.bug") : RUNTIME ERROR: Unable to evaluate upper index of counter i Reading through the cacheSweave documentation, it remains unclear to me why this error would occur. Would anyone be able to clarify, and/ or suggest a possible workaround? It's an unfortunate conflict because running JAGS models is one of the most computation-intensive operations that I have in my .Rnw document. Best & many thanks in advance. Roger -- Roger Levy Email: rlevy at ling.ucsd.edu Assistant Professor Phone: 858-534-7219 Department of Linguistics Fax: 858-534-4789 UC San Diego Web: http://ling.ucsd.edu/~rlevy
Martyn Plummer
2010-Mar-29 08:32 UTC
[R] cacheSweave fails when used in conjunction with rjags
Hi Roger, If you do not supply an explicit data list, the jags.model() function will try to get the data from the calling environment. This is obviously not working with cacheSweave, but if you do supply an explicit list, like this: m <- jags.model("j.bug", data=list("y"=y)) it will work. Martyn On Thu, 2010-03-25 at 22:57 -0700, Roger Levy wrote:> Hi all, > > I use the excellent packages rjags and cacheSweave, and unfortunately > seem to have found an incompatibility between them. Below are a > minimal .Rnw file and corresponding JAGS model file which illustrate > the problem: > > *** JAGS model file; name=j.bug *** > model { > mu ~ dnorm(0,1.0E-5) > sigma ~ dunif(0,100) > for(i in 1:length(y)) { > y[i] ~ dnorm(mu,sigma) > } > } > *** > > *** .Rnw file; name=testCacheSweave-jags.Rnw *** > \documentclass[12pt]{article} > \usepackage{Sweave} > > \begin{document} > > <<loadLib>>> library(rjags) > @ > > <<testJags,cache=T>>> y <- rnorm(100) > m <- jags.model("j.bug") > update(m,100) > res <- coda.samples(m,c("mu","sigma"),n.iter=1000) > @ > > <<plotResults,fig=T>>> plot(res) > @ > \end{document} > *** > > > When I run Sweave with cacheSweaveDriver, I get the following error > (note that this happens on the first invocation, not just on repeat > invocations): > > > Sweave("testCacheSweave-jags.Rnw",driver=cacheSweaveDriver) > Writing to file testCacheSweave-jags.tex > Processing code chunks ... > 1 : echo term verbatim (label=loadLib) > 2 : echo term verbatim (label=testJags) > > Error: chunk 2 (label=testJags) > Error in jags.model("j.bug") : RUNTIME ERROR: > Unable to evaluate upper index of counter i > > > Reading through the cacheSweave documentation, it remains unclear to > me why this error would occur. Would anyone be able to clarify, and/ > or suggest a possible workaround? It's an unfortunate conflict > because running JAGS models is one of the most computation-intensive > operations that I have in my .Rnw document. > > Best & many thanks in advance. > > Roger > > > -- > > Roger Levy Email: rlevy at ling.ucsd.edu > Assistant Professor Phone: 858-534-7219 > Department of Linguistics Fax: 858-534-4789 > UC San Diego Web: http://ling.ucsd.edu/~rlevy > > > > > > >----------------------------------------------------------------------- This message and its attachments are strictly confidenti...{{dropped:8}}