Hi agian,
I would like to see how nimbleHMC packageworks for different examples, so I ran the package in an example attached below:
library(nimbleHMC)
set.seed(1)
p <- 15 # number of explanatory variables
n <- 100 # number of observations
X <- matrix(rnorm(p*n), nrow = n, ncol = p) # explanatory variables
true_betas <- c(c(0.1, 0.2, 0.3, 0.4, 0.5), rep(0, p-5)) # coefficients
sigma <- 1
y <- rnorm(n, X %*% true_betas, sigma)
library(nimble, warn.conflicts = FALSE)
dnorm_vec <- nimbleFunction( ## Define the distribution
run = function(x = double(1), mean = double(1), sd = double(0), log = integer(0, default = 0)) {
returnType(double(0))
logProb <- sum(dnorm(x, mean, sd, log = TRUE))
if(log) return(logProb)
else return(exp(logProb))
})
rnorm_vec <- nimbleFunction( ## Define a simulation function, optionally.
run = function(n = integer(0), mean = double(1), sd = double(0)) {
returnType(double(1))
if(n != 1) print("rnorm_vec only allows n = 1; using n = 1.")
smp <- rnorm(n, mean, sd)
return(smp)
})
code <- nimbleCode({
beta0 ~ dnorm(0, sd = 100)
beta[1:p] ~ dmnorm(zeros[1:p], omega[1:p, 1:p])
sigma ~ dunif(0, 100) # prior for variance components based on Gelman (2006)
linpred[1:n] <- beta0 + x[1:n, 1:p] %*% beta[1:p]
y[1:n] ~ dnorm_vec(linpred[1:n], sigma)
})
constants <- list(n = n, p = p, x = X, zeros = rep(0, p), omega = 0.0001 * diag(p))
data <- list(y = y)
inits <- list(beta0 = mean(y), beta = rep(0, p), sigma = 0.5)
model <- nimbleModel(code, constants = constants, data = data, inits = inits,buildDerivs = TRUE)
Defining model
[Note] Distribution dnorm_vec does not appear to support derivatives. Set buildDerivs = TRUE (or to a list) in its nimbleFunction to turn on derivative support.
Building model
Setting data and initial values
Running calculate on model
[Note] Any error reports that follow may simply reflect missing values in model variables.
Checking model sizes and dimensions
HMC <- buildHMC(model)
Error: Failed to create the shared library. Run 'printErrors()' to see the compilation errors.
Cmodel <- compileNimble(model)
Error: Compiled C++ model not available; please include the model in your compilation call (or compile it in advance).
CHMC <- compileNimble(HMC, project = model)
samples <- runMCMC(CHMC, niter = 1000, nburnin = 500)
Could you please advice in this regards?