Run Cox Analysis in Batch Mode

ezcox(
  data,
  covariates,
  controls = NULL,
  time = "time",
  status = "status",
  global_method = c("likelihood", "wald", "logrank"),
  keep_models = FALSE,
  return_models = FALSE,
  model_dir = file.path(tempdir(), "ezcox"),
  verbose = TRUE,
  ...
)

Arguments

data

a data.frame containing variables, time and os status.

covariates

column names specifying variables.

controls

column names specifying controls. The names with pattern "*:|()" will be treated as interaction/combination term, please make sure all column names in data are valid R variable names.

time

column name specifying time, default is 'time'.

status

column name specifying event status, default is 'status'.

global_method

method used to obtain global p value for cox model, should be one of "likelihood", "wald", "logrank". The likelihood-ratio test, Wald test, and score logrank statistics. These three methods are asymptotically equivalent. For large enough N, they will give similar results. For small N, they may differ somewhat. The Likelihood ratio test has better behavior for small sample sizes, so it is generally preferred.

keep_models

If TRUE, keep models as local files.

return_models

default FALSE. If TRUE, return a list contains cox models.

model_dir

a path for storing model results.

verbose

if TRUE, print extra info.

...

other parameters passing to survival::coxph().

Value

a ezcox object

Author

Shixiang Wang w_shixiang@163.com

Examples

library(survival)

# Build unvariable models
t1 <- ezcox(lung, covariates = c("age", "sex", "ph.ecog"))
#> => Processing variable age
#> ==> Building Surv object...
#> ==> Building Cox model...
#> ==> Done.
#> => Processing variable sex
#> ==> Building Surv object...
#> ==> Building Cox model...
#> ==> Done.
#> => Processing variable ph.ecog
#> ==> Building Surv object...
#> ==> Building Cox model...
#> ==> Done.
t1
#> # A tibble: 3 × 12
#>   Variable is_cont…¹ contr…² ref_l…³ n_con…⁴ n_ref    beta    HR lower…⁵ upper…⁶
#>   <chr>    <lgl>     <chr>   <chr>     <int> <int>   <dbl> <dbl>   <dbl>   <dbl>
#> 1 age      FALSE     age     age         228   228  0.0187 1.02    1       1.04 
#> 2 sex      FALSE     sex     sex         228   228 -0.531  0.588   0.424   0.816
#> 3 ph.ecog  FALSE     ph.ecog ph.ecog     227   227  0.476  1.61    1.29    2.01 
#> # … with 2 more variables: p.value <dbl>, global.pval <dbl>, and abbreviated
#> #   variable names ¹​is_control, ²​contrast_level, ³​ref_level, ⁴​n_contrast,
#> #   ⁵​lower_95, ⁶​upper_95

# Build multi-variable models
# Control variable 'age'
t2 <- ezcox(lung, covariates = c("sex", "ph.ecog"), controls = "age")
#> => Processing variable sex
#> ==> Building Surv object...
#> ==> Building Cox model...
#> ==> Done.
#> => Processing variable ph.ecog
#> ==> Building Surv object...
#> ==> Building Cox model...
#> ==> Done.
t2
#> # A tibble: 4 × 12
#>   Variable is_cont…¹ contr…² ref_l…³ n_con…⁴ n_ref    beta    HR lower…⁵ upper…⁶
#>   <chr>    <lgl>     <chr>   <chr>     <int> <int>   <dbl> <dbl>   <dbl>   <dbl>
#> 1 sex      FALSE     sex     sex         228   228 -0.513  0.599   0.431   0.831
#> 2 sex      TRUE      age     age         228   228  0.017  1.02    0.999   1.04 
#> 3 ph.ecog  FALSE     ph.ecog ph.ecog     227   227  0.443  1.56    1.24    1.96 
#> 4 ph.ecog  TRUE      age     age         228   228  0.0113 1.01    0.993   1.03 
#> # … with 2 more variables: p.value <dbl>, global.pval <dbl>, and abbreviated
#> #   variable names ¹​is_control, ²​contrast_level, ³​ref_level, ⁴​n_contrast,
#> #   ⁵​lower_95, ⁶​upper_95

# Return models
t3 <- ezcox(lung,
  covariates = c("age", "sex", "ph.ecog"),
  return_models = TRUE
)
#> => Processing variable age
#> ==> Building Surv object...
#> ==> Building Cox model...
#> ==> Done.
#> => Processing variable sex
#> ==> Building Surv object...
#> ==> Building Cox model...
#> ==> Done.
#> => Processing variable ph.ecog
#> ==> Building Surv object...
#> ==> Building Cox model...
#> ==> Done.
t3
#> $res
#> # A tibble: 3 × 12
#>   Variable is_cont…¹ contr…² ref_l…³ n_con…⁴ n_ref    beta    HR lower…⁵ upper…⁶
#>   <chr>    <lgl>     <chr>   <chr>     <int> <int>   <dbl> <dbl>   <dbl>   <dbl>
#> 1 age      FALSE     age     age         228   228  0.0187 1.02    1       1.04 
#> 2 sex      FALSE     sex     sex         228   228 -0.531  0.588   0.424   0.816
#> 3 ph.ecog  FALSE     ph.ecog ph.ecog     227   227  0.476  1.61    1.29    2.01 
#> # … with 2 more variables: p.value <dbl>, global.pval <dbl>, and abbreviated
#> #   variable names ¹​is_control, ²​contrast_level, ³​ref_level, ⁴​n_contrast,
#> #   ⁵​lower_95, ⁶​upper_95
#> 
#> $models
#> # A tibble: 3 × 5
#>   Variable control model_file                                     model   status
#>   <chr>    <chr>   <chr>                                          <list>  <lgl> 
#> 1 age      NA      /var/folders/bj/nw1w4g1j37ddpgb6zmh3sfh80000g… <coxph> TRUE  
#> 2 sex      NA      /var/folders/bj/nw1w4g1j37ddpgb6zmh3sfh80000g… <coxph> TRUE  
#> 3 ph.ecog  NA      /var/folders/bj/nw1w4g1j37ddpgb6zmh3sfh80000g… <coxph> TRUE  
#> 
#> attr(,"class")
#> [1] "ezcox" "list" 
#> attr(,"controls")
#> character(0)
t4 <- ezcox(lung,
  covariates = c("sex", "ph.ecog"), controls = "age",
  return_models = TRUE
)
#> => Processing variable sex
#> ==> Building Surv object...
#> ==> Building Cox model...
#> ==> Done.
#> => Processing variable ph.ecog
#> ==> Building Surv object...
#> ==> Building Cox model...
#> ==> Done.
t4
#> $res
#> # A tibble: 4 × 12
#>   Variable is_cont…¹ contr…² ref_l…³ n_con…⁴ n_ref    beta    HR lower…⁵ upper…⁶
#>   <chr>    <lgl>     <chr>   <chr>     <int> <int>   <dbl> <dbl>   <dbl>   <dbl>
#> 1 sex      FALSE     sex     sex         228   228 -0.513  0.599   0.431   0.831
#> 2 sex      TRUE      age     age         228   228  0.017  1.02    0.999   1.04 
#> 3 ph.ecog  FALSE     ph.ecog ph.ecog     227   227  0.443  1.56    1.24    1.96 
#> 4 ph.ecog  TRUE      age     age         228   228  0.0113 1.01    0.993   1.03 
#> # … with 2 more variables: p.value <dbl>, global.pval <dbl>, and abbreviated
#> #   variable names ¹​is_control, ²​contrast_level, ³​ref_level, ⁴​n_contrast,
#> #   ⁵​lower_95, ⁶​upper_95
#> 
#> $models
#> # A tibble: 2 × 5
#>   Variable control model_file                                     model   status
#>   <chr>    <chr>   <chr>                                          <list>  <lgl> 
#> 1 sex      age     /var/folders/bj/nw1w4g1j37ddpgb6zmh3sfh80000g… <coxph> TRUE  
#> 2 ph.ecog  age     /var/folders/bj/nw1w4g1j37ddpgb6zmh3sfh80000g… <coxph> TRUE  
#> 
#> attr(,"class")
#> [1] "ezcox" "list" 
#> attr(,"controls")
#>   age 
#> "age"