R/imputation.R

Defines functions glance.marginaleffects_mids tidy.marginaleffects_mids process_imputation

process_imputation <- function(mfx) {
    insight::check_if_installed("mice")

    model <- mfx@model
    mfxcall <- mfx@call

    micedata <- tryCatch(get_modeldata_mids(model), error = function(e) NULL)

    # issue #1269: transforms must be applied after pooling
    if ("transform" %in% names(mfxcall)) {
        tr <- eval.parent(mfxcall[["transform"]])
        mfxcall[["transform"]] <- NULL
    } else {
        tr <- NULL
    }

    if (inherits(model, "mira")) {
        modellist <- model$analyses
    } else if (inherits(model, "amest")) {
        # amest already in the right format
        modellist <- model
    } else {
        stop_sprintf("MI class not implemented yet.")
    }

    # Issue #1682: Warn once if we can't recover the mids data and newdata is NULL
    if (is.null(micedata) && is.null(mfxcall[["newdata"]])) {
        warning("Could not recover the original data from the `mids` object. ",
                "When the model formula contains in-formula transformations (e.g., `bs()`, `poly()`, `log()`), ",
                "results may be incorrect. Please supply an explicit `newdata` argument or use `set_modeldata()` to attach the original data to the model.",
                call. = FALSE)
    }

    mfxlist <- vector("list", length(modellist))
    for (i in seq_along(modellist)) {
        calltmp <- mfxcall
        calltmp[["model"]] <- modellist[[i]]

        # Issue #1682: When newdata is NULL, provide the correct completed dataset
        # so individual models don't fall back on insight::get_data() which can
        # fail for with.mids() models (e.g., bs() gets expanded into basis columns).
        if (is.null(calltmp[["newdata"]]) && !is.null(micedata)) {
            nd <- micedata[[i]]
            # Apply subset from the original model call if present.
            # Use the formula environment as enclos so external variables
            # (e.g., subset = year >= cutoff_year) resolve correctly.
            # Use which() to drop NA rows, matching model.frame() behavior.
            subcall <- modellist[[i]]$call$subset
            if (!is.null(subcall)) {
                fallback_env <- parent.frame()
                enclos <- tryCatch(environment(stats::formula(modellist[[i]])), error = function(e) fallback_env)
                nd <- nd[which(eval(subcall, envir = nd, enclos = enclos)), , drop = FALSE]
            }
            calltmp[["newdata"]] <- nd
        }

        # Handle deferred newdata processing for calls like subset(treat == 1)
        if ("newdata" %in% names(calltmp) && rlang::is_call(calltmp[["newdata"]])) {
            newdata_call <- calltmp[["newdata"]]
            if (rlang::call_name(newdata_call) == "subset") {
                # Add the model data as the 'x' argument for subset
                if (!"x" %in% rlang::call_args_names(newdata_call)) {
                    newdata_call <- rlang::call_modify(newdata_call, x = micedata[[i]])
                }
            } else if (rlang::call_name(newdata_call) == "filter") {
                # Add the model data as the '.data' argument for filter
                if (!".data" %in% rlang::call_args_names(newdata_call)) {
                    newdata_call <- rlang::call_modify(newdata_call, .data = micedata[[i]])
                }
            } else if (grepl("^datagrid", rlang::call_name(newdata_call))) {
                # datagrid() needs the individual model to recover its data
                if (!any(c("model", "newdata") %in% rlang::call_args_names(newdata_call))) {
                    newdata_call <- rlang::call_modify(newdata_call, model = modellist[[i]])
                }
            }
            # Evaluate the newdata call with the individual model's data
            calltmp[["newdata"]] <- eval(newdata_call)
        }

        mfxlist[[i]] <- eval.parent(calltmp)

        # save the S4 structure of a single model to host the eventual output
        if (i == 1) {
            out <- mfxlist[[i]]
        }

        # needed for mice::pool(); after saving `out`
        mfxlist[[i]]$term <- seq_len(nrow(mfxlist[[i]]))

        # Needed for mice::pool() to get dfcom, even when lean = TRUE
        attr(mfxlist[[i]], "marginaleffects")@model <- modellist[[i]]
        class(mfxlist[[i]]) <- c("marginaleffects_mids", class(mfxlist[[i]]))
    }

    mipool <- mice::pool(mfxlist)

    ti <- mice::tidy(mipool)
    for (col in c("estimate", "std.error", "statistic", "p.value", "conf.low", "conf.high", "df")) {
        if (col %in% colnames(ti)) {
            out[[col]] <- ti[[col]]
        } else {
            out[[col]] <- NULL
        }
    }

    out$df <- mipool$pooled$df

    # TODO: change this once we store the metadata in its final place
    out <- get_ci(out, attr(mfxlist[[1]], "marginaleffects"))

    out <- backtransform(out, sanitize_transform(tr))

    return(out)
}

#' tidy helper
#'
#' @noRd
#' @export
tidy.marginaleffects_mids <- function(x, ...) {
    if (!"std.error" %in% colnames(x)) {
        stop_sprintf(
            'The output does not include a `std.error` column. Some models do not generate standard errors when estimates are backtransformed (e.g., GLM models). One solution is to use `type="response"` for those models.'
        )
    }
    out <- as.data.frame(x[, c("estimate", "std.error")])
    out$term <- seq_len(nrow(out))
    return(out)
}


#' glance helper
#'
#' @noRd
#' @export
glance.marginaleffects_mids <- function(x, ...) {
    data.frame()
}

Try the marginaleffects package in your browser

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

marginaleffects documentation built on Sept. 3, 2026, 9:08 a.m.