File: methods_fixest.R

package info (click to toggle)
r-cran-marginaleffects 0.32.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,784 kB
  • sloc: sh: 13; makefile: 8
file content (90 lines) | stat: -rw-r--r-- 2,602 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#' @rdname get_predict
#' @export
get_predict.fixest <- function(
    model,
    newdata = insight::get_data(model),
    type = "response",
    mfx = NULL,
    ...) {
    insight::check_if_installed("fixest")

    if (is.null(type)) {
        calling_function <- if (!is.null(mfx)) mfx@calling_function else "predictions"
        type <- sanitize_type(
            model = model,
            type = type,
            calling_function = calling_function
        )
    }

    dots <- list(...)

    # some predict methods raise warnings on unused arguments
    unused <- c(
        "normalize_dydx",
        "step_size",
        "numDeriv_method",
        "conf.int",
        "internal_call"
    )
    dots <- dots[setdiff(names(dots), unused)]

    # fixest is super slow when using do call because of some `deparse()` call
    # issue #531: we don't want to waste time computing intervals or risk having
    # them as leftover columns in contrast computations
    pred <- try(
        stats::predict(
            object = model,
            newdata = newdata,
            type = type
        ),
        silent = TRUE
    )

    if (inherits(pred, "try-error")) {
        return(pred)
    }

    out <- data.table(estimate = as.numeric(pred))
    out <- add_rowid(out, newdata)

    return(out)
}


#' @rdname sanitize_model_specific
sanitize_model_specific.fixest <- function(model, vcov = TRUE, calling_function = "predictions", ...) {
    # issue #1487 is only a problem for standard errors
    if (isFALSE(vcov)) {
        return(model)
    }
    if (!isTRUE(getOption("marginaleffects_safe", default = TRUE))) {
        return(model)
    }

    msg <- paste(
        "For this model type, `marginaleffects` cannot take into account the uncertainty in fixed-effects parameters.",
        "This is especially consequential for predictions and their uncertainty intervals.",
        "Set `vcov=FALSE` to compute estimates without standard errors."
    )

    if (!is.null(model[["fixef_vars"]])) {
        # issue #1487: fixed-effects always matter for predictions
        if (identical(calling_function, "predictions")) {
            stop_sprintf(msg)
        }

        # issue #1487: fixed-effects matter for slopes and contrasts, except for linear models
        if (!is.null(model$family)) {
            if (identical(calling_function, "hypotheses")) {
                if (isTRUE(getOption("marginaleffects_safe", default = TRUE))) {
                    warn_sprintf(msg)
                }
            } else {
                stop_sprintf(msg)
            }
        }
    }

    return(model)
}