Skip to contents

Stores additional data and attributes on a generic model class so that it can be used as the regr argument to ei_est(). Given the wide variety of model classes, there is no guarantee this function will work. However, most model classes supporting a fitted() and predict() method will work as long as there is no transformation of the predictor variables as part of the model formula or fitting.

Usage

ei_wrap_model(x, data, predictors = NULL, outcome = NULL, ...)

Arguments

x

A model object, supporting fitted() and predict() generics.

data

A data frame or matrix containing the data used to fit the model, or an ei_spec() object (recommended). If the latter, then the predictors and outcome arguments are ignored and need not be provided.

predictors

<tidy-select> Predictor variables. This is the x variable in ecological regression that is of primary interest. For example, the columns containing the percentage of each racial group.

outcome

<tidy-select> Outcome variables. This is the y variable in ecological regression that is of primary interest. For example, the columns containing the percentage of votes for each party.

...

Additional arguments passed to the predict() method.

Value

An ei_wrapped object, which has the information required to use the provided x with ei_est().

Examples

data(elec_1968)

spec = ei_spec(elec_1968, vap_white:vap_other, pres_ind_wal, pres_total,
               covariates = c(pop_urban, farm))

# Note: this is not a model recommended for valid ecological inference!
m = suppressWarnings(
    glm(pres_ind_wal ~ 0 + vap_white + vap_black + vap_other + pop_urban + farm,
        data = spec, family = "binomial")
)
m_wrap = ei_wrap_model(m, spec, type = "response")
print(m_wrap)
#> A wrapped <glm/lm> model with 1143 observations

ei_est(m_wrap, data = spec) # notice all estimates nonnegative
#> # A tibble: 3 × 4
#>   predictor outcome        estimate std.error
#>   <chr>     <chr>             <dbl>     <dbl>
#> 1 vap_white pres_ind_wal 0.339        0.0239 
#> 2 vap_black pres_ind_wal 0.741        0.0565 
#> 3 vap_other pres_ind_wal 0.00000298   0.00539