I get the following error message whenever I am trying to create a markdown in R:
"Error in jags.model(model.file, data = data, inits = inits.values, n.chains = n.chains, : Nothing to compile calls: <Anonymous>... withVisible -> eval -> jags -> jags.model ->. Call In addition: Warning message: In sink() : no sink to remove Execution halted."
The code runs fine, the problem is just when I try to knit it. The piece of code I have is this one:
library(R2jags)
setwd("~/[...]")
getwd()
#Model:
sink("model1.txt")
cat("
model
{
for(i in 1:N){
y[i] ~ dnorm(mu[i],tau)
mu[i] <- alpha + beta * (x[i]-x.bar)
}
alpha ~ dnorm(0, 0.0001)
beta ~ dnorm(1,1)
tau ~ dgamma(.25,.25)
sigma <- 1/sqrt(tau)
}
",fill = TRUE)
sink()
#Data:
x = c(1,2,3,4,5)
y = c(1,3,3,3,5)
N = 5
x.bar = 3
jags.data = list("x","y","N","x.bar")
#Parameters:
jags.params = c("alpha", "beta", "tau", "sigma")
#Initial Values:
jags.inits = function(){
list("alpha" = 0, "beta" = 1, "tau" = 1)
}
#Fit Model:
lab1.sim = jags(jags.data, jags.inits, jags.params,
model.file = "model1.txt",
n.chains = 3, n.iter = 11000, n.burnin = 1000)
I use Windows 10, Rx64 3.2.3 and RStudio 0.99.903.
When you try to knit your markdown, it runs the sink to create the .txt with the model code and overwrites it. Markdown will mess up the .txt and make it an empty file each time subsequent time you knit it. So there is nothing to compile there and you get that error.
Solution: run sink(...) to create your .txt on the console and comment it out on the markdown source.