# Introduction to SEQuential

## Setting up your Analysis

There are some assumptions which must be met to avoid unintended errors when using SEQuential. These are:

1. User provided `time.col` begins at 0 per unique `id.col` entries, we also assume that the column contains only integers and continues by 1 for every time step. e.g. (0, 1, 2, 3, …) is allowed and (0, 1, 2, 2.5, …) or (0, 1, 2, 4, 5, …) are not.
2. Provided `time.col` entries may be out of order as a sort is enforced at the beginning of the function, e.g. (0, 2, 1, 4, 3, …) is valid because it begins at 0 and is continuously increasing by increments of 1, even though it is not ordered.
3. `eligible` and column names provided to and `excused.cols` are once one only one (with respect to `time.col`) flag variables

### Step 1 - Defining your options

In your R script, you will always start by defining your options object, through the `SEQopts` helper. There are many defaults which allow you to target exactly how you would like to change your analysis. Through this wiki there are specific pages dedicated to each causal contrast and the parameters which affect them, but for simplicity let’s start with an intention-to-treat analysis with 20 bootstrap samples.

    library(SEQTaRget)

    options <- SEQopts(km.curves = TRUE, #asks the function to return survival and risk estimates
                       bootstrap = TRUE, #asks the model to preform bootstrapping
                       bootstrap.nboot = 10) #asks the model for 10 bootstrap samples

In general, options will be in the form `{option}.{parameter}` - here you may notice that we use `bootstrap.nboot` indicating that this parameter affects the `bootstrap`

### Step 2 - Running the Primary Function

The next step is running the primary R function, `SEQuential`. Here you will give your options, data, and data-level information. We provide some small simulated datasets to test on.

    data <- SEQdata
    model <- SEQuential(data, id.col = "ID",
                              time.col = "time",
                              eligible.col = "eligible",
                              treatment.col = "tx_init",
                              outcome.col = "outcome",
                              time_varying.cols = c("N", "L", "P"),
                              fixed.cols = "sex",
                              method = "ITT", options = options)
    #> Non-required columns provided, pruning for efficiency
    #> Pruned
    #> Expanding Data...
    #> Expansion Successful
    #> Moving forward with ITT analysis
    #> Bootstrapping with 80 % of data 10 times
    #> ITT model created successfully
    #> Creating Survival curves
    #> Scale for colour is already present.
    #> Adding another scale for colour, which will replace the existing scale.
    #> Completed

`SEQuential` is a rather chunky algorithm and will take some time to run, especially when bootstrapping. We provide some print statements to help track where the function is processing at any given point in time.

### Step 3 - Recovering your results

`SEQuential` produces a lot of internal diagnostics, models, and dataframes out of its main function in an S4 class. We provide a few different methods to handle obtaining your results.

    outcome(model)     # Returns a list of only the outcome models
    #> [[1]]
    #> [[1]][[1]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -6.85931555            0.22530938            0.03538172
    #>           followup_sq                 trial              trial_sq
    #>           -0.00015987            0.04471790            0.00057617
    #>                  sex1                 N_bas                 L_bas
    #>            0.12704583            0.00328671           -0.01385088
    #>                 P_bas tx_init_bas1:followup
    #>            0.20092890           -0.00170402
    #>
    #> [[1]][[2]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -2.8818e+00            1.5458e-01            2.8891e-02
    #>           followup_sq                 trial              trial_sq
    #>           -1.2442e-05           -3.3364e-02            1.1065e-03
    #>                  sex1                 N_bas                 L_bas
    #>            2.6813e-01            5.4326e-03            7.6253e-03
    #>                 P_bas tx_init_bas1:followup
    #>           -2.2438e-01            4.8174e-04
    #>
    #> [[1]][[3]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -5.93954351            0.39052220            0.04623346
    #>           followup_sq                 trial              trial_sq
    #>           -0.00017119            0.01828274            0.00088302
    #>                  sex1                 N_bas                 L_bas
    #>            0.10997513            0.00415936            0.01088913
    #>                 P_bas tx_init_bas1:followup
    #>            0.06552656           -0.00462971
    #>
    #> [[1]][[4]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -5.20280470            0.11778505            0.04716606
    #>           followup_sq                 trial              trial_sq
    #>           -0.00033879            0.02070199            0.00072309
    #>                  sex1                 N_bas                 L_bas
    #>            0.16653561            0.00772625           -0.05460015
    #>                 P_bas tx_init_bas1:followup
    #>           -0.00121856           -0.00458081
    #>
    #> [[1]][[5]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -1.31069886            0.39533850            0.04000588
    #>           followup_sq                 trial              trial_sq
    #>           -0.00024612           -0.07495500            0.00170491
    #>                  sex1                 N_bas                 L_bas
    #>            0.54203177            0.00296646           -0.16169056
    #>                 P_bas tx_init_bas1:followup
    #>           -0.40800972           -0.00556035
    #>
    #> [[1]][[6]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -1.59212946            0.31055215            0.04601782
    #>           followup_sq                 trial              trial_sq
    #>           -0.00028746           -0.05696195            0.00122489
    #>                  sex1                 N_bas                 L_bas
    #>            0.23952318           -0.00087135            0.07354039
    #>                 P_bas tx_init_bas1:followup
    #>           -0.36763273           -0.01002600
    #>
    #> [[1]][[7]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -7.45515991            0.32387607            0.04234360
    #>           followup_sq                 trial              trial_sq
    #>           -0.00028500            0.05781114            0.00063773
    #>                  sex1                 N_bas                 L_bas
    #>            0.30031113            0.00061300           -0.24698932
    #>                 P_bas tx_init_bas1:followup
    #>            0.25498781           -0.00660780
    #>
    #> [[1]][[8]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>            0.10086177            0.19474362            0.03973499
    #>           followup_sq                 trial              trial_sq
    #>           -0.00014536           -0.08827239            0.00160902
    #>                  sex1                 N_bas                 L_bas
    #>            0.28107125            0.00676083           -0.03627233
    #>                 P_bas tx_init_bas1:followup
    #>           -0.56357419            0.00102267
    #>
    #> [[1]][[9]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -8.1032e+00            4.4098e-01            4.8202e-02
    #>           followup_sq                 trial              trial_sq
    #>           -2.7460e-04            6.9834e-02            3.2752e-04
    #>                  sex1                 N_bas                 L_bas
    #>            8.1452e-02           -5.3239e-05            1.2785e-02
    #>                 P_bas tx_init_bas1:followup
    #>            3.0500e-01           -1.1470e-02
    #>
    #> [[1]][[10]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -3.6821e+00            8.7000e-02            3.3651e-02
    #>           followup_sq                 trial              trial_sq
    #>           -5.9960e-06           -1.9660e-02            1.0831e-03
    #>                  sex1                 N_bas                 L_bas
    #>            1.2764e-01            5.1156e-03            1.0989e-01
    #>                 P_bas tx_init_bas1:followup
    #>           -1.6046e-01            5.1112e-03
    #>
    #> [[1]][[11]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -7.35403702            0.16931737            0.04659462
    #>           followup_sq                 trial              trial_sq
    #>           -0.00031804            0.04820928            0.00031898
    #>                  sex1                 N_bas                 L_bas
    #>            0.33172709            0.00128275            0.28121168
    #>                 P_bas tx_init_bas1:followup
    #>            0.20887049           -0.00078443
    km_curve(model)    # Prints the survival curve
    #> Scale for colour is already present.
    #> Adding another scale for colour, which will replace the existing scale.
    #> [[1]]

    risk_data(model)
    #> [[1]]
    #>    Method      A      Risk   95% LCI   95% UCI         SE
    #>    <char> <char>     <num>     <num>     <num>      <num>
    #> 1:    ITT      0 0.8372582 0.8001404 0.8743760 0.01893802
    #> 2:    ITT      1 0.8744359 0.8232240 0.9256478 0.02612901
    risk_comparison(model)
    #> [[1]]
    #>       A_x    A_y Risk Ratio RR 95% LCI RR 95% UCI Risk Differerence  RD 95% LCI
    #>    <fctr> <fctr>      <num>      <num>      <num>             <num>       <num>
    #> 1: risk_0 risk_1  1.0444041  0.9704394   1.124006        0.03717768 -0.02607099
    #> 2: risk_1 risk_0  0.9574838  0.8896748   1.030461       -0.03717768 -0.10042635
    #>    RD 95% UCI
    #>         <num>
    #> 1: 0.10042635
    #> 2: 0.02607099