| Title: | Calculating Marginal Effects and Levels with Errors |
| Version: | 0.9.6 |
| Description: | Calculate predicted levels and marginal effects, using the delta method to calculate standard errors. This is an R-based version of the 'margins' command from Stata. |
| URL: | https://github.com/anniejw6/modmarg |
| BugReports: | https://github.com/anniejw6/modmarg/issues |
| Depends: | R (≥ 3.5.0) |
| License: | GPL-3 |
| Encoding: | UTF-8 |
| LazyData: | true |
| Suggests: | knitr, rmarkdown, testthat, sandwich, AER |
| VignetteBuilder: | knitr |
| RoxygenNote: | 7.1.1 |
| NeedsCompilation: | no |
| Packaged: | 2020-11-21 20:20:28 UTC; ajw |
| Author: | Alex Gold [aut], Nat Olin [aut], Annie Wang [aut, cre] |
| Maintainer: | Annie Wang <anniejw6@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2020-11-22 21:00:02 UTC |
Clustered variance-covariance matrices and T statistic d.o.f.
Description
Variance-covariance matrices with robust clustered standard errors anddegrees-of-freedom for T statistics, for tests and examples specifyingvcov(d.o.f. defined as g - 1, where g is the number of clusters). Generatedwithmargex data in this package.
Usage
cvcovFormat
A list of three lists, from an OLS model, logit model, and OLS with a polynomialinteraction with missing data, each containing
- clust
3-by-3 variance-covariance matrix
- dof
integer, degrees of freedom for the T statistic
Details
See data-raw/make_cluster_vcov.R for details.
Source
http://cameron.econ.ucdavis.edu/research/Cameron_Miller_JHR_2015_February.pdf
Estimating predictive margins on a model
Description
This function estimates the predictive effects and levels for variables withina model using the delta method.
Usage
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 |
var_interest | name of the variable of interest, must correspond to acovariate in the model |
data | data.frame that margins should run over, defaults changes basedon 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 numberof rows in |
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 anOLS model, defaults changes based on class-specific method |
type | either |
base_rn | numeric, if |
at_var_interest | vector, if type == 'levels', the values for thevariable of interest at which levels should be calculated.If |
at | list, should be in the format of |
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 byvar_interest. Ifmargins are only needed at particular values ofvar_interest,at_var_interest should be used. If margins ofvar_interest areneeded at across the levels of adifferent variable in the model,at should be used.
If higher-order polynomial terms (e.g.y ~ x + x^2) are addedusing the R functionpoly, theraw = TRUEargument should be used to include the basic polynomial termsinstead of orthogonal polynomial terms. If orthogonal polynomials are used,marg will fail when the user specifiesat for a small setof values for the variable in question (e.g.at = list(x = 10)),sincepoly needs more data to calculate orthogonal polynomials(e.g.poly(10, 2) fails, butpoly(c(10, 8, 3), 2) will run).
P values are calculated with T tests for gaussian families, and Z testsotherwise. If a new variance-covariance matrix is provided (e.g. forclustering standard errors), the degrees of freedom for the T test / p-valuecalculation may need to be specified usingdof.
This function currently only supportsglm andivreg objects. If you would like to uselmobjects, consider running aglm with familygaussian.
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 settingweights = NULL.
Value
list of dataframes with predicted margins/effects, standard errors, p-values,and confidence interval bounds
Predicted Margins for 'glm' objects
Description
Obtains predicted margins and standard errorsof those predictions from a fitted generalized linear model object.
Usage
## S3 method for class 'glm'marg( mod, var_interest, data = mod$data[names(mod$prior.weights), ], weights = mod$prior.weights, ...)Arguments
mod | model object, currently only support those of class |
var_interest | name of the variable of interest, must correspond to acovariate in the model |
data | data.frame that margins should run over, defaults changes basedon 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 numberof rows in |
... | additional parameters passed to |
Examples
data(mtcars)mod <- glm(vs ~ as.factor(gear) + mpg, data = mtcars, family = 'binomial')# Get the level of the outcome variable at different values of `gear`marg(mod, var_interest = 'gear', type = 'levels')# Get the effect of `gear` on the outcome value, holding values of `mpg`# constantmarg(mod, var_interest = 'gear', type = 'effects', at = list(mpg = c(15, 21)))data(margex)mod <- glm(outcome ~ as.factor(treatment) + distance, data = margex, family = 'binomial')# Get the level of the outcome variable at different values of `treatment`marg(mod, var_interest = 'treatment', type = 'levels', at = NULL)# Get the effect of `treatment` on the outcome variablemarg(mod, var_interest = 'treatment', type = 'effects', at = NULL)# Get the level of the outcome variable at different values of `distance`marg(mod, var_interest = 'distance', type = 'levels', at = NULL, at_var_interest = c(10, 20, 30))# Using a custom variance-covariance matrix for clustered standard errors# (also requires custom degrees of freedom for T statistic with OLS model),# clustering on the "arm" variabledata(margex)data(cvcov)# ?cvcovv <- cvcov$ols$clustd <- cvcov$ols$stata_dofmod <- glm(outcome ~ treatment + distance, data = margex, family = 'binomial')marg(mod, var_interest = 'treatment', type = 'levels', vcov_mat = v, dof = d)# Using weightsdata(margex)mm <- glm(y ~ as.factor(treatment) + age, data = margex, family = 'gaussian', weights = distance)z1 <- marg(mod = mm, var_interest = 'treatment', type = 'levels')[[1]]z2 <- marg(mod = mm, var_interest = 'treatment', type = 'effects')[[1]]Predicted Margins for 'ivreg' objects from theAER package
Description
Obtains predicted margins and standard errorsof those predictions from a fittedivreg model object.
Usage
## S3 method for class 'ivreg'marg(mod, var_interest, data, weights = NULL, ...)Arguments
mod | model object, currently only support those of class |
var_interest | name of the variable of interest, must correspond to acovariate in the model |
data | data.frame that margins should run over, defaults changes basedon 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 numberof rows in |
... | additional parameters passed to |
Examples
# From ?AER::ivreg# datadata("CigarettesSW", package = "AER")CigarettesSW$rprice <- with(CigarettesSW, price/cpi)CigarettesSW$rincome <- with(CigarettesSW, income/population/cpi)CigarettesSW$tdiff <- with(CigarettesSW, (taxs - tax)/cpi)# modelfm <- AER::ivreg(log(packs) ~ log(rprice) + log(rincome) | log(rincome) + tdiff + I(tax/cpi), data = CigarettesSW, subset = year == "1995")# Get margins for different levels of price/cpirprice_levs <- round(quantile(CigarettesSW$rprice))marg(fm, data = subset(CigarettesSW, year == "1995"), var_interest = 'rprice', at_var_interest = rprice_levs)Artificial data for margins
Description
A fictitious dataset outcome, treatment, and demographic variables for 3000observations.
Usage
margexFormat
A data frame with 3000 rows and 11 variables:
- y
numeric
- outcome
integer, 0 or 1
- sex
character: "female" or "male"
- group
integer
- age
integer
- distance
numeric
- ycn
numeric
- yc
numeric, 0 or 1
- treatment
integer
- agegroup
character: "20-29", "30-39", or "40+"
- arm
integer
Source
https://www.stata-press.com/data/r14/margex.dta
Main wrapper function to calculate margins and standard errors
Description
For one set of transformed covariates (not including the variable ofinterest), calculate the predicted level and standard error for thevariable of interest.
Usage
pred_se( df_levels, model, type, base_rn, vcov_mat, weights, deriv_func, link_func)Arguments
df_levels | data.frame, already transformed for variables not relatedto the variable of interest |
model | model object |
type | either effects or levels |
base_rn | numeric, row number of the base level |
vcov_mat | matrix, variance-covariance matrix |
weights | vector of weights, or NULL |
deriv_func | function for the derivative of the predicted outcomes |
link_func | function to transform output of 'predict' method intoresponse scale |