R/methods_inferences_simulation.R

Defines functions get_predict.inferences_simulation sanitize_model_specific.inferences_simulation get_vcov.inferences_simulation

Documented in get_predict.inferences_simulation get_vcov.inferences_simulation sanitize_model_specific.inferences_simulation

#' @rdname get_vcov
#' @export
get_vcov.inferences_simulation <- function(model, ...) return(NULL)


#' @rdname sanitize_model_specific
#' @export
sanitize_model_specific.inferences_simulation <- function(model, vcov = FALSE, ...) {
    tmp <- model
    class(tmp) <- setdiff(class(tmp), "inferences_simulation")
    B <- get_coef(tmp)
    # at this stage, the `vcov` has been pre-processed, so we get all the "HC3" shortcuts
    V <- get_vcov(tmp, vcov = vcov)
    simfun <- attr(model, "inferences_simulate")
    R <- attr(model, "inferences_R")
    attr(model, "inferences_coefmat") <- simfun(R = R, B = B, V = V)
    return(model)
}


#' @rdname get_predict
#' @export
get_predict.inferences_simulation <- function(model, newdata, ...) {
    coefmat <- attr(model, "inferences_coefmat")
    # coefmat: BxM 
    checkmate::assert_matrix(coefmat)
    # remove the special class to avoid calling myself
    mod <- model
    class(mod) <- setdiff(class(mod), "inferences_simulation")
    FUN <- function(coefs) {
        mod_tmp <- set_coef(mod, coefs = coefs)
        get_predict(mod_tmp, newdata = newdata)$estimate
    }
    # should never compute SE via delta method for these models
    out <- get_predict(mod, newdata = newdata, ...)
    attr(out, "posterior_draws") <- apply(coefmat, MARGIN = 1, FUN = FUN)
    return(out)
}

Try the marginaleffects package in your browser

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

marginaleffects documentation built on Oct. 20, 2023, 1:07 a.m.