Skip to contents

Creates a matrix suitable for create_analysis_obj(). Creates dummy variables for factors and allows transformations of covariates specified with a formula.

Usage

create_data_matrix(
  data,
  outcome,
  trt_flag_col,
  ext_flag_col,
  covariates = NULL,
  weight_var = NULL
)

Arguments

data

data.frame. Data containing all variables

outcome

character. The outcome variable for binary outcomes or the time and censoring variables.

trt_flag_col

character. The treatment indicator variable.

ext_flag_col

character. The external cohort indicator.

covariates

character or formula. The covariates for model adjustment.

weight_var

character. An optional weight variable.

Value

Invisibly returns a matrix containing all variables to pass to create_analysis_obj(). Prints names of covariates columns to use with add_covariates().

Examples

dat <- survival::diabetic
dat$ext <- dat$trt == 0 & dat$id > 1000
data_mat <- create_data_matrix(
  dat,
  outcome = c("time", "status"),
  trt_flag_col = "trt",
  ext_flag_col = "ext",
  covariates = ~ age + laser + log(risk)
)
#> Call `add_covariates()` with `covariates =  c("age", "laserargon", "log(risk)") `
data_mat
#>      time status trt extTRUE age laserargon log(risk)
#> 1   46.23      0   0       0  28          1  2.197225
#> 2   46.23      0   1       0  28          1  2.197225
#> 3   42.50      0   1       0  12          0  2.079442
#> 4   31.30      1   0       0  12          0  1.791759
#> 5   42.27      0   1       0   9          0  2.397895
#> 6   42.27      0   0       0   9          0  2.397895
#> 7   20.60      0   0       0   9          0  2.397895
#> 8   20.60      0   1       0   9          0  2.397895
#> 9    0.30      1   0       0  13          0  2.302585
#> 10  38.77      0   1       0  13          0  2.197225
#> 11  65.23      0   1       0  12          0  2.197225
#> 12  54.27      1   0       0  12          0  2.197225
#> 13  63.50      0   1       0   8          0  2.079442
#> 14  10.80      1   0       0   8          0  1.791759
#> 15  23.17      0   1       0  12          0  2.079442
#> 16  23.17      0   0       0  12          0  2.197225
#> 17   1.47      0   1       0  16          0  2.197225
#> 18   1.47      0   0       0  16          0  2.302585
#> 19  58.07      0   1       0  21          1  2.197225
#> 20  13.83      1   0       0  21          1  2.197225
#> 21  48.53      0   0       0  23          1  2.197225
#> 22  46.43      1   1       0  23          1  2.197225
#> 23  44.40      0   1       0  44          1  2.397895
#> 24   7.90      1   0       0  44          1  2.484907
#> 25  39.57      0   0       0  47          1  1.791759
#> 26  39.57      0   1       0  47          1  2.397895
#> 27  30.83      1   1       0  48          1  1.791759
#> 28  38.57      1   0       0  48          1  2.302585
#> 29  66.27      0   1       0  26          1  2.302585
#> 30  14.10      1   0       0  26          1  2.197225
#> 31   6.90      1   0       0  10          0  2.302585
#> 32  20.17      1   1       0  10          0  2.197225
#> 33  41.40      1   0       0  23          1  2.197225
#> 34  58.43      0   1       0  23          1  2.484907
#> 35  58.20      0   1       0   5          0  2.197225
#> 36  58.20      0   0       0   5          0  2.197225
#> 37  57.43      0   1       0  46          1  1.791759
#> 38  57.43      0   0       0  46          1  2.079442
#> 39  56.03      0   1       0   5          0  2.397895
#> 40  56.03      0   0       0   5          0  2.484907
#> 41  67.53      0   0       0  13          0  2.197225
#> 42  67.53      0   1       0  13          0  2.197225
#> 43  61.40      0   1       0  45          1  2.197225
#> 44   0.60      1   0       0  45          1  2.484907
#> 45  10.27      1   1       0  11          0  2.302585
#> 46   1.63      1   0       0  11          0  2.302585
#> 47  66.20      0   0       0   1          0  2.397895
#> 48  66.20      0   1       0   1          0  2.484907
#> 49  13.83      1   0       0  10          0  2.197225
#> 50   5.67      1   1       0  10          0  2.484907
#> 51  58.83      0   1       0  12          0  2.197225
#> 52  29.97      1   0       0  12          0  2.397895
#> 53  60.27      0   1       0  36          1  2.197225
#> 54  26.37      1   0       0  36          1  2.397895
#> 55   1.33      1   0       0  53          1  2.484907
#> 56   5.77      1   1       0  53          1  2.302585
#> 57  35.53      1   0       0  10          0  2.397895
#> 58   5.90      1   1       0  10          0  2.397895
#> 59  21.90      1   0       0  25          1  2.197225
#> 60  25.63      1   1       0  25          1  2.302585
#> 61  14.80      1   0       0  14          0  2.197225
#> 62  33.90      1   1       0  14          0  2.197225
#> 63   6.20      1   0       0  16          0  2.079442
#> 64   1.73      1   1       0  16          0  2.302585
#> 65  22.00      1   0       0  38          1  2.079442
#> 66  46.90      0   1       0  38          1  2.197225
#> 67  31.13      0   0       0  14          0  2.197225
#> 68  31.13      0   1       0  14          0  2.197225
#> 69  22.00      1   0       0  10          0  2.397895
#> 70  30.20      1   1       0  10          0  2.197225
#> 71  70.90      0   0       0  17          0  2.484907
#> 72  70.90      0   1       0  17          0  2.397895
#> 73  25.80      1   1       0  44          1  2.397895
#> 74  13.87      1   0       0  44          1  2.079442
#> 75  48.30      1   0       0  21          1  2.197225
#> 76   5.73      1   1       0  21          1  2.197225
#> 77  53.43      0   0       0  19          0  2.197225
#> 78  53.43      0   1       0  19          0  2.197225
#> 79   1.90      1   1       0  13          0  2.302585
#> 80  51.10      0   0       0  13          0  2.484907
#> 81   9.90      1   0       0  40          1  2.197225
#> 82   9.90      1   1       0  40          1  2.302585
#> 83  34.20      0   1       0   9          0  2.079442
#> 84  34.20      0   0       0   9          0  2.484907
#> 85   2.67      1   0       0  48          1  2.484907
#> 86  46.73      0   1       0  48          1  2.302585
#> 87  18.73      0   1       0  42          1  2.302585
#> 88  13.83      1   0       0  42          1  2.302585
#> 89  32.03      0   1       0  24          1  2.397895
#> 90   4.27      1   0       0  24          1  2.302585
#> 91  13.90      1   0       0  55          1  2.302585
#> 92  69.87      0   1       0  55          1  2.302585
#> 93  66.80      0   0       0  17          0  2.197225
#> 94  66.80      0   1       0  17          0  2.397895
#> 95  64.73      0   1       0   5          0  2.197225
#> 96  64.73      0   0       0   5          0  2.079442
#> 97   1.70      1   1       0   6          0  2.302585
#> 98   1.70      1   0       0   6          0  1.791759
#> 99   1.77      1   1       0  19          0  2.197225
#> 100 43.03      1   0       0  19          0  2.484907
#> 101 29.03      0   1       0  12          0  2.079442
#> 102 29.03      0   0       0  12          0  2.079442
#> 103 56.57      0   0       0  45          1  2.197225
#> 104 56.57      0   1       0  45          1  2.197225
#> 105  8.30      1   0       0  27          1  2.302585
#> 106  8.30      1   1       0  27          1  2.197225
#> 107 21.57      0   1       0  43          1  2.079442
#> 108 18.43      1   0       0  43          1  2.197225
#> 109 31.57      0   0       0   4          0  2.079442
#> 110 31.57      0   1       0   4          0  2.197225
#> 111 31.63      1   0       0  45          1  2.197225
#> 112 31.63      0   1       0  45          1  2.302585
#> 113 39.77      0   1       0  32          1  2.397895
#> 114 39.77      0   0       0  32          1  2.302585
#> 115  6.53      1   0       0   3          0  2.397895
#> 116 18.70      1   1       0   3          0  2.302585
#> 117 18.90      0   1       0  14          0  2.079442
#> 118 18.90      0   0       0  14          0  2.197225
#> 119 56.80      0   1       0  13          0  2.302585
#> 120 22.23      1   0       0  13          0  2.079442
#> 121 55.60      0   1       0  15          0  2.302585
#> 122 14.00      1   0       0  15          0  2.197225
#> 123 42.17      1   0       0  10          0  2.302585
#> 124 42.17      1   1       0  10          0  2.197225
#> 125  5.33      1   0       0   6          0  2.197225
#> 126 10.70      0   1       0   6          0  2.197225
#> 127 59.80      1   0       0  17          0  2.302585
#> 128 66.33      0   1       0  17          0  2.197225
#> 129  5.83      1   0       0  37          1  2.397895
#> 130 52.33      0   1       0  37          1  2.484907
#> 131 58.17      0   1       0  18          0  2.197225
#> 132  2.17      1   0       0  18          0  2.484907
#> 133 48.43      1   0       0  13          0  2.079442
#> 134 14.30      1   1       0  13          0  2.197225
#> 135 25.83      0   0       0  14          0  2.302585
#> 136 25.83      0   1       0  14          0  2.484907
#> 137 45.40      0   1       0  12          0  2.302585
#> 138 45.40      0   0       0  12          0  2.197225
#> 139 47.60      0   1       0   9          0  1.791759
#> 140 47.60      0   0       0   9          0  2.197225
#> 141  9.60      1   0       0  11          0  2.302585
#> 142 13.33      1   1       0  11          0  2.484907
#> 143 42.10      0   0       0  10          0  2.397895
#> 144 42.10      0   1       0  10          0  2.397895
#> 145 39.93      0   1       0   5          0  2.197225
#> 146 39.93      0   0       0   5          0  1.791759
#> 147  7.60      1   0       0  15          0  2.484907
#> 148 14.27      1   1       0  15          0  2.197225
#> 149  1.80      1   0       0   7          0  2.484907
#> 150 34.57      1   1       0   7          0  2.484907
#> 151  4.30      1   0       0   2          0  2.484907
#> 152 65.80      0   1       0   2          0  2.197225
#> 153 12.20      1   0       0  22          1  1.791759
#> 154  4.10      1   1       0  22          1  2.079442
#> 155 60.93      0   0       0   5          0  2.484907
#> 156 60.93      0   1       0   5          0  2.397895
#> 157 57.20      0   0       0   4          0  2.197225
#> 158 57.20      0   1       0   4          0  2.302585
#> 159 38.07      0   1       0  27          1  2.079442
#> 160 12.73      1   0       0  27          1  2.302585
#> 161 54.10      0   1       0  53          1  2.397895
#> 162 54.10      1   0       0  53          1  2.397895
#> 163 59.27      0   1       0  10          0  2.197225
#> 164  9.40      1   0       0  10          0  2.484907
#> 165  9.90      1   0       0  13          0  2.302585
#> 166 21.57      1   1       0  13          0  2.484907
#> 167 54.10      0   0       0  12          0  2.079442
#> 168 54.10      0   1       0  12          0  2.079442
#> 169 50.47      0   0       0  24          1  2.397895
#> 170 50.47      0   1       0  24          1  2.197225
#> 171 46.17      0   0       0  17          0  2.484907
#> 172 46.17      0   1       0  17          0  2.197225
#> 173 46.30      0   0       0   8          0  2.397895
#> 174 46.30      0   1       0   8          0  2.397895
#> 175 38.83      0   1       0  58          1  2.484907
#> 176 38.83      0   0       0  58          1  2.197225
#> 177 44.60      0   0       0  17          0  2.197225
#> 178 44.60      0   1       0  17          0  2.197225
#> 179 43.07      0   0       0  12          0  2.197225
#> 180 43.07      0   1       0  12          0  2.484907
#> 181 40.03      0   0       0  25          1  2.079442
#> 182 26.23      1   1       0  25          1  2.079442
#> 183 41.60      0   1       0  15          0  2.197225
#> 184 18.03      1   0       0  15          0  2.397895
#> 185 38.07      0   0       0  21          1  2.397895
#> 186 38.07      0   1       0  21          1  2.397895
#> 187 65.23      0   1       0  20          1  2.197225
#> 188 65.23      0   0       0  20          1  2.197225
#> 189  7.07      1   1       0  23          1  2.302585
#> 190 66.77      0   0       0  23          1  2.484907
#> 191 13.77      1   0       0  13          0  2.484907
#> 192 13.77      1   1       0  13          0  2.302585
#> 193  9.63      1   0       0  45          1  2.484907
#> 194  9.63      0   1       0  45          1  2.302585
#> 195 46.23      0   1       0   5          0  2.484907
#> 196 46.23      0   0       0   5          0  2.484907
#> 197  1.50      1   0       0   8          0  2.484907
#> 198 45.73      0   1       0   8          0  2.197225
#> 199 33.63      1   0       0  30          1  2.197225
#> 200 33.63      1   1       0  30          1  2.197225
#> 201 40.17      0   0       0   7          0  1.791759
#> 202 40.17      0   1       0   7          0  2.079442
#> 203 27.60      1   0       0  39          1  2.302585
#> 204 63.33      1   1       0  39          1  2.397895
#> 205 38.47      1   1       0  26          1  2.302585
#> 206  1.63      1   0       0  26          1  2.302585
#> 207 55.23      0   1       0  50          1  1.791759
#> 208 55.23      0   0       0  50          1  2.079442
#> 209 25.30      1   0       0  34          1  2.302585
#> 210 52.77      0   1       0  34          1  1.791759
#> 211 46.20      1   0       0  10          0  2.079442
#> 212 57.17      0   1       0  10          0  1.791759
#> 213  9.87      0   1       0  40          1  2.302585
#> 214  1.70      1   0       0  40          1  2.302585
#> 215 57.90      0   0       0  13          0  2.397895
#> 216 57.90      0   1       0  13          0  2.484907
#> 217  5.90      0   0       0   7          0  2.484907
#> 218  5.90      0   1       0   7          0  2.302585
#> 219 32.20      0   0       0  11          0  2.484907
#> 220 32.20      0   1       0  11          0  2.484907
#> 221 10.33      1   1       0  13          0  2.197225
#> 222  0.83      1   0       0  13          0  2.302585
#> 223 50.90      0   0       0   9          0  2.197225
#> 224  6.13      1   1       0   9          0  2.484907
#> 225 25.93      1   0       0   5          0  2.197225
#> 226 43.67      0   1       0   5          0  2.197225
#> 227 38.30      0   0       0  10          0  2.197225
#> 228 38.30      0   1       0  10          0  2.197225
#> 229 38.77      0   1       0  23          1  2.197225
#> 230 19.40      1   0       0  23          1  2.302585
#> 231 21.97      1   0       0   2          0  2.302585
#> 232 38.07      0   1       0   2          0  2.484907
#> 233 38.30      0   1       0  12          0  2.397895
#> 234 38.30      0   0       0  12          0  2.397895
#> 235 70.03      0   0       0   7          0  2.197225
#> 236 26.20      1   1       0   7          0  2.302585
#> 237 18.03      1   0       1  13          0  2.397895
#> 238 62.57      0   1       0  13          0  2.197225
#> 239  1.57      1   0       1  50          1  2.197225
#> 240 13.83      1   1       0  50          1  2.079442
#> 241 46.50      0   1       0  20          1  2.484907
#> 242 13.37      1   0       1  20          1  2.484907
#> 243  1.97      1   0       1  15          0  2.197225
#> 244 11.07      1   1       0  15          0  2.397895
#> 245 42.47      0   1       0  30          1  2.197225
#> 246 22.20      1   0       1  30          1  2.197225
#> 247 38.73      0   0       1  32          1  2.197225
#> 248 38.73      0   1       0  32          1  2.197225
#> 249 51.13      0   0       1  39          1  2.397895
#> 250 51.13      0   1       0  39          1  2.397895
#> 251 46.50      0   0       1   4          0  2.397895
#> 252  6.10      1   1       0   4          0  2.302585
#> 253 11.30      1   0       1   3          0  2.397895
#> 254  2.10      1   1       0   3          0  2.302585
#> 255 17.73      1   1       0  10          0  2.302585
#> 256 42.30      0   0       1  10          0  2.197225
#> 257 26.47      0   0       1   6          0  2.302585
#> 258 26.47      0   1       0   6          0  2.197225
#> 259 10.77      0   1       0  15          0  2.397895
#> 260 10.77      0   0       1  15          0  2.397895
#> 261 55.33      0   1       0  33          1  2.484907
#> 262 55.33      0   0       1  33          1  2.302585
#> 263 58.67      0   1       0  15          0  2.197225
#> 264 58.67      0   0       1  15          0  2.197225
#> 265  4.97      1   0       1  44          1  2.197225
#> 266 12.93      1   1       0  44          1  2.302585
#> 267 26.47      1   0       1  48          1  2.484907
#> 268 54.20      0   1       0  48          1  2.197225
#> 269 49.57      0   0       1   4          0  2.197225
#> 270 49.57      0   1       0   4          0  2.302585
#> 271  9.87      1   0       1  46          1  2.397895
#> 272 24.43      1   1       0  46          1  2.484907
#> 273 50.23      0   0       1  25          1  2.197225
#> 274 50.23      0   1       0  25          1  2.197225
#> 275 30.40      1   0       1  12          0  2.484907
#> 276 13.97      1   1       0  12          0  2.397895
#> 277 43.33      0   1       0  12          0  1.791759
#> 278 43.33      1   0       1  12          0  2.302585
#> 279 42.23      0   1       0  26          1  2.079442
#> 280 42.23      0   0       1  26          1  2.079442
#> 281 74.93      0   0       1  11          0  2.197225
#> 282 74.93      0   1       0  11          0  2.197225
#> 283 66.93      0   1       0  36          1  2.197225
#> 284 66.93      0   0       1  36          1  2.197225
#> 285 73.43      0   1       0  12          0  2.197225
#> 286 73.43      0   0       1  12          0  2.197225
#> 287 67.47      0   1       0  50          1  2.484907
#> 288 38.57      1   0       1  50          1  2.397895
#> 289  3.67      0   1       0  44          1  2.197225
#> 290  3.67      1   0       1  44          1  2.302585
#> 291 67.03      0   0       1   8          0  2.197225
#> 292 48.87      1   1       0   8          0  2.302585
#> 293 65.60      0   1       0  14          0  2.197225
#> 294 65.60      0   0       1  14          0  2.197225
#> 295 15.83      1   0       1  18          0  2.302585
#> 296 15.83      0   1       0  18          0  2.302585
#> 297 20.07      0   1       0  56          1  2.397895
#> 298  8.83      1   0       1  56          1  2.079442
#> 299 67.43      0   0       1   9          0  2.197225
#> 300 67.43      0   1       0   9          0  2.302585
#> 301  1.47      0   1       0  15          0  2.197225
#> 302  1.47      0   0       1  15          0  1.791759
#> 303 62.93      0   1       0   5          0  2.397895
#> 304 22.13      1   0       1   5          0  2.197225
#> 305  6.30      1   1       0   1          0  2.197225
#> 306 56.97      0   0       1   1          0  2.397895
#> 307 59.70      0   1       0   1          0  2.302585
#> 308 18.93      1   0       1   1          0  2.302585
#> 309 19.00      1   0       1  14          0  2.197225
#> 310 13.80      1   1       0  14          0  2.302585
#> 311 55.13      0   1       0  57          1  1.791759
#> 312 55.13      0   0       1  57          1  1.791759
#> 313  5.43      1   0       1   8          0  2.397895
#> 314 13.57      1   1       0   8          0  2.397895
#> 315 42.20      0   1       0  33          1  2.397895
#> 316 42.20      0   0       1  33          1  2.397895
#> 317 38.27      0   0       1  46          1  2.197225
#> 318 38.27      0   1       0  46          1  2.302585
#> 319  7.10      0   1       0   3          0  2.079442
#> 320  7.10      1   0       1   3          0  2.484907
#> 321 26.17      1   0       1  35          1  2.397895
#> 322 63.63      0   1       0  35          1  2.397895
#> 323 24.73      1   0       1   8          0  2.397895
#> 324 59.00      0   1       0   8          0  2.397895
#> 325 54.37      0   1       0  30          1  2.197225
#> 326 54.37      0   0       1  30          1  2.302585
#> 327 54.60      0   1       0  51          1  2.079442
#> 328 10.97      1   0       1  51          1  2.484907
#> 329 21.10      1   0       1  42          1  2.197225
#> 330 63.87      0   1       0  42          1  2.197225
#> 331 62.37      0   1       0  20          1  2.197225
#> 332 43.70      1   0       1  20          1  2.079442
#> 333 62.80      0   0       1  23          1  2.197225
#> 334 62.80      0   1       0  23          1  2.397895
#> 335 63.33      0   1       0  22          1  2.397895
#> 336 14.37      1   0       1  22          1  2.197225
#> 337 58.53      0   0       1  25          1  2.197225
#> 338 58.53      0   1       0  25          1  2.197225
#> 339 58.07      0   0       1  45          1  2.397895
#> 340 58.07      0   1       0  45          1  2.484907
#> 341 58.50      0   0       1  20          1  2.197225
#> 342 58.50      0   1       0  20          1  2.302585
#> 343 14.37      0   0       1  41          1  2.197225
#> 344  1.50      1   1       0  41          1  2.197225
#> 345 54.73      0   1       0  19          0  2.484907
#> 346 38.40      1   0       1  19          0  2.302585
#> 347 50.63      0   1       0   4          0  2.484907
#> 348  2.83      1   0       1   4          0  2.397895
#> 349 51.10      0   0       1  36          1  2.197225
#> 350 51.10      0   1       0  36          1  2.302585
#> 351 49.93      0   1       0  20          1  2.197225
#> 352  6.57      1   0       1  20          1  2.197225
#> 353 46.27      0   1       0  24          1  2.197225
#> 354 46.27      1   0       1  24          1  2.197225
#> 355 10.60      0   1       0  28          1  2.197225
#> 356 10.60      0   0       1  28          1  2.302585
#> 357 42.77      0   1       0  51          1  2.079442
#> 358 42.77      0   0       1  51          1  2.484907
#> 359 34.37      1   1       0  16          0  2.197225
#> 360 42.27      0   0       1  16          0  2.302585
#> 361 42.07      0   1       0  16          0  2.302585
#> 362 42.07      0   0       1  16          0  2.302585
#> 363 38.77      0   0       1  10          0  2.197225
#> 364 38.77      0   1       0  10          0  2.197225
#> 365 61.83      1   0       1  20          1  2.484907
#> 366 74.97      0   1       0  20          1  2.197225
#> 367 66.97      0   0       1  10          0  2.484907
#> 368  6.57      1   1       0  10          0  2.302585
#> 369 38.87      1   1       0  16          0  1.791759
#> 370 68.30      0   0       1  16          0  1.791759
#> 371 46.63      1   0       1  10          0  2.197225
#> 372 42.43      1   1       0  10          0  2.397895
#> 373 67.07      0   1       0  11          0  2.197225
#> 374 67.07      0   0       1  11          0  2.197225
#> 375  2.70      1   1       0   1          0  2.302585
#> 376  2.70      0   0       1   1          0  2.484907
#> 377 63.80      0   0       1  17          0  2.079442
#> 378 63.80      0   1       0  17          0  1.791759
#> 379 32.63      0   0       1   7          0  2.197225
#> 380 32.63      0   1       0   7          0  2.197225
#> 381 62.00      0   1       0  29          1  2.302585
#> 382 62.00      0   0       1  29          1  2.079442
#> 383 54.80      0   0       1   5          0  2.302585
#> 384 13.10      1   1       0   5          0  2.397895
#> 385  8.00      0   0       1   1          0  2.079442
#> 386  8.00      0   1       0   1          0  2.079442
#> 387 42.33      1   0       1  22          1  2.397895
#> 388 51.60      0   1       0  22          1  2.484907
#> 389 49.97      0   1       0  33          1  2.197225
#> 390  2.90      1   0       1  33          1  2.302585
#> 391 45.90      0   1       0   3          0  2.302585
#> 392  1.43      1   0       1   3          0  2.302585
#> 393 41.93      0   1       0  32          1  2.197225
#> 394 41.93      0   0       1  32          1  2.197225