library(rjags)
## Loading required package: coda
## Linked to JAGS 4.2.0
## Loaded modules: basemod,bugs
library(ggmcmc)
## Loading required package: dplyr
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
## Loading required package: tidyr
## Loading required package: ggplot2
mod <-"model {
for (i in 1:N) {
y[i] ~ dnorm(mu, tau)
}
mu ~ dunif(0, 10)
tau <- pow(sigma, -2)
sigma ~ dunif(0, 100)
}"
y=c(1,2,3)
N=length(y)
data<-list(N=N,y=y)
model<-jags.model(textConnection(mod),data=data)
## Compiling model graph
## Resolving undeclared variables
## Allocating nodes
## Graph information:
## Observed stochastic nodes: 3
## Unobserved stochastic nodes: 2
## Total graph size: 13
##
## Initializing model
update(model,n.iter=1000)
output=coda.samples(model=model,variable.names=c("mu"),n.iter=100000,thin=10)
update(model,n.iter=1000)
output=coda.samples(model=model,variable.names=c("mu"),n.iter=100000)
plot(output)
summary(output)
##
## Iterations = 103001:203000
## Thinning interval = 1
## Number of chains = 1
## Sample size per chain = 1e+05
##
## 1. Empirical mean and standard deviation for each variable,
## plus standard error of the mean:
##
## Mean SD Naive SE Time-series SE
## 2.388345 1.476242 0.004668 0.013698
##
## 2. Quantiles for each variable:
##
## 2.5% 25% 50% 75% 97.5%
## 0.3265 1.5327 2.1079 2.8290 6.6932