如何在Sweave中编译Latex文档而不必每次都运行rjags?

时间:2016-05-10 07:06:56

标签: r sweave jags

我正在使用sweave中的latexr编译pdf。我正在为MCMC方法运行rjags。运行这些模型大约需要一个小时才能收敛。每次运行sweave代码编译pdf时,它都会再次运行所有jags模型。如果编译pdf需要一个小时,这使编辑和查找我是否已经使语法错误很小。如何保留我的jags代码生成的所有变量,但不是每次都要sweave进行评估? 数据可在此处找到: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

以下是一些示例代码

 \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}

正如您所看到的,我正在创建24种不同的模型。这花了一点时间。我如何得到&#34;一些传统的文字!!!!!!!&#34;在编译pdf时快速显示,因为我需要jags创建的变量?

enter image description here

上述步骤需要一段时间。

1 个答案:

答案 0 :(得分:1)

查看((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);块选项。您可以在第一次在R数据库文件中运行时存储块的结果。在再次运行时,将跳过缓存的块。可以找到有关可能性的更全面的描述here