Open vanAmsterdam opened 3 years ago
BTW this is solution 1.
filter_strata <- function(smformula) {
rhs <- gsub(x = smformula, pattern = ".*~", replacement = "")
smformula_matrix <- as.formula(paste0("~ +", rhs))
# Check if there is stratification - if so remove from model matrix
# (stratification means different baseline hazards, coefficients still same)
if (grepl(x = rhs, pattern = "strata")) {
strata_var <- stringr::str_replace_all(rhs, pattern = ".*\\(|\\).*", replacement = "")
rm_strata <- as.formula(paste0("~ . - strata(", strata_var, ")"))
smformula_matrix <- update(smformula_matrix, rm_strata)
}
return(smformula_matrix)
}
Then at the 3 places where this is relevant:
smformula_matrix <- filter_strata(smformula)
outmodxb <- model.matrix(as.formula(smformula_matrix),imputations[[imp]])
outmodxb <- as.matrix(outmodxb[,2:dim(outmodxb)[2]]) %*% as.matrix(outcomeModBeta)
Hi, thanks for making this code public
I need to work with a coxph model with stratified baseline hazard Due to the calculation of the log-hazard ratio inside smcfcs this goes wrong when strata are involved. The strata do not contribute to the log-hazard ratio, but the current code assumes that all terms do and throws a non-conformable arguments error in the matrix multiplication
I've come up with two solutions:
strata
arguments in the formula definition and throws them out at the right placepredict.coxph
method usingtype='lp'
I think option 2. is more general and I couldn't find a reason not to use this
I've implemented this solution and created a PR