# 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.4058e+01            3.4546e-01            3.5261e-02
    #>           followup_sq                 trial              trial_sq
    #>           -1.8155e-04            1.7967e-01           -3.7334e-04
    #>                  sex1                 N_bas                 L_bas
    #>            2.0827e-02            1.0890e-03           -4.0549e-02
    #>                 P_bas tx_init_bas1:followup
    #>            9.6429e-01           -4.6524e-03
    #>
    #> [[1]][[3]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -1.0657e+01            1.6281e-01            2.7905e-02
    #>           followup_sq                 trial              trial_sq
    #>           -4.8791e-05            1.1791e-01            2.4743e-04
    #>                  sex1                 N_bas                 L_bas
    #>            3.8692e-01            2.1622e-03           -1.5366e-01
    #>                 P_bas tx_init_bas1:followup
    #>            6.1536e-01            6.1362e-03
    #>
    #> [[1]][[4]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -5.42475690           -0.00879902            0.03927933
    #>           followup_sq                 trial              trial_sq
    #>           -0.00011925            0.02926560            0.00063883
    #>                  sex1                 N_bas                 L_bas
    #>            0.14618347            0.00117743           -0.02516365
    #>                 P_bas tx_init_bas1:followup
    #>            0.03949348            0.00198139
    #>
    #> [[1]][[5]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -2.1479e+00            2.7512e-01            3.2204e-02
    #>           followup_sq                 trial              trial_sq
    #>           -6.2059e-05           -5.0454e-02            1.2961e-03
    #>                  sex1                 N_bas                 L_bas
    #>            8.1077e-02            7.0885e-03            2.7426e-02
    #>                 P_bas tx_init_bas1:followup
    #>           -3.0804e-01            3.4366e-03
    #>
    #> [[1]][[6]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -1.82712919            0.43496341            0.04885087
    #>           followup_sq                 trial              trial_sq
    #>           -0.00026942           -0.05247555            0.00116964
    #>                  sex1                 N_bas                 L_bas
    #>            0.28189559            0.00381025            0.01966807
    #>                 P_bas tx_init_bas1:followup
    #>           -0.38022154           -0.00972070
    #>
    #> [[1]][[7]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -2.86022253            0.41575547            0.03108768
    #>           followup_sq                 trial              trial_sq
    #>           -0.00017033           -0.03862018            0.00110694
    #>                  sex1                 N_bas                 L_bas
    #>            0.06382078            0.00560025            0.01729830
    #>                 P_bas tx_init_bas1:followup
    #>           -0.21483028           -0.00450247
    #>
    #> [[1]][[8]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -8.2994e+00            7.4986e-02            2.9284e-02
    #>           followup_sq                 trial              trial_sq
    #>           -4.1048e-05            6.5740e-02            8.1030e-04
    #>                  sex1                 N_bas                 L_bas
    #>            7.8510e-02            6.8961e-03           -9.3489e-02
    #>                 P_bas tx_init_bas1:followup
    #>            3.7256e-01            8.6232e-03
    #>
    #> [[1]][[9]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -2.68788123            0.37866604            0.04062045
    #>           followup_sq                 trial              trial_sq
    #>           -0.00017688           -0.03476344            0.00127386
    #>                  sex1                 N_bas                 L_bas
    #>            0.09171853           -0.00022630           -0.12865507
    #>                 P_bas tx_init_bas1:followup
    #>           -0.24749633           -0.00345267
    #>
    #> [[1]][[10]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -6.98395658           -0.09421815            0.03191213
    #>           followup_sq                 trial              trial_sq
    #>           -0.00010034            0.05335655            0.00044117
    #>                  sex1                 N_bas                 L_bas
    #>           -0.04211071            0.00324712            0.08202445
    #>                 P_bas tx_init_bas1:followup
    #>            0.21659699            0.00790335
    #>
    #> [[1]][[11]]
    #>
    #> Call:
    #> fastglm.default(x = X, y = y, family = quasibinomial(), method = params@fastglm.method)
    #>
    #> Coefficients:
    #>           (Intercept)          tx_init_bas1              followup
    #>           -4.4714e+00            1.8526e-01            3.3744e-02
    #>           followup_sq                 trial              trial_sq
    #>           -5.0528e-05           -3.0973e-03            1.0649e-03
    #>                  sex1                 N_bas                 L_bas
    #>            1.0695e-01            4.4074e-03           -2.8084e-02
    #>                 P_bas tx_init_bas1:followup
    #>           -6.3078e-02            1.7363e-03
    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.7804726 0.8940438 0.02897280
    #> 2:    ITT      1 0.8744359 0.8140039 0.9348679 0.03083321
    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.9480158   1.150592        0.03717768 -0.04574778
    #> 2: risk_1 risk_0  0.9574838  0.8691175   1.054835       -0.03717768 -0.12010314
    #>    RD 95% UCI
    #>         <num>
    #> 1: 0.12010314
    #> 2: 0.04574778