# Basic package workflow

    library(steppedwedge)

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.

    data(sw_data_example)
    head(sw_data_example)
    #>   cluster period trt      prob outcome_bin outcome_cont
    #> 1       1      1   0 0.5667807           0  -3.02179837
    #> 2       1      1   0 0.5667807           0  -0.07145287
    #> 3       1      1   0 0.5667807           1   0.96807617
    #> 4       1      1   0 0.5667807           0   0.29456948
    #> 5       1      1   0 0.5667807           1  -0.83921584
    #> 6       1      1   0 0.5667807           1  -0.42335941

    dat <- load_data(
      time = "period",
      cluster_id = "cluster",
      individual_id = NULL,
      treatment = "trt",
      outcome = "outcome_bin",
      data = sw_data_example
    )
    #> Stepped wedge dataset loaded. Discrete time design with 15 clusters, 5 sequences, and 6 time points.

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

    plot_dat <- plot_design(dat)

    print(plot_dat)
    #> $design_plot

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`.

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

    print(analysis_1)
    #> Treatment effect estimate: -0.189
    #> Treatment effect 95% confidence interval: -0.448, 0.07
    #> Converged: TRUE

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

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

    print(analysis_1b)
    #> Treatment effect estimate: -0.189
    #> Treatment effect 95% confidence interval: -0.448, 0.07
    #> Converged: TRUE

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

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

    print(analysis_2)
    #> Treatment effect estimate: -0.188
    #> Treatment effect 95% confidence interval: -0.438, 0.061
    #> Converged:

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

    analysis_3 <- analyze(
      dat = dat,
      method = "mixed",
      estimand_type = "TATE",
      exp_time = "ETI",
      family = binomial
    )
    #> boundary (singular) fit: see help('isSingular')

    print(analysis_3)
    #> Treatment effect estimate: -0.184
    #> Treatment effect 95% confidence interval: -0.496, 0.129
    #> Converged: TRUE

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

    analysis_4 <- analyze(
      dat = dat,
      method = "mixed",
      estimand_type = "TATE",
      exp_time = "NCS",
      family = binomial
    )
    #> boundary (singular) fit: see help('isSingular')

    print(analysis_4)
    #> Treatment effect estimate: -0.184
    #> Treatment effect 95% confidence interval: -0.497, 0.128
    #> Converged: TRUE

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

    analysis_5 <- analyze(
      dat = dat,
      method = "mixed",
      estimand_type = "TATE",
      exp_time = "TEH",
      family = binomial
    )
    #> boundary (singular) fit: see help('isSingular')

    print(analysis_5)
    #> Treatment effect estimate: -0.189
    #> Treatment effect 95% confidence interval: -0.448, 0.07
    #> Converged: TRUE

## Continuous outcome

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

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

    print(analysis_6)
    #> Treatment effect estimate: -0.046
    #> Treatment effect 95% confidence interval: -0.126, 0.034
    #> Converged: TRUE