rm(list=ls())
library(RobinCar)

References

Ting Ye, Jun Shao, Yanyao Yi, Qinyuan Zhao (2022+). Toward Better Practice of Covariate Adjustment in Analyzing Randomized Clinical Trials. Journal of the American Statistical Association, in press.

Ting Ye, Marlena Bannick, Yanyao Yi, Jun Shao (2023). Robust variance estimation for covariate-adjusted unconditional treatment effect in randomized clinical trials with binary outcomes. Statistical Theory and Related Fields, 7(2):159-163.

Marlena Bannick, Jun Shao, Jingyi Liu, Yu Du, Yanyao Yi, Ting Ye (2023+). A General Form of Covariate Adjustment in Randomized Clinical Trials.

Adjustment with Continuous Outcomes

The dataset that we will use for this is included in the package.

data <- RobinCar:::data_sim
data$A <- as.factor(data$A)

ANOVA Model

fit.anova <- robincar_linear(df = data, 
                              response_col="y",
                              treat_col="A",
                              covariate_cols=c("x1", "x3"),
                              car_scheme="simple",
                              adj_method="ANOVA")

ANCOVA model with biased coin randomization

fit.ancova <- robincar_linear(df = data, 
                               response_col="y",
                               treat_col="A",
                               car_strata_cols=c("z1", "z2"),
                               covariate_cols=c("x1", "x3"),
                               car_scheme="biased-coin",
                               adj_method="ANCOVA")

ANHECOVA model with Pocock-Simon randomization and linear contrast

Include strata variables in the covariates.

fit.anhecova <- robincar_linear(df = data, 
                                 response_col="y",
                                 treat_col="A",
                                 car_strata_cols=c("z1", "z2"),
                                 covariate_cols=c("x1", "x3", "z1", "z2"),
                                 car_scheme="pocock-simon",
                                 adj_method="ANHECOVA",
                                 contrast_h="diff")
#> Warning in func(): We suggest you try out robin_calibrate.
#> Warning in func(): Prediction unbiasedness does not hold in all joint levels of
#> Z.

Adjustment with Binary Outcomes

Dichotomize the continuous outcome.

data$y_bin <- ifelse(data$y > 2, 1, 0)

Homogeneous working model with simple randomization

glm.homogeneous<-robincar_glm(df = data,
                               response_col="y_bin",
                               treat_col="A",
                               car_strata_cols=c("z1"),
                               formula="y_bin ~ A",
                               car_scheme="pocock-simon",
                               g_family=binomial(link="logit"),
                               g_accuracy=7)
#> Warning in func(): We suggest you try out robin_calibrate.
#> Warning in func(): Prediction unbiasedness does not hold in all joint levels of
#> Z.
glm.homogeneous
#> Treatment group mean estimates from a GLM working model of family binomial and link logit using formula: 
#> response ~ treat
#> 
#> 
#> Used HC0-type of heteroskedasticity-consistent variance estimates 
#> 
#> Estimates:
#> # A tibble: 3 × 4
#>   treat estimate     se `pval (2-sided)`
#>   <chr>    <dbl>  <dbl>            <dbl>
#> 1 0        0.554 0.0308        2.02e- 72
#> 2 1        0.763 0.0293        1.87e-149
#> 3 2        0.712 0.0321        4.70e-109
#> 
#> Variance-Covariance Matrix:
#>           [,1]         [,2]         [,3]
#> 0 9.470821e-04 1.140478e-04 5.004661e-05
#> 1 1.140478e-04 8.597323e-04 1.490513e-05
#> 2 5.004661e-05 1.490513e-05 1.030656e-03

Heterogeneous working model with permuted-block randomization, using one covariate

glm.heterogeneous<-robincar_glm(df = data,
                               response_col="y_bin",
                               treat_col="A",
                               car_strata_cols=c("z2"),
                               formula="y_bin ~ A * (x1 + z2)",
                               car_scheme="biased-coin",
                               g_family=binomial(link="logit"),
                               g_accuracy=7)

glm.heterogeneous
#> Treatment group mean estimates from a GLM working model of family binomial and link logit using formula: 
#> response ~ treat * (x1 + z2)
#> 
#> 
#> Used HC0-type of heteroskedasticity-consistent variance estimates 
#> and adjusted variance-covariance matrix for randomization car_strata z2 
#> consistent with the biased-coin design.
#> 
#> Estimates:
#> # A tibble: 3 × 4
#>   treat estimate     se `pval (2-sided)`
#>   <chr>    <dbl>  <dbl>            <dbl>
#> 1 0        0.559 0.0297        3.27e- 79
#> 2 1        0.771 0.0273        3.37e-176
#> 3 2        0.711 0.0308        5.25e-118
#> 
#> Variance-Covariance Matrix:
#>           [,1]         [,2]         [,3]
#> 0 8.810939e-04 7.535047e-05 1.730168e-05
#> 1 7.535047e-05 7.431284e-04 7.862983e-05
#> 2 1.730168e-05 7.862983e-05 9.482230e-04

Heterogeneous working model with biased-coin randomization, differences contrast

glm.heterogeneous <- robincar_glm(df = data,
                                   response_col="y_bin",
                                   treat_col="A",
                                   car_strata_cols=c("z2"),
                                   formula="y_bin ~ A * (x1 + z2)",
                                   car_scheme="biased-coin",
                                   g_family=binomial(link="logit"),
                                   g_accuracy=7,
                                   contrast_h="diff")
glm.heterogeneous
#> $main
#> Treatment group mean estimates from a GLM working model of family binomial and link logit using formula: 
#> response ~ treat * (x1 + z2)
#> 
#> 
#> Used HC0-type of heteroskedasticity-consistent variance estimates 
#> and adjusted variance-covariance matrix for randomization car_strata z2 
#> consistent with the biased-coin design.
#> 
#> Estimates:
#> # A tibble: 3 × 4
#>   treat estimate     se `pval (2-sided)`
#>   <chr>    <dbl>  <dbl>            <dbl>
#> 1 0        0.559 0.0297        3.27e- 79
#> 2 1        0.771 0.0273        3.37e-176
#> 3 2        0.711 0.0308        5.25e-118
#> 
#> Variance-Covariance Matrix:
#>           [,1]         [,2]         [,3]
#> 0 8.810939e-04 7.535047e-05 1.730168e-05
#> 1 7.535047e-05 7.431284e-04 7.862983e-05
#> 2 1.730168e-05 7.862983e-05 9.482230e-04
#> 
#> $contrast
#> Treatment group contrasts using linear contrast
#> 
#> Contrasts:
#> # A tibble: 2 × 4
#>   contrast    estimate     se `pval (2-sided)`
#>   <chr>          <dbl>  <dbl>            <dbl>
#> 1 treat 1 - 0    0.212 0.0384     0.0000000327
#> 2 treat 2 - 0    0.152 0.0424     0.000340    
#> 
#> Variance-Covariance Matrix for Contrasts:
#>              [,1]         [,2]
#> [1,] 0.0014735214 0.0008670716
#> [2,] 0.0008670716 0.0017947136