library(simplFeedbackPerformance)
library(magrittr)
library(ggplot2)
library(ggExtra)
#> Warning: package 'ggExtra' was built under R version 4.0.3The first step is reducing the complete data set to our desired sample. This filters for complete, paired evaluations where the subject is “Trainee” and a “Categorical” resident in a PGY 1 through 5 year of training.
Our propsed model is of the form DV ~ TIME + VOL + (1| TRAIN) + (1|PROC) + (1|PROGRAM) where TIME represents the number of days since the beginning of a given PGY academic year and VOL represents the number of previously completed evaluations at the time of a given observation.
TIMEFirst, we need to create a function to calculate TIME, which is the number of days since the beginning of the year. We will define the end date of any given year as June 30th, meaning the start date is July 1st.
valiDATE <- function(date) {
stopifnot(`date must take the form of "MM-DD"` = stringr::str_detect(date, "^\\d{2}-\\d{2}$"))
}
days <- function(x, end = "06-30") {
valiDATE(end)
calcdiff <- function(x) {
endx <- glue::glue("{lubridate::year(x)}-{end}")
if(lubridate::yday(x) > lubridate::yday(endx)) {
diff <- ceiling(difftime(x, endx, units = "days"))
} else {
endx <- glue::glue("{lubridate::year(x)-1}-{end}")
diff <- ceiling(difftime(x, endx, units = "days"))
}
unclass(diff)
}
purrr::map_dbl(x, calcdiff)
}
day_vec <- seq(as.Date("2020-07-01"), as.Date("2021-06-30"), by = "days")
days(day_vec)
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#> [19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#> [37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#> [55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#> [73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
#> [91] 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
#> [109] 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
#> [127] 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
#> [145] 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
#> [163] 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
#> [181] 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
#> [199] 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
#> [217] 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
#> [235] 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
#> [253] 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
#> [271] 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
#> [289] 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
#> [307] 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
#> [325] 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
#> [343] 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
#> [361] 361 362 363 364 365Since this appears to work correctly (I will more rigorously test this function in our package), we can apply it to our data frame:
VOLNext, we should create the function to calculate VOL. For any given individual (designated by a unique subjectID), VOL should be equal to the number of evaluations completed prior to any given observation. However, there are technically some records with identical caseIDs because SIMPL generates multiple evalutions with the same caseID for different procedures or even different residents within the same case. The code below lists all residents with multiple evaluations derived from the same case:
df %>%
dplyr::filter(duplicated(caseID) | duplicated(caseID, fromLast = TRUE)) %>%
dplyr::group_by(caseID) %>%
dplyr::filter(duplicated(subjectID) | duplicated(subjectID, fromLast = TRUE)) %>%
dplyr::select(caseID, subjectID, procID, procName) %>%
dplyr::arrange(caseID)In fact, there are some cases where a resident can have multiple procedures documented with the same caseID and receive different performance grades for each procedure:
df %>%
dplyr::filter(duplicated(caseID) | duplicated(caseID, fromLast = TRUE)) %>%
dplyr::group_by(caseID) %>%
dplyr::filter(duplicated(subjectID) | duplicated(subjectID, fromLast = TRUE)) %>%
dplyr::summarise(same = dplyr::n_distinct(performance)) %>%
dplyr::pull(same) %>%
unique()
#> `summarise()` ungrouping output (override with `.groups` argument)
#> [1] 1 2 3This leaves a question of should VOL represent the number of evaluations performed or the number of cases performed. We can run models with both, so I will create two variables to represent this (vol_evals and vol_cases):
independentThe outcome variable, independent is to be a simple Boolean where
\[f(x) = \begin{cases} 1, & \text{if $x\geq D$}.\\ 0, & \text{otherwise}. \end{cases} \]
df %>%
ggplot(aes(x = scale(vol_evals))) +
geom_histogram()
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
df %>%
ggplot(aes(x = scale(vol_cases))) +
geom_histogram()
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
df %>%
ggplot(aes(x = scale(time))) +
geom_histogram()
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.summary(fit1)
#> Generalized linear mixed model fit by maximum likelihood (Laplace
#> Approximation) [glmerMod]
#> Family: binomial ( logit )
#> Formula: independent ~ scale(time) + scale(vol_evals) + complexity + (1 |
#> procName) + (1 | subjectID) + (1 | raterID)
#> Data: subset(df, df$traineePGY == 4)
#> Control:
#> lme4::glmerControl(optimizer = "bobyqa", optCtrl = list(maxfun = 2e+05))
#>
#> AIC BIC logLik deviance df.resid
#> 11019.3 11077.4 -5501.7 11003.3 10517
#>
#> Scaled residuals:
#> Min 1Q Median 3Q Max
#> -6.8826 -0.5054 0.1452 0.5160 6.3772
#>
#> Random effects:
#> Groups Name Variance Std.Dev.
#> procName (Intercept) 1.420 1.192
#> raterID (Intercept) 2.770 1.664
#> subjectID (Intercept) 1.111 1.054
#> Number of obs: 10525, groups: procName, 1117; raterID, 924; subjectID, 648
#>
#> Fixed effects:
#> Estimate Std. Error z value Pr(>|z|)
#> (Intercept) 0.39779 0.11132 3.573 0.000352 ***
#> scale(time) 0.25387 0.04093 6.202 5.57e-10 ***
#> scale(vol_evals) 0.39683 0.05429 7.309 2.69e-13 ***
#> complexity.L -1.30846 0.07930 -16.499 < 2e-16 ***
#> complexity.Q -0.03379 0.05384 -0.628 0.530321
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Correlation of Fixed Effects:
#> (Intr) scl(t) scl(_) cmpl.L
#> scale(time) -0.063
#> scl(vl_vls) 0.198 -0.348
#> complexty.L -0.158 -0.023 -0.059
#> complexty.Q 0.206 -0.011 0.016 -0.338summary(fit2)
#> Generalized linear mixed model fit by maximum likelihood (Laplace
#> Approximation) [glmerMod]
#> Family: binomial ( logit )
#> Formula:
#> independent ~ scale(time) + scale(vol_evals) + traineePGY + complexity +
#> (1 | procName) + (1 | subjectID) + (1 | raterID)
#> Data: df
#> Control:
#> lme4::glmerControl(optimizer = "bobyqa", optCtrl = list(maxfun = 2e+05))
#>
#> AIC BIC logLik deviance df.resid
#> 33754.1 33856.7 -16865.0 33730.1 38314
#>
#> Scaled residuals:
#> Min 1Q Median 3Q Max
#> -46.769 -0.416 -0.113 0.413 21.979
#>
#> Random effects:
#> Groups Name Variance Std.Dev.
#> subjectID (Intercept) 1.030 1.015
#> procName (Intercept) 1.798 1.341
#> raterID (Intercept) 3.081 1.755
#> Number of obs: 38326, groups: subjectID, 1891; procName, 1728; raterID, 1516
#>
#> Fixed effects:
#> Estimate Std. Error z value Pr(>|z|)
#> (Intercept) -0.96494 0.08318 -11.601 < 2e-16 ***
#> scale(time) 0.24509 0.02019 12.142 < 2e-16 ***
#> scale(vol_evals) 0.31426 0.02482 12.660 < 2e-16 ***
#> traineePGY.L 5.10037 0.09552 53.397 < 2e-16 ***
#> traineePGY.Q 0.32804 0.06847 4.791 1.66e-06 ***
#> traineePGY.C -0.05549 0.05851 -0.948 0.34288
#> traineePGY^4 -0.21254 0.05083 -4.182 2.89e-05 ***
#> complexity.L -1.02648 0.04267 -24.055 < 2e-16 ***
#> complexity.Q -0.07554 0.02906 -2.599 0.00935 **
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Correlation of Fixed Effects:
#> (Intr) scl(t) scl(_) tPGY.L tPGY.Q tPGY.C tPGY^4 cmpl.L
#> scale(time) -0.065
#> scl(vl_vls) 0.101 -0.274
#> trainePGY.L -0.158 0.214 -0.029
#> trainePGY.Q 0.080 0.056 -0.013 -0.288
#> trainePGY.C -0.007 0.008 0.033 0.130 -0.358
#> trainePGY^4 0.013 0.003 -0.029 -0.055 0.151 -0.275
#> complexty.L -0.040 -0.024 -0.042 -0.157 0.005 0.002 0.015
#> complexty.Q 0.139 -0.009 0.000 0.003 -0.020 0.014 0.002 -0.227