rm(list=ls())
library(RobinCar)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

Robust Cox Score

Simulate data

Simulate data with permuted block randomization scheme using helper functions.

set.seed(10)
n <- 100

# Simulate data
data.simu0 <- data_gen2(
  n=n,
  theta=0,
  randomization="permuted_block",
  p_trt=0.5,
  case="case1"
)

# Restructure data
data.simu <- data.simu0 %>%
  tidyr::pivot_longer(
    cols=starts_with("car_strata"),
    names_prefix="car_strata",
    names_to="strt"
  ) %>%
  dplyr::filter(value == 1) %>%
  dplyr::select(-value) %>%
  dplyr::mutate(strt=forcats::as_factor(strt)) %>%
  dplyr::select(t, strt) %>%
  dplyr::left_join(data.simu0, .)
#> Joining with `by = join_by(t)`

Fit a Cox score model

cox <- robincar_coxscore(
  df=data.simu,
  treat_col="I1",
  response_col="t",
  event_col="delta",
  car_strata_cols="strt",
  covariate_cols=c("model_w3"),
  car_scheme="permuted-block",
  ref_arm=0
)
print(cox)
#> Performed coxscore test, 
#>   adjusting for covariates model_w3, 
#>   adjusting SE for car_strata strt
#> ------------------------------------
#>    Treatment N.total N.events
#>       <char>   <num>    <num>
#> 1:    I1 = 0      48       48
#> 2:    I1 = 1      52       52
#> 
#> Reference arm is  I1 = 0 
#> 
#> Score function: 2.127963 
#> Standard error: 4.475487 
#> Test statistic: 0.4754706 
#> 2-side p-value: 0.6344516

Fit a covariate-adjusted (stratified) logrank test

Use adj_method = "CL" for covariate-adjusted logrank test.

cl <- robincar_logrank(
  df=data.simu,
  treat_col="I1",
  response_col="t",
  event_col="delta",
  car_strata_cols="strt",
  covariate_cols=c("model_w3"),
  car_scheme="permuted-block",
  adj_method="CL",
  ref_arm=0
)
print(cl)
#> Performed covariate-adjusted logrank test with covariates  model_w3,  strt, 
#> ------------------------------------
#>    Treatment N.total N.events
#>       <char>   <num>    <num>
#> 1:    I1 = 0      48       48
#> 2:    I1 = 1      52       52
#> 
#> Reference arm is  I1 = 0 
#> 
#> Score function: 0.0428465 
#> Standard error: 0.04075915 
#> Test statistic: 1.051212 
#> 2-side p-value: 0.2931612

Use adj_method = "CSL" for covariate-adjusted stratified logrank test.

csl <- robincar_logrank(
  df=data.simu,
  treat_col="I1",
  response_col="t",
  event_col="delta",
  car_strata_cols="strt",
  covariate_cols=c("model_w3"),
  car_scheme="permuted-block",
  adj_method="CSL",
  ref_arm=0
)
print(csl)
#> Performed covariate-adjusted stratified logrank test with covariates  model_w3,   and stratifying by  strt, 
#> ------------------------------------
#>             Strata Treatment N.total N.events
#>             <char>    <char>   <num>    <num>
#>  1: car_strata = 1    I1 = 0      12       12
#>  2: car_strata = 1    I1 = 1       6        6
#>  3: car_strata = 2    I1 = 0       7        7
#>  4: car_strata = 2    I1 = 1       9        9
#>  5: car_strata = 3    I1 = 0      13       13
#>  6: car_strata = 3    I1 = 1       9        9
#>  7: car_strata = 4    I1 = 0       6        6
#>  8: car_strata = 4    I1 = 1      15       15
#>  9: car_strata = 5    I1 = 0       2        2
#> 10: car_strata = 5    I1 = 1       6        6
#> 11: car_strata = 6    I1 = 0       8        8
#> 12: car_strata = 6    I1 = 1       7        7
#> 
#> Reference arm is  I1 = 0 
#> 
#> Score function: 0.03009601 
#> Standard error: 0.04256389 
#> Test statistic: 0.7070785 
#> 2-side p-value: 0.4795177