---
title: "Basic package workflow"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Basic package workflow}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

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

```{r, include = FALSE}
# Including this twice (once here with include=F) avoids a bug having to do with dplyr and pkgdown
data(sw_data_example)
dat <- load_data(
  time = "period",
  cluster_id = "cluster",
  individual_id = NULL,
  treatment = "trt",
  outcome = "outcome_bin",
  data = sw_data_example
)
```

The `load_data` function takes in raw data and creates a data object that can be accepted by the `plot_design` and `analyze` functions. We use the made-up dataframe `sw_data_example` to demonstrate the workflow.

```{r}
data(sw_data_example)
head(sw_data_example)

dat <- load_data(
  time = "period",
  cluster_id = "cluster",
  individual_id = NULL,
  treatment = "trt",
  outcome = "outcome_bin",
  data = sw_data_example
)
```


The `plot_design` function produces a diagram of the stepped wedge design and a summary of the variables.

```{r, fig.width=9, fig.height=8, fig.align = "center"}
plot_dat <- plot_design(dat)

print(plot_dat)
```


The `analyze` function analyzes the stepped wedge data. First, we analyze the data using a mixed effects model, with the Time Average Treament Effect (TATE) as the estimand, assuming an Immediate Treatment (IT) effect, passing the `family = "binomial"` and `link = "logit"` arguments to `glmer`. 

```{r}
analysis_1 <- analyze(
  dat = dat,
  method = "mixed",
  estimand_type = "TATE", 
  exp_time = "IT",
  family = binomial,
  re = c("clust", "time")
)

print(analysis_1)
```

Repeat the analysis, but including a random effect for cluster only, not for cluster-time interaction.

```{r}
analysis_1b <- analyze(
  dat = dat,
  method = "mixed",
  estimand_type = "TATE", 
  exp_time = "IT",
  family = binomial,
  re = "clust"
)

print(analysis_1b)
```


Repeat the analysis, but using GEE rather than a mixed model.

```{r}
analysis_2 <- analyze(
  dat = dat,
  method = "GEE",
  estimand_type = "TATE", 
  exp_time = "IT",
  family = binomial,
  corstr = "exchangeable"
)

print(analysis_2)
```


Mixed model, with Time Average Treament Effect (TATE) as the estimand, using an Exposure Time Indicator (ETI) model.

```{r}
analysis_3 <- analyze(
  dat = dat,
  method = "mixed",
  estimand_type = "TATE", 
  exp_time = "ETI",
  family = binomial
)

print(analysis_3)
```


Mixed model, with Time Average Treatment Effect (TATE) as the estimand, using a Natural Cubic Splines (NCS) model.

```{r}
analysis_4 <- analyze(
  dat = dat,
  method = "mixed",
  estimand_type = "TATE", 
  exp_time = "NCS",
  family = binomial
)

print(analysis_4)
```


Mixed model, with Time Average Treament Effect (TATE) as the estimand, using a Treatment Effect Heterogeneity over exposure time (TEH) model.

```{r}
analysis_5 <- analyze(
  dat = dat,
  method = "mixed",
  estimand_type = "TATE", 
  exp_time = "TEH",
  family = binomial
)

print(analysis_5)
```

## Continuous outcome

Mixed model, with Time Average Treament Effect (TATE) as the estimand, using a Natural Cubic Splines (NCS) model.

```{r}
analysis_6 <- analyze(
  dat = dat,
  method = "mixed",
  estimand_type = "TATE", 
  exp_time = "NCS",
  family = gaussian
)

print(analysis_6)
```