# 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
    #>           -1.1611e+01            2.2882e-01            3.9648e-02
    #>           followup_sq                 trial              trial_sq
    #>           -1.2707e-05            1.3483e-01            1.2872e-04
    #>                  sex1                 N_bas                 L_bas
    #>           -9.2140e-02            1.7953e-03           -5.4280e-02
    #>                 P_bas tx_init_bas1:followup
    #>            6.6794e-01           -1.7396e-03
    #>
    #> [[1]][[3]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -1.0342e+01           -3.2295e-03            3.4273e-02
    #>           followup_sq                 trial              trial_sq
    #>           -9.3766e-05            1.1262e-01           -1.7230e-04
    #>                  sex1                 N_bas                 L_bas
    #>            2.7689e-01            6.0393e-03            2.1043e-01
    #>                 P_bas tx_init_bas1:followup
    #>            5.3769e-01            5.1338e-03
    #>
    #> [[1]][[4]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -1.1515e+01            3.5272e-01            5.3181e-02
    #>           followup_sq                 trial              trial_sq
    #>           -4.2199e-04            1.3409e-01           -1.0882e-05
    #>                  sex1                 N_bas                 L_bas
    #>            3.8615e-01            2.3402e-03           -3.4840e-02
    #>                 P_bas tx_init_bas1:followup
    #>            6.6221e-01           -7.2209e-03
    #>
    #> [[1]][[5]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -5.09460298            0.55030989            0.04025223
    #>           followup_sq                 trial              trial_sq
    #>           -0.00019134            0.00054694            0.00092772
    #>                  sex1                 N_bas                 L_bas
    #>            0.26925035            0.00385078           -0.04034624
    #>                 P_bas tx_init_bas1:followup
    #>           -0.00533367           -0.00876662
    #>
    #> [[1]][[6]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -1.1915e+01            2.4183e-01            4.1928e-02
    #>           followup_sq                 trial              trial_sq
    #>           -1.4895e-04            1.4709e-01           -9.9127e-05
    #>                  sex1                 N_bas                 L_bas
    #>           -3.1886e-01            1.1281e-03           -5.1752e-03
    #>                 P_bas tx_init_bas1:followup
    #>            7.3268e-01            1.2237e-04
    #>
    #> [[1]][[7]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -6.64242284            0.39490881            0.05087902
    #>           followup_sq                 trial              trial_sq
    #>           -0.00028304            0.04305123            0.00043852
    #>                  sex1                 N_bas                 L_bas
    #>           -0.05787322            0.00204688            0.09276564
    #>                 P_bas tx_init_bas1:followup
    #>            0.15226078           -0.00921877
    #>
    #> [[1]][[8]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -4.92814305            0.68332724            0.05502778
    #>           followup_sq                 trial              trial_sq
    #>           -0.00034519            0.00230283            0.00086158
    #>                  sex1                 N_bas                 L_bas
    #>           -0.12162013            0.00516636           -0.02676057
    #>                 P_bas tx_init_bas1:followup
    #>           -0.03532724           -0.01451178
    #>
    #> [[1]][[9]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -6.69548280            0.24638704            0.03881572
    #>           followup_sq                 trial              trial_sq
    #>           -0.00032314            0.03791212            0.00045823
    #>                  sex1                 N_bas                 L_bas
    #>            0.45665924            0.00455101            0.09482990
    #>                 P_bas tx_init_bas1:followup
    #>            0.16930227           -0.00538022
    #>
    #> [[1]][[10]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -7.89301938           -0.00182891            0.03298869
    #>           followup_sq                 trial              trial_sq
    #>           -0.00022935            0.05788279            0.00052729
    #>                  sex1                 N_bas                 L_bas
    #>            0.27947100            0.00537989            0.12908188
    #>                 P_bas tx_init_bas1:followup
    #>            0.29911231            0.00515485
    #>
    #> [[1]][[11]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -7.61401463            0.24806848            0.04553806
    #>           followup_sq                 trial              trial_sq
    #>           -0.00031468            0.05300240            0.00045997
    #>                  sex1                 N_bas                 L_bas
    #>            0.38952025            0.00600147            0.12613862
    #>                 P_bas tx_init_bas1:followup
    #>            0.24548862           -0.00273528
    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.8009313 0.8735851 0.01853446
    #> 2:    ITT      1 0.8744359 0.8390789 0.9097928 0.01803959
    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.9842638   1.108219        0.03717768 -0.01351508
    #> 2: risk_1 risk_0  0.9574838  0.9023487   1.015988       -0.03717768 -0.08787043
    #>    RD 95% UCI
    #>         <num>
    #> 1: 0.08787043
    #> 2: 0.01351508