---
title: "Class effect NMA analysis using MBNMAdose"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Class effect NMA analysis using MBNMAdose}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 5,
  include=TRUE,
  tidy.opts=list(width.cutoff=80),
  tidy=TRUE
)
```

```{r setup, include = FALSE}
library(MBNMAdose)
library(dplyr)
```

Although `MBNMAdose` is intended to be used for dose-response Model-Based Network Meta-Analysis (MBNMA), it can also be adapted to perform standard Network Meta-Analysis (NMA), and this allows users to take advantage of some of the additional features of `MBNMAdose`, such as modelling class effects, for use in standard NMA. As well as fitting class effect models, `MBNMAdose` also allows for nodes-splitting to check for consistency in these models.

To illustrate how this can be done we will use a dataset of inhaled medications for Chronic Obstructive Pulmonary Disease (COPD) from the `netmeta` package:

```{r, loaddata}
library(netmeta)
data("Dong2013")

# Rename column names to match those used in MBNMAdose
Dong2013 <- Dong2013 %>% 
  rename(studyID = id,
         r = death,
         n = randomized)
```


## Performing standard NMA using `mbnma.run()`

The simplest use is in a network that includes a placebo treatment. In this dataset we do not have any dose-response information, so there is no value in performing a MBNMA. However, if we assume that every active treatment in the network is a separate "agent" with a dose of 1, and that the Placebo treatment has a dose of 0, then we can use the data in `MBNMAdose`, and by modelling a linear dose-response function we estimate parameters that are identical to a standard NMA model. 

```{r}
# Define agents and assign a dose of 1 to all agents
Dong2013 <- Dong2013 %>% 
  dplyr::rename(agent=treatment) %>%
  dplyr::mutate(dose=dplyr::case_when(agent=="Placebo" ~ 0,
                                      agent!="Placebo" ~ 1))
```

Note that if there is an intervention within the dataset that has been administered at multiple doses, you can force the dataset to be analysed either as a "split" network (in which different doses are assumed to have independent effects) by assigning each of them a separate `agent` name (e.g. "warfarinlow", "warfarinhigh"), or as a "lumped" network (in which different doses are assumed to have the same effect) by simply assigning both doses a dose of 1. Further details of "lumping" and "splitting" and the implications of these assumptions can be found in [@pedder2021cons].

Once we have reassigned the doses within the dataset we can create an `"mbnma.network"` object and create a network plot:

```{r, network.plot, message=FALSE}
network <- mbnma.network(Dong2013)
```
```{r}
summary(network)
plot(network)
```

We can then use a linear dose-response MBNMA to analyse the data. The coefficients for the linear slope of the dose-response function are mathematically equivalent to the basic treatment effect parameters estimated in a standard NMA. Note that the results are equivalent in both models (allowing for Monte-Carlo error from the MCMC sampling). The only difference is that the placebo parameter (`beta.1[1]`) that is equal to zero is not given in the output.

```{r, standard.nma, results="hide"}
nma.linear <- mbnma.run(network, fun=dpoly(degree=1),
                        n.iter=50000)
```
```{r}
print(nma.linear)
```

```{r, results="hide"}
nma <- nma.run(network, n.iter=50000)
```
```{r}
print(nma)
```

We can also show the equivalence of results using `get.relative()` to compare relative effects from both models:

```{r}
rels <- get.relative(nma.linear, nma)
```

Without a placebo, estimation is very similar, but requires renaming and recoding the network reference intervention to `"Placebo"`. This is not strictly necessary, as `MBNMAdose` can handle dose-response datasets that do not include placebo (or dose=0), but it will ensure that parameter estimates are equivalent between the NMA and MBNMA models and will make it easier to estimate relative effects. We illustrate this with the Surgical Site Infection dataset.

```{r}
# Ensure that Suture-absorbable is the network reference
ssi <- ssi_closure %>% dplyr::mutate(agent=factor(trt, levels=c("Suture-absorbable", unique(ssi_closure$trt)[-1])))

# Set dose=0 for network reference and dose=1 for all other interventions
ssi.plac <- ssi %>% dplyr::mutate(dose=dplyr::case_when(trt=="Suture-absorbable" ~ 0,
                                                        TRUE ~ 1))

network.plac <- mbnma.network(ssi.plac)
plot(network.plac)
# Note that Suture-absorbable (the comparator) has been renamed to Placebo
```
```{r, results="hide"}
# Run linear MBNMA model
nma.linear <- mbnma.run(network.plac, fun=dpoly(degree=1),
                        n.iter=50000)
```
```{r}
summary(nma.linear)
```

The linear dose-response coefficients can then be interpreted as the relative effect for each intervention versus the network reference (`"Suture-absorbable"`). 


## Benefits of using `mbnma.run()` for standard NMA models

Now that we have shown how to specify a standard NMA model within the MBNMA framework in `mbnma.run()`, we can now use `MBNMAdose` to implement some more interesting models, such as class effect models and node-splits to assess consistency.

A class effects model can be implemented using the `class.effect` argument in `mbnma.run()`, introducing either a `"common"` or 2 class effect on the single linear dose-response parameter, `beta.1`:

```{r, eval=FALSE}
# Random class effect model
nma.class <- mbnma.run(network.plac, fun=dpoly(degree=1),
                       class.effect=list(beta.1="random"),
                       n.iter=50000)
```

A `"common"` class effect assumes that all treatments within a class have the same effect, whilst a `"random"` class effect assumes that treatment-level effects are randomly distributed around a mean class effect with a standard deviation (SD) that is estimated within the model.

Common and random class effect models can be compared using model fit statistics (e.g. Deviance Information Criterion) to identify which is the most parsimonious model. Note that within `MBNMAdose` when a random class effect is fitted this makes the assumption that all classes share the same within-class SD. This may not necessarily be valid, but relaxing this cannot currently be done in `MBNMAdose` and it requires specific JAGS code to be written.
