library("R2WinBUGS")
library("R2jags")
library("rstan")
library("nimble")
library("boa")
library("coda")
library("xtable")
library("kableExtra")
library("tictoc")
######################################################################################
# SIMULAÇÃO DOS DADOS #
######################################################################################
set.seed(123)
#Parâmetros verdadeiros
beta0=-2
beta1=2
beta2=10
sigma2=5
n=100
tau=1/sigma2
#Geração
x1=rnorm(n,0,1)
x2=rnorm(n,2,10)
e=rnorm(n,0, sd = sqrt(sigma2))
y=beta0+beta1*x1+beta2*x2+e
######################################################################################
# BUGS #
######################################################################################
#Modelo do tipo BUGS
sink("mod1.txt")
cat("
MODEL LR1 {
#Verossimilhança
for(i in 1:N) {
y[i] ~ dnorm(mu[i], tau)
mu[i] <- beta0 + beta1*x1[i] + beta2*x2[i]
}
#Proris
beta0 ~ dnorm(0,0.1)
beta1 ~ dnorm(0,0.1)
beta2 ~ dnorm(0,0.1)
tau ~ dexp(0.1)
#Transformação para variância
sigma2 <- 1/tau
} ", fill = TRUE)
sink()
#Obeservação: A dist normal no bugs está definido para a precisão.
#Tamanho amostral
N = length(y)
#Conjunto de dados observados
data = list("N","y","x1","x2")
#Parâmetros estudados
params = c("beta0", "beta1","beta2","tau","sigma2")
#Valores iniciais
inits <- function () {list(beta0 = rnorm(1),
beta1 = rnorm(1),
beta2 = rnorm(1),
tau = 1)
sigma2 = 1}
#Modelo final
tic()
result <- bugs(data = data, inits = inits, parameters.to.save = params,
model.file = "mod1.txt", n.chains = 3, n.iter = 10000,
n.burnin = 1000, n.thin=9, bugs.directory = "C:\\Program Files\\WinBUGS14",
debug = TRUE, save.history = TRUE, DIC = FALSE)
cannot create file 'C:\Program Files\WinBUGS14/System/Rsrc/Registry_Rsave.odc', reason 'Permission denied'cannot open file 'C:\Program Files\WinBUGS14/System/Rsrc/Registry.odc': Permission deniedError in file(con, "wb") : cannot open the connection
toc()
6.12 sec elapsed
result$summary %>%
kable() %>%
kable_styling()
| mean | sd | 2.5% | 25% | 50% | 75% | 97.5% | Rhat | n.eff | |
|---|---|---|---|---|---|---|---|---|---|
| beta0 | -1.7017525 | 0.2174842 | -2.1550000 | -1.84525 | -1.70400 | -1.554000 | -1.279975 | 1.002923 | 830 |
| beta1 | 1.6939550 | 0.2304004 | 1.2440000 | 1.53975 | 1.69900 | 1.845000 | 2.144000 | 1.001317 | 2400 |
| beta2 | 10.0042037 | 0.0220753 | 9.9620000 | 9.98900 | 10.00000 | 10.020000 | 10.050000 | 1.000688 | 3000 |
| tau | 0.2250701 | 0.0327324 | 0.1658975 | 0.20160 | 0.22385 | 0.246025 | 0.293710 | 1.000817 | 3000 |
| sigma2 | 4.5387863 | 0.6724222 | 3.4048749 | 4.06400 | 4.46750 | 4.959250 | 6.026150 | 1.000817 | 3000 |
bugs = result$sims.list
bugs_b0 = bugs$beta0
bugs_b1 = bugs$beta1
bugs_b2 = bugs$beta2
bugs_s2 = bugs$sigma2
bugs_tau = bugs$tau
plot(bugs_b0,type = "l",main=expression(beta[0]),ylab="",
xlab="",cex.main=2)
abline(h=-2,col="red")
plot(bugs_b1,type = "l",main=expression(beta[1]),ylab="",
xlab="",cex.main=2)
abline(h=2,col="red")
plot(bugs_b2,type = "l",main=expression(beta[2]),ylab="",
xlab="",cex.main=2)
abline(h=10,col="red")
plot(bugs_s2,type = "l",main=expression(sigma^2),ylab="",
xlab="",cex.main=2)
abline(h=5,col="red")
plot(bugs_tau,type = "l",main=expression(tau),ylab="",
xlab="",cex.main=2)
abline(h=1/5,col="red")
densplot(as.mcmc(bugs_b0),main=expression(beta[0]),xlab="",ylab="",cex.main=2)
densplot(as.mcmc(bugs_b1),main=expression(beta[1]),xlab="",ylab="",cex.main=2)
densplot(as.mcmc(bugs_b2),main=expression(beta[2]),xlab="",ylab="",cex.main=2)
densplot(as.mcmc(bugs_s2),main=expression(sigma^2),xlab="",ylab="",cex.main=2)
densplot(as.mcmc(bugs_tau),main=expression(tau),xlab="",ylab="",cex.main=2)
######################################################################################
# JAGS #
######################################################################################
model_code = 'model{
#Verossimilhança
for(i in 1:N) {
y[i] ~ dnorm(mu[i], tau)
mu[i] <- beta0 + beta1*x1[i] + beta2*x2[i]
}
#Proris
beta0 ~ dnorm(0,0.1)
beta1 ~ dnorm(0,0.1)
beta2 ~ dnorm(0,0.1)
tau ~ dexp(0.1)
#Transformação para variância
sigma2 <- 1/tau}'
N = length(y)
model_data = list(N = N, y = y, x1 = x1, x2 = x2)
model_parameters = c("beta0", "beta1","beta2", "sigma2","tau")
tic()
model_run = jags(data = model_data,
parameters.to.save = model_parameters,
model.file=textConnection(model_code),
n.chains=3, # Number of different starting positions
n.iter=10000, # Number of iterations
n.burnin=1000, # Number of iterations to remove at start
n.thin=9,# Amount of thinning
DIC=FALSE)
module glm loaded
module dic loaded
Compiling model graph
Resolving undeclared variables
Allocating nodes
Graph information:
Observed stochastic nodes: 100
Unobserved stochastic nodes: 4
Total graph size: 609
Initializing model
|
| | 0%
|
|* | 2%
|
|** | 4%
|
|*** | 7%
|
|**** | 9%
|
|****** | 11%
|
|******* | 13%
|
|******** | 16%
|
|********* | 18%
|
|********** | 20%
|
|*********** | 22%
|
|************ | 24%
|
|************* | 27%
|
|************** | 29%
|
|**************** | 31%
|
|***************** | 33%
|
|****************** | 36%
|
|******************* | 38%
|
|******************** | 40%
|
|********************* | 42%
|
|********************** | 44%
|
|*********************** | 47%
|
|************************ | 49%
|
|************************** | 51%
|
|*************************** | 53%
|
|**************************** | 56%
|
|***************************** | 58%
|
|****************************** | 60%
|
|******************************* | 62%
|
|******************************** | 64%
|
|********************************* | 67%
|
|********************************** | 69%
|
|************************************ | 71%
|
|************************************* | 73%
|
|************************************** | 76%
|
|*************************************** | 78%
|
|**************************************** | 80%
|
|***************************************** | 82%
|
|****************************************** | 84%
|
|******************************************* | 87%
|
|******************************************** | 89%
|
|********************************************** | 91%
|
|*********************************************** | 93%
|
|************************************************ | 96%
|
|************************************************* | 98%
|
|**************************************************| 100%
toc()
1.53 sec elapsed
model_run$BUGSoutput$summary %>%
kable() %>%
kable_styling()
| mean | sd | 2.5% | 25% | 50% | 75% | 97.5% | Rhat | n.eff | |
|---|---|---|---|---|---|---|---|---|---|
| beta0 | -1.7076087 | 0.2190477 | -2.1461585 | -1.8507053 | -1.7046471 | -1.5691274 | -1.2767817 | 1.002397 | 1100 |
| beta1 | 1.6961675 | 0.2307555 | 1.2357823 | 1.5441611 | 1.6996822 | 1.8477681 | 2.1492119 | 1.000865 | 3000 |
| beta2 | 10.0048006 | 0.0221120 | 9.9629177 | 9.9894164 | 10.0044162 | 10.0195144 | 10.0493598 | 1.001024 | 3000 |
| sigma2 | 4.5286361 | 0.6615459 | 3.3978227 | 4.0576457 | 4.4566326 | 4.9135678 | 5.9771186 | 1.000677 | 3000 |
| tau | 0.2254174 | 0.0322019 | 0.1673047 | 0.2035181 | 0.2243847 | 0.2464483 | 0.2943061 | 1.000677 | 3000 |
jags = model_run[["BUGSoutput"]][["sims.matrix"]]
jags_b0 = jags[,1]
jags_b1 = jags[,2]
jags_b2 = jags[,3]
jags_s2 = jags[,4]
jags_tau = jags[,5]
plot(jags_b0,type = "l",main=expression(beta[0]),ylab="",
xlab="",cex.main=2)
abline(h=beta0,col="red")
plot(jags_b1,type = "l",main=expression(beta[1]),ylab="",
xlab="",cex.main=2)
abline(h=beta1,col="red")
plot(bugs_b2,type = "l",main=expression(beta[2]),ylab="",
xlab="",cex.main=2)
abline(h=10,col="red")
plot(jags_s2,type = "l",main=expression(sigma^2),ylab="",
xlab="",cex.main=2)
abline(h=sigma2,col="red")
plot(jags_tau,type = "l",main=expression(tau),ylab="",
xlab="",cex.main=2)
abline(h=tau,col="red")
densplot(as.mcmc(jags_b0),main=expression(beta[0]),xlab="",ylab="",cex.main=2)
densplot(as.mcmc(jags_b1),main=expression(beta[1]),xlab="",ylab="",cex.main=2)
densplot(as.mcmc(bugs_b2),main=expression(beta[2]),xlab="",ylab="",cex.main=2)
densplot(as.mcmc(jags_s2),main=expression(sigma^2),xlab="",ylab="",cex.main=2)
densplot(as.mcmc(jags_tau),main=expression(tau),xlab="",ylab="",cex.main=2)
######################################################################################
# STAN #
######################################################################################
model = "data {
int<lower=1> N;
vector[N] y;
vector[N] x1;
vector[N] x2;
}
parameters {
real alpha;
real beta1;
real beta2;
real<lower=0> sigma2;
}
model {
alpha ~ normal(0, 10);
beta1 ~ normal(0, 10);
beta2 ~ normal(0, 10);
sigma2 ~ exponential(0.1);
y ~ normal(alpha + beta1 * x1 + beta2*x2, sqrt(sigma2));
}
generated quantities {
vector[N] y_rep;
for(n in 1:N) {
y_rep[n] = normal_rng(alpha + beta1 * x1[n] + beta2*x2[n], sqrt(sigma2));
}
}"
data <- list(y = y, x1 = x1, x2 = x2 , N = 100)
tic()
fit <- stan(model_code = model,
data = data, iter = 2000, warmup = 1000, chains = 3)
SAMPLING FOR MODEL 'e1b1399f422eab1042231e247cc960d0' NOW (CHAIN 1).
Chain 1:
Chain 1: Gradient evaluation took 0 seconds
Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 1: Adjust your expectations accordingly!
Chain 1:
Chain 1:
Chain 1: Iteration: 1 / 2000 [ 0%] (Warmup)
Chain 1: Iteration: 200 / 2000 [ 10%] (Warmup)
Chain 1: Iteration: 400 / 2000 [ 20%] (Warmup)
Chain 1: Iteration: 600 / 2000 [ 30%] (Warmup)
Chain 1: Iteration: 800 / 2000 [ 40%] (Warmup)
Chain 1: Iteration: 1000 / 2000 [ 50%] (Warmup)
Chain 1: Iteration: 1001 / 2000 [ 50%] (Sampling)
Chain 1: Iteration: 1200 / 2000 [ 60%] (Sampling)
Chain 1: Iteration: 1400 / 2000 [ 70%] (Sampling)
Chain 1: Iteration: 1600 / 2000 [ 80%] (Sampling)
Chain 1: Iteration: 1800 / 2000 [ 90%] (Sampling)
Chain 1: Iteration: 2000 / 2000 [100%] (Sampling)
Chain 1:
Chain 1: Elapsed Time: 0.266 seconds (Warm-up)
Chain 1: 0.253 seconds (Sampling)
Chain 1: 0.519 seconds (Total)
Chain 1:
SAMPLING FOR MODEL 'e1b1399f422eab1042231e247cc960d0' NOW (CHAIN 2).
Chain 2:
Chain 2: Gradient evaluation took 0 seconds
Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 2: Adjust your expectations accordingly!
Chain 2:
Chain 2:
Chain 2: Iteration: 1 / 2000 [ 0%] (Warmup)
Chain 2: Iteration: 200 / 2000 [ 10%] (Warmup)
Chain 2: Iteration: 400 / 2000 [ 20%] (Warmup)
Chain 2: Iteration: 600 / 2000 [ 30%] (Warmup)
Chain 2: Iteration: 800 / 2000 [ 40%] (Warmup)
Chain 2: Iteration: 1000 / 2000 [ 50%] (Warmup)
Chain 2: Iteration: 1001 / 2000 [ 50%] (Sampling)
Chain 2: Iteration: 1200 / 2000 [ 60%] (Sampling)
Chain 2: Iteration: 1400 / 2000 [ 70%] (Sampling)
Chain 2: Iteration: 1600 / 2000 [ 80%] (Sampling)
Chain 2: Iteration: 1800 / 2000 [ 90%] (Sampling)
Chain 2: Iteration: 2000 / 2000 [100%] (Sampling)
Chain 2:
Chain 2: Elapsed Time: 0.247 seconds (Warm-up)
Chain 2: 0.283 seconds (Sampling)
Chain 2: 0.53 seconds (Total)
Chain 2:
SAMPLING FOR MODEL 'e1b1399f422eab1042231e247cc960d0' NOW (CHAIN 3).
Chain 3:
Chain 3: Gradient evaluation took 0 seconds
Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
Chain 3: Adjust your expectations accordingly!
Chain 3:
Chain 3:
Chain 3: Iteration: 1 / 2000 [ 0%] (Warmup)
Chain 3: Iteration: 200 / 2000 [ 10%] (Warmup)
Chain 3: Iteration: 400 / 2000 [ 20%] (Warmup)
Chain 3: Iteration: 600 / 2000 [ 30%] (Warmup)
Chain 3: Iteration: 800 / 2000 [ 40%] (Warmup)
Chain 3: Iteration: 1000 / 2000 [ 50%] (Warmup)
Chain 3: Iteration: 1001 / 2000 [ 50%] (Sampling)
Chain 3: Iteration: 1200 / 2000 [ 60%] (Sampling)
Chain 3: Iteration: 1400 / 2000 [ 70%] (Sampling)
Chain 3: Iteration: 1600 / 2000 [ 80%] (Sampling)
Chain 3: Iteration: 1800 / 2000 [ 90%] (Sampling)
Chain 3: Iteration: 2000 / 2000 [100%] (Sampling)
Chain 3:
Chain 3: Elapsed Time: 0.254 seconds (Warm-up)
Chain 3: 0.296 seconds (Sampling)
Chain 3: 0.55 seconds (Total)
Chain 3:
stan = extract(fit)
toc()
2.92 sec elapsed
summary(fit,probs = c(0.025, 0.25, 0.50, 0.75, 0.975),pars=c("alpha","beta1","beta2","sigma2")) %>%
kable() %>%
kable_styling()
|
|
length(stan_b0)
[1] 6000
stan_b0 = stan$alpha
stan_b1 = stan$beta1
stan_b2 = stan$beta2
stan_s2 = stan$sigma2
par(mfrow=c(1,4))
plot(stan_b0,type = "l",main=expression(beta[0]),ylab="",
xlab="",cex.main=2)
abline(h=-2,col="red")
plot(stan_b1,type = "l",main=expression(beta[1]),ylab="",
xlab="",cex.main=2)
abline(h=2,col="red")
plot(stan_b2,type = "l",main=expression(beta[2]),ylab="",
xlab="",cex.main=2)
abline(h=10,col="red")
plot(stan_s2,type = "l",main=expression(sigma^2),ylab="",
xlab="",cex.main=2)
abline(h=5,col="red")
par(mfrow=c(1,4))
plot(density(stan_b0),main=expression(beta[0]),cex.main=2)
plot(density(stan_b1),main=expression(beta[1]),cex.main=2)
plot(density(stan_b2),main=expression(beta[2]),cex.main=2)
plot(density(stan_s2),main=expression(sigma^2),cex.main=2)
######################################################################################
# NIMBLE #
######################################################################################
simpleCode1 <- nimbleCode({
beta0 ~ dnorm(0,0.1)
beta1 ~ dnorm(0,0.1)
beta2 ~ dnorm(0,0.1)
tau ~ dexp(0.1)
sigma2 <- 1/tau
for(i in 1:N) {
Ypred[i] <- beta0 + beta1 * x1[i] + beta2*x2[i]
Y[i] ~ dnorm(Ypred[i], tau)
}
})
simpleModel1 <- nimbleModel(simpleCode1,
data = list(Y = y, x1 = x1,x2=x2),
constants = list(N = N),
inits = list(beta0 = 0, beta1 = 0,beta2=0, tau = 2 ))
defining model...
building model...
setting data and initial values...
running calculate on model (any error reports that follow may simply reflect missing values in model variables) ...
checking model sizes and dimensions...
model building finished.
tic()
mcmc.out <- nimbleMCMC(code = simpleModel1,
data = list(Y = y, x1 = x1,x2=x2),
inits = list(beta0 = 0, beta1 = 0,beta2=0, tau = 2),
monitors=c("beta0","beta1","beta2","tau","sigma2"),
nchains = 3,
niter = 10000,
thin = 9,
nburnin = 1000,
summary = TRUE,
progressBar = TRUE)
compiling... this may take a minute. Use 'showCompilerOutput = TRUE' to see C++ compiler details.
compilation finished.
runMCMC's handling of nburnin changed in nimble version 0.6-11. Previously, nburnin samples were discarded *post-thinning*. Now nburnin samples are discarded *pre-thinning*. The number of samples returned will be floor((niter-nburnin)/thin).
running chain 1...
|-------------|-------------|-------------|-------------|
|-------------------------------------------------------|
running chain 2...
|-------------|-------------|-------------|-------------|
|-------------------------------------------------------|
running chain 3...
|-------------|-------------|-------------|-------------|
|-------------------------------------------------------|
toc()
47.8 sec elapsed
mcmc.out$summary %>%
kable() %>%
kable_styling()
|
|
|
|
nimble_b1 = c(mcmc.out$samples$chain1[,1],mcmc.out$samples$chain2[,1],mcmc.out$samples$chain3[,1])
nimble_b1 = c(mcmc.out$samples$chain1[,2],mcmc.out$samples$chain2[,2],mcmc.out$samples$chain3[,2])
nimble_b2 = c(mcmc.out$samples$chain1[,3],mcmc.out$samples$chain2[,3],mcmc.out$samples$chain3[,3])
numble_s2 = c(mcmc.out$samples$chain1[,4],mcmc.out$samples$chain2[,4],mcmc.out$samples$chain3[,4])
nimble_tau = c(mcmc.out$samples$chain1[,5],mcmc.out$samples$chain2[,5],mcmc.out$samples$chain3[,5])
plot(nimble_b0,type = "l",main=expression(beta[0]),ylab="",
xlab="",cex.main=2)
abline(h=-2,col="red")
plot(nimble_b1,type = "l",main=expression(beta[1]),ylab="",
xlab="",cex.main=2)
abline(h=2,col="red")
plot(nimble_b2,type = "l",main=expression(beta[2]),ylab="",
xlab="",cex.main=2)
abline(h=10,col="red")
plot(numble_s2,type = "l",main=expression(sigma^2),ylab="",
xlab="",cex.main=2)
abline(h=5,col="red")
plot(nimble_tau,type = "l",main=expression(tau),ylab="",
xlab="",cex.main=2)
abline(h=1/5,col="red")
plot(density(nimble_b0),main=expression(beta[0]),cex.main=2)
plot(density(nimble_b1),main=expression(beta[1]),cex.main=2)
plot(density(nimble_b2),main=expression(beta[2]),cex.main=2)
plot(density(numble_s2),main=expression(sigma^2),cex.main=2)
plot(density(nimble_tau),main=expression(tau),cex.main=2)