library(ezcox)
library(survival)

lung$ph.ecog <- factor(lung$ph.ecog)

ezcox_group(lung, grp_var = "sex", covariate = "ph.ecog")
#> => Processing variable 1
#> ==> Building Surv object...
#> ==> Building Cox model...
#> ==> Done.
#> => Processing variable 2
#> ==> Building Surv object...
#> ==> Building Cox model...
#> ==> Done.
#> covariates=NULL but drop_controls=TRUE, detecting controls...
#> No. Skipping...
#> Done.
#> Returns a list containing data and ggplot.
#> $data
#> $stats
#> # A tibble: 6 × 13
#>   Group Variable is_control contrast_level ref_level n_contrast n_ref   beta
#>   <chr> <chr>    <lgl>      <chr>          <chr>          <dbl> <dbl>  <dbl>
#> 1 1     ph.ecog  FALSE      1              0                 71    36  0.364
#> 2 1     ph.ecog  FALSE      2              0                 29    36  0.819
#> 3 1     ph.ecog  FALSE      3              0                  1    36  1.9  
#> 4 2     ph.ecog  FALSE      1              0                 42    27  0.416
#> 5 2     ph.ecog  FALSE      2              0                 21    27  1.22 
#> 6 2     ph.ecog  FALSE      3              0                  0    27 NA    
#> # … with 5 more variables: HR <dbl>, lower_95 <dbl>, upper_95 <dbl>,
#> #   p.value <dbl>, global.pval <dbl>
#> 
#> $models
#> # A tibble: 2 × 6
#>   Group Variable control model_file                                 model status
#>   <chr> <chr>    <chr>   <chr>                                      <lis> <lgl> 
#> 1 1     ph.ecog  <NA>    /var/folders/bj/nw1w4g1j37ddpgb6zmh3sfh80… <cox… TRUE  
#> 2 2     ph.ecog  <NA>    /var/folders/bj/nw1w4g1j37ddpgb6zmh3sfh80… <cox… TRUE  
#> 
#> attr(,"class")
#> [1] "ezcox"
#> 
#> $plot

ezcox_group(lung, grp_var = "sex", covariate = "ph.ecog", controls = "age")
#> => Processing variable 1
#> ==> Building Surv object...
#> ==> Building Cox model...
#> ==> Done.
#> => Processing variable 2
#> ==> Building Surv object...
#> ==> Building Cox model...
#> ==> Done.
#> covariates=NULL but drop_controls=TRUE, detecting controls...
#> Yes. Setting variables to keep...
#> Done.
#> Returns a list containing data and ggplot.
#> $data
#> $stats
#> # A tibble: 8 × 13
#>   Group Variable is_control contrast_level ref_level n_contrast n_ref    beta
#>   <chr> <chr>    <lgl>      <chr>          <chr>          <dbl> <dbl>   <dbl>
#> 1 1     ph.ecog  FALSE      1              0                 71    36  0.353 
#> 2 1     ph.ecog  FALSE      2              0                 29    36  0.809 
#> 3 1     ph.ecog  FALSE      3              0                  1    36  1.77  
#> 4 1     ph.ecog  TRUE       age            age              138   138  0.0182
#> 5 2     ph.ecog  FALSE      1              0                 42    27  0.439 
#> 6 2     ph.ecog  FALSE      2              0                 21    27  1.41  
#> 7 2     ph.ecog  FALSE      3              0                  0    27 NA     
#> 8 2     ph.ecog  TRUE       age            age               90    90 -0.0153
#> # … with 5 more variables: HR <dbl>, lower_95 <dbl>, upper_95 <dbl>,
#> #   p.value <dbl>, global.pval <dbl>
#> 
#> $models
#> # A tibble: 2 × 6
#>   Group Variable control model_file                                 model status
#>   <chr> <chr>    <chr>   <chr>                                      <lis> <lgl> 
#> 1 1     ph.ecog  age     /var/folders/bj/nw1w4g1j37ddpgb6zmh3sfh80… <cox… TRUE  
#> 2 2     ph.ecog  age     /var/folders/bj/nw1w4g1j37ddpgb6zmh3sfh80… <cox… TRUE  
#> 
#> attr(,"class")
#> [1] "ezcox"
#> 
#> $plot

ezcox_group(lung, grp_var = "sex", covariate = "ph.ecog", controls = "age", add_all = TRUE)
#> => Processing variable 1
#> ==> Building Surv object...
#> ==> Building Cox model...
#> ==> Done.
#> => Processing variable 2
#> ==> Building Surv object...
#> ==> Building Cox model...
#> ==> Done.
#> => Processing variable ALL
#> ==> Building Surv object...
#> ==> Building Cox model...
#> ==> Done.
#> covariates=NULL but drop_controls=TRUE, detecting controls...
#> Yes. Setting variables to keep...
#> Done.
#> Returns a list containing data and ggplot.
#> $data
#> $stats
#> # A tibble: 12 × 13
#>    Group Variable is_control contrast_level ref_level n_contrast n_ref    beta
#>    <chr> <chr>    <lgl>      <chr>          <chr>          <dbl> <dbl>   <dbl>
#>  1 1     ph.ecog  FALSE      1              0                 71    36  0.353 
#>  2 1     ph.ecog  FALSE      2              0                 29    36  0.809 
#>  3 1     ph.ecog  FALSE      3              0                  1    36  1.77  
#>  4 1     ph.ecog  TRUE       age            age              138   138  0.0182
#>  5 2     ph.ecog  FALSE      1              0                 42    27  0.439 
#>  6 2     ph.ecog  FALSE      2              0                 21    27  1.41  
#>  7 2     ph.ecog  FALSE      3              0                  0    27 NA     
#>  8 2     ph.ecog  TRUE       age            age               90    90 -0.0153
#>  9 ALL   ph.ecog  FALSE      1              0                113    63  0.359 
#> 10 ALL   ph.ecog  FALSE      2              0                 50    63  0.857 
#> 11 ALL   ph.ecog  FALSE      3              0                  1    63  2.11  
#> 12 ALL   ph.ecog  TRUE       age            age              228   228  0.0108
#> # … with 5 more variables: HR <dbl>, lower_95 <dbl>, upper_95 <dbl>,
#> #   p.value <dbl>, global.pval <dbl>
#> 
#> $models
#> # A tibble: 3 × 6
#>   Group Variable control model_file                                 model status
#>   <chr> <chr>    <chr>   <chr>                                      <lis> <lgl> 
#> 1 1     ph.ecog  age     /var/folders/bj/nw1w4g1j37ddpgb6zmh3sfh80… <cox… TRUE  
#> 2 2     ph.ecog  age     /var/folders/bj/nw1w4g1j37ddpgb6zmh3sfh80… <cox… TRUE  
#> 3 ALL   ph.ecog  age     /var/folders/bj/nw1w4g1j37ddpgb6zmh3sfh80… <cox… TRUE  
#> 
#> attr(,"class")
#> [1] "ezcox"
#> 
#> $plot