---
title: "smoking-jags"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{smoking-jags}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(BCEA)
```

Load the R2jags package and the the data file
```{r}
library(R2jags)
```

Specify the name of the jags model file.
```{r}
model.file <- here::here("inst/jags/smoking_model_RE.txt")
```

Load smoking data.
```{r}
data("Smoking")
```

Copy smoking data frame columns to local variables.
```{r}
attach(smoking)
nobs <- nobs
s <- s
t <- i
r <- r_i
n <- n_i
b <- b_i + 1
detach(smoking)
```

```{r}
# number of trials
ns <- length(unique(s))

# number of comparators
nt <- length(unique(t))

# number of observations
nobs <- dim(smoking)[1]

# how many studies include baseline
incb <- sum(table(s, b)[, 1] > 0)
```

Define data and parameters to monitor and run.
```{r}
inputs <- list ("s", "n", "r", "t", "ns", "nt", "b", "nobs", "incb")#, "na")
pars <- c("rr ", "pi ", "p", "d", "sd ")#, "T")

smoking_output <-
  jags(
    data = inputs,
    inits = NULL,
    parameters.to.save = pars,
    model.file = model.file,
    n.burnin = 5000,
    n.chains = 2,
    n.iter = 10000,
    n.thin = 20)
```


```{r}
smoking_output
```

```{r}
save(smoking_output, file = here::here("inst/extdata/smoking_output.RData"))
```
