This function estimates the predictive effects and levels for variables within a model using the delta method.
marg(
mod,
var_interest,
data = NULL,
weights = NULL,
vcov_mat = NULL,
dof = NULL,
type = "levels",
base_rn = 1,
at_var_interest = NULL,
at = NULL,
cofint = 0.95,
...
)
name of the variable of interest, must correspond to a covariate in the model
data.frame that margins should run over, defaults changes based on class-specific method
numeric, vector of weights used to generate predicted levels,
defaults changes based on class-specific method. Must be equal to the number
of rows in data
.
the variance-covariance matrix, defaults changes based on class-specific method
integer, the degrees of freedom used for the T statistic in an OLS model, defaults changes based on class-specific method
either 'levels'
(predicted outcomes) or 'effects'
\(dydx\),
defaults to 'levels'
numeric, if type == 'effects'
, the base level (taken as the
index of one of the ordered unique values in var_interest
). if
type == 'levels'
, this parameter is ignored. Defaults to 1.
vector, if type == 'levels', the values for the
variable of interest at which levels should be calculated.
If NULL
, indicates all levels for a factor variable, defaults to NULL
list, should be in the format of list('var_name' = c(values))
,
defaults to NULL
. This calculates the margins of the variable at these
particular variables. If all values are needed, suggested syntax is
at = list('var' = unique(df$var))
.
numeric, confidence interval (must be less than 1), defaults to 0.95
additional parameters passed to class-specific methods
list of dataframes with predicted margins/effects, standard errors, p-values, and confidence interval bounds
The variable for the predictive margin is specified by var_interest
. If
margins are only needed at particular values of var_interest
,
at_var_interest
should be used. If margins of var_interest
are
needed at across the levels of a different variable in the model,
at
should be used.
If higher-order polynomial terms (e.g. \(y ~ x + x^2\)) are added
using the R function poly
, the raw = TRUE
argument should be used to include the basic polynomial terms
instead of orthogonal polynomial terms. If orthogonal polynomials are used,
marg
will fail when the user specifies at
for a small set
of values for the variable in question (e.g. at = list(x = 10)
),
since poly
needs more data to calculate orthogonal polynomials
(e.g. poly(10, 2)
fails, but poly(c(10, 8, 3), 2)
will run).
P values are calculated with T tests for gaussian families, and Z tests
otherwise. If a new variance-covariance matrix is provided (e.g. for
clustering standard errors), the degrees of freedom for the T test / p-value
calculation may need to be specified using dof
.
This function currently only supports glm
and
ivreg
objects. If you would like to use lm
objects, consider running a glm
with family gaussian
.
When calculating predicted levels and effects for models built using weights,
marg
returns weighted averages for levels and effects by default.
Users can remove this option by setting weights = NULL
.