code <- nimbleCode({
b0 ~ dnorm(0, 0.001)
b1 ~ dnorm(0, 0.001)
sigma ~ dunif(0, 10000)
for(i in 1:N) {
mu[i] <- b0 + b1 * x[i]
y[i] ~ dnorm(mu[i], sd = sigma)
}
})
N <- 10
constants <- list(N = N, x = 1:N)
data <- list(y = 1:N)
inits <- list(b0 = 1, b1 = 0.1, sigma = 1)
Rmodel <- nimbleModel(code, constants, data, inits, buildDerivs = TRUE)
## create default MCMC configuration object
conf <- configureMCMC(Rmodel)
## remove default samplers operating on b0 and b1
conf$removeSamplers(c("b0", "b1"))
## add an HMC sampler operating on b0 and b1
addHMC(conf, target = c("b0", "b1"))
Rmcmc <- buildMCMC(conf)
# Cmodel <- compileNimble(Rmodel)
# Cmcmc <- compileNimble(Rmcmc, project = Rmodel)
# samples <- runMCMC(Cmcmc)
Run the code above in your browser using DataLab