marg: Estimating predictive margins on a model

Description Usage Arguments Details Value

View source: R/marg.R

Description

This function estimates the predictive effects and levels for variables within a model using the delta method.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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,
  ...
)

Arguments

mod

model object, currently only support those of class glm or ivreg

var_interest

name of the variable of interest, must correspond to a covariate in the model

data

data.frame that margins should run over, defaults changes based on class-specific method

weights

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.

vcov_mat

the variance-covariance matrix, defaults changes based on class-specific method

dof

integer, the degrees of freedom used for the T statistic in an OLS model, defaults changes based on class-specific method

type

either 'levels' (predicted outcomes) or 'effects' dydx, defaults to 'levels'

base_rn

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.

at_var_interest

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

at

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)).

cofint

numeric, confidence interval (must be less than 1), defaults to 0.95

...

additional parameters passed to class-specific methods

Details

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.

Value

list of dataframes with predicted margins/effects, standard errors, p-values, and confidence interval bounds


anniejw6/modmarg documentation built on Nov. 21, 2020, 10:38 p.m.