R/methods_survival.R

Defines functions sanitize_model_specific.coxph get_predict.coxph set_coef.survreg

Documented in get_predict.coxph sanitize_model_specific.coxph set_coef.survreg

#' @include set_coef.R
#' @rdname set_coef
#' @export
set_coef.survreg <- function(model, coefs, ...) {
    #Reverse engineering insight::get_get_parameters.survreg(),
    #which uses summary.survreg()

    nvar0 <- length(model$coefficients)
    nvar <- nrow(model$var)
    if (nvar > nvar0) {
        model[["coefficients"]][] <- coefs[-nvar0]
        model[["scale"]][] <- exp(coefs[nvar0])
    } else {
        model$coefficients[] <- coefs
    }

    model
}

#' @rdname get_predict
#' @export
get_predict.coxph <- function(
    model,
    newdata = insight::get_data(model),
    type = "lp",
    ...
) {
    out <- stats::predict(model, newdata = newdata, type = type, ...)

    out <- data.frame(rowid = seq_len(nrow(newdata)), estimate = out)
    return(out)
}


#' @rdname sanitize_model_specific
sanitize_model_specific.coxph <- function(model, vcov, ...) {
    insight::check_if_installed("survival")
    flag1 <- !isFALSE(vcov)
    flag2 <- !isTRUE(checkmate::check_choice(vcov, choices = c("rsample", "boot", "fwb")))
    flag3 <- isTRUE(getOption("marginaleffects_safe", default = TRUE))
    if (flag1 && flag2 && flag3) {
        msg <- 'The default delta method standard errors for `coxph` models only take into account uncertainty in the regression coefficients. Standard errors may be too small. Use the `inferences()` function or set `vcov` to "rsample", "boot"  or "fwb" to compute confidence intervals by bootstrapping. Set `vcov` to `FALSE` or `options(marginaleffects_safe=FALSE)` to silence this warning.'
        warning(msg, call. = FALSE)
    }
    return(model)
}

Try the marginaleffects package in your browser

Any scripts or data that you put into this service are public.

marginaleffects documentation built on June 8, 2025, 12:44 p.m.