---
title: "Stat. 481 Pumps Example"
output: html_notebook
---
```{r}
require(rjags)
source("DBDA2E-utilities.R")
```
JAGS need the data to be in a list of values, vectors, and matrices.
```{r}
dataList <- list(t = c(94.3, 15.7, 62.9, 126, 5.24, 31.4, 1.05, 1.05, 2.1, 10.5),
x = c( 5, 1, 5, 14, 3, 19, 1, 1, 4, 22), N = 10)
dataList
```
# Model
```{r}
modelString <- "
model
{
for (i in 1 : N) {
theta[i] ~ dgamma(alpha, beta)
lambda[i] <- theta[i] * t[i]
x[i] ~ dpois(lambda[i])
}
alpha ~ dexp(1)
beta ~ dgamma(0.1, 1.0)
}
"
```
# Inits
```{r}
initsList <- list(alpha = 1, beta = 1)
```
# Updates
```{r}
jagsModel <- jags.model( file = textConnection(modelString),
data = dataList,
inits = initsList,
n.chains = 5,
n.adapt = 10000 )
update( jagsModel , n.iter=5000 ) # Burn-in
codaSamples = coda.samples( jagsModel,
variable.names=c("alpha","beta","theta","lambda"),
n.iter=5000 ) # Record sampled values
save( codaSamples , file=paste0("Mcmc.Rdata") )
```
# Examine the chains:
# Convergence diagnostics:
```{r}
plot(codaSamples)
```
```{r}
diagMCMC( codaObject=codaSamples, parName="alpha" )
diagMCMC( codaObject=codaSamples, parName="beta" )
```
```{r}
traceplot(codaSamples)
```
```{r}
plotPost( codaSamples[,"alpha"], main="alpha", xlab=bquote(alpha) )
plotPost( codaSamples[,"beta"], main="beta", xlab=bquote(beta) )
```
```{r}
summary(codaSamples)
```