Search code examples
rsweavejags

How to compile a Latex document in Sweave without having to run rjags everytime?


I am compiling a pdf using sweave and latex in r. I am running rjags for MCMC methods. Running these models take approximately an hour to converge. Every time I run my sweave code to compile a pdf, it also runs all the jags models again. This makes editing and finding out if I have made small syntax errors a pain if it takes an hour to compile the pdf. How do I keep all my variables generated by my jags code but not have sweave have to evaluate every time? The data can be found here: https://uwyo-files.instructure.com/courses/481850/files/36253354/course%20files/project4_genomebinom/chrgc.txt?download=1&inline=1&sf_verifier=ea3569eec1ca938fad4122a92e35ff57&ts=1462863980&user_id=569842

Here is some sample code

 \documentclass[12pt, letterpaper]{article}
 \begin{document}

<<computation,results=hide>>=
humangc <- read.csv("c:\\temp\\RtmpYpMfSP\\data15a4519241c1")
chr<-substr(humangc$chr, 4, 8)
chr[chr=='X']<-23
chr[chr=='Y']<-24
chr<-as.numeric(chr)
humangc<-data.frame(humangc[,-1], chr=chr)
humangc<-humangc[order(humangc$chr, humangc$bp),]  ### reorder data by chr

## drop NA data and blocks with fewer than 100000 (10%) valid data
humangc<-humangc[!is.na(humangc$valid) & humangc$valid > 100000,]


 ### hierarchical Bayesian model in JAGS
 bin.beta.beta<-"
 model{
 for(i in 1:bins){
 gc[i] ~ dbinom(p[i], n[i])
 p[i] ~ dbeta(chrgc * chrprec, (1-chrgc)*chrprec)   
}

chrgc ~ dbeta(1,1)  ## chrgc is same as pi
chrprec ~ dunif(0.001,10000)  ## chrprec is same as theta
}
"
require(rjags)

for(i in 1:24){
data.jags<-list(gc=humangc$gc[humangc$chr==i],
                n=humangc$valid[humangc$chr==i],
                bins=length(humangc$gc[humangc$chr==i]) )

mod.jags<-   jags.model(textConnection(bin.beta.beta),data=data.jags,n.chains=3,n.adapt=1000)

mod.samples<-jags.samples(model=mod.jags, variable.names=c("chrgc", "chrprec"), n.iter=5000,thin=2)
###  summarize quantiles of beta and p-values of empirical obs from Beta

gcest<-NULL

gcest$q<-qbeta(c(0.025, 0.5, 0.975),
               mean(mod.samples$chrprec[1,,] * mod.samples$chrgc[1,,]),
               mean(mod.samples$chrprec[1,,] * (1-mod.samples$chrgc[1,,])) )
gcest$p<-pbeta(humangc$perc[humangc$chr==i], 
               mean(mod.samples$chrprec[1,,] * mod.samples$chrgc[1,,]),
               mean(mod.samples$chrprec[1,,] * (1-mod.samples$chrgc[1,,])) )
gcest$perc <- humangc$perc[humangc$chr==i]
gcest$bp <- humangc$bp[humangc$chr==i]
## write workspace for chromosome to disk
save.image(paste("Rworkspace_chr", i, sep=""))
  }
@


<<echo=F, fig=T, include=F>>=
update(mod.jags)
require(coda)
params <- c("chrgc", "chrprec")
samps <- coda.samples(mod.jags, params, n.iter = 2000)
plot(samps)
@
SOME TRIVIAL TEXT!!!!!!
end{document}

As you can see I am creating 24 different models. Which takes a little while. How do I get "SOME TRIVIAL TEXT!!!!!!!" to show up quick when I compile the pdf, given that I need the variables created by jags?

enter image description here

The step above takes a while.


Solution

  • Have a look at the cache chunk option. You can store results of a chunk the first time it is run in an R database file. On running again cached chunks are skipped. A more thorough description of what is possible can be found here.