-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheg_nimble.R
More file actions
47 lines (47 loc) · 1.33 KB
/
eg_nimble.R
File metadata and controls
47 lines (47 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
library(nimble)
# set compiler output to true
nimbleOptions(showCompilerOutput = TRUE)
# see ?jags.fit
nfun <- nimbleCode({
for (i in 1:N) {
Y[i] ~ dnorm(mu[i], tau)
mu[i] <- alpha + beta * (x[i] - x.bar)
}
x.bar <- mean(x[])
alpha ~ dnorm(0.0, 1.0E-4)
beta ~ dnorm(0.0, 1.0E-4)
sigma <- 1.0 / sqrt(tau)
tau ~ dgamma(1.0E-3, 1.0E-3)
})
## data generation
set.seed(1234)
N <- 100
alpha <- 1
beta <- -1
sigma <- 0.5
x <- runif(N)
linpred <- crossprod(t(model.matrix(~x)), c(alpha, beta))
Y <- rnorm(N, mean = linpred, sd = sigma)
## list of data for the model
ndata <- list(Y = Y, x = x)
## list of constants for the model
nconst <- list("N" = N)
## what to monitor
npara <- c("alpha", "beta", "sigma")
## optionally, do separate stages
if (0) {
message("remember to delete `working` directory")
mod <- nimbleModel(code = nfun, constants = nconst, data = ndata, name = "mod")
Cmod <- compileNimble(mod, dirName = "./working/")
modConf <- configureMCMC(mod, print = TRUE)
modConf$addMonitors(npara)
modMCMC <- buildMCMC(modConf)
CmodMCMC <- compileNimble(modMCMC, project = mod, dirName = "./working/")
samples <- runMCMC(CmodMCMC, niter = 10000)
print(summary(samples))
} else {
## do mcmc
regmod <- nimbleMCMC(code = nfun, constants = nconst, data = ndata, monitors = npara)
## model summary
print(summary(regmod))
}