| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/margins.R |
| 3 |
\name{tidy.margins} |
| 4 |
\alias{tidy.margins} |
| 5 |
\alias{margins_tidiers} |
| 6 |
\title{Tidy a(n) margins object} |
| 7 |
\usage{ |
| 8 |
\method{tidy}{margins}(x, conf.int = FALSE, conf.level = 0.95, ...) |
| 9 |
} |
| 10 |
\arguments{ |
| 11 |
\item{x}{A \code{margins} object returned from \code{\link[margins:margins]{margins::margins()}}.} |
| 12 |
|
| 13 |
\item{conf.int}{Logical indicating whether or not to include a confidence |
| 14 |
interval in the tidied output. Defaults to \code{FALSE}.} |
| 15 |
|
| 16 |
\item{conf.level}{The confidence level to use for the confidence interval |
| 17 |
if \code{conf.int = TRUE}. Must be strictly greater than 0 and less than 1. |
| 18 |
Defaults to 0.95, which corresponds to a 95 percent confidence interval.} |
| 19 |
|
| 20 |
\item{...}{Additional arguments. Not used. Needed to match generic |
| 21 |
signature only. \strong{Cautionary note:} Misspelled arguments will be |
| 22 |
absorbed in \code{...}, where they will be ignored. If the misspelled |
| 23 |
argument has a default value, the default value will be used. |
| 24 |
For example, if you pass \code{conf.lvel = 0.9}, all computation will |
| 25 |
proceed using \code{conf.level = 0.95}. Two exceptions here are: |
| 26 |
\itemize{ |
| 27 |
\item \code{tidy()} methods will warn when supplied an \code{exponentiate} argument if |
| 28 |
it will be ignored. |
| 29 |
\item \code{augment()} methods will warn when supplied a \code{newdata} argument if it |
| 30 |
will be ignored. |
| 31 |
}} |
| 32 |
} |
| 33 |
\description{ |
| 34 |
Tidy summarizes information about the components of a model. |
| 35 |
A model component might be a single term in a regression, a single |
| 36 |
hypothesis, a cluster, or a class. Exactly what tidy considers to be a |
| 37 |
model component varies across models but is usually self-evident. |
| 38 |
If a model has several distinct types of components, you will need to |
| 39 |
specify which components to return. |
| 40 |
} |
| 41 |
\details{ |
| 42 |
The \code{margins} package provides a way to obtain coefficient marginal |
| 43 |
effects for a variety of (non-linear) models, such as logit or models with |
| 44 |
multiway interaction terms. Note that the \code{glance.margins()} method |
| 45 |
requires rerunning the underlying model again, which can take some time. |
| 46 |
Similarly, an \code{augment.margins()} method is not currently supported, but |
| 47 |
users can simply run the underlying model to obtain the same information. |
| 48 |
} |
| 49 |
\examples{ |
| 50 |
\dontshow{if (rlang::is_installed("margins")) withAutoprint(\{ # examplesIf} |
| 51 |
|
| 52 |
# load libraries for models and data |
| 53 |
library(margins) |
| 54 |
|
| 55 |
# example 1: logit model |
| 56 |
mod_log <- glm(am ~ cyl + hp + wt, data = mtcars, family = binomial) |
| 57 |
|
| 58 |
# get tidied "naive" model coefficients |
| 59 |
tidy(mod_log) |
| 60 |
|
| 61 |
# convert to marginal effects with margins() |
| 62 |
marg_log <- margins(mod_log) |
| 63 |
|
| 64 |
# get tidied marginal effects |
| 65 |
tidy(marg_log) |
| 66 |
tidy(marg_log, conf.int = TRUE) |
| 67 |
|
| 68 |
# requires running the underlying model again. quick for this example |
| 69 |
glance(marg_log) |
| 70 |
|
| 71 |
# augmenting `margins` outputs isn't supported, but |
| 72 |
# you can get the same info by running on the underlying model |
| 73 |
augment(mod_log) |
| 74 |
|
| 75 |
# example 2: threeway interaction terms |
| 76 |
mod_ie <- lm(mpg ~ wt * cyl * disp, data = mtcars) |
| 77 |
|
| 78 |
# get tidied "naive" model coefficients |
| 79 |
tidy(mod_ie) |
| 80 |
|
| 81 |
# convert to marginal effects with margins() |
| 82 |
marg_ie0 <- margins(mod_ie) |
| 83 |
# get tidied marginal effects |
| 84 |
tidy(marg_ie0) |
| 85 |
glance(marg_ie0) |
| 86 |
|
| 87 |
# marginal effects evaluated at specific values of a variable (here: cyl) |
| 88 |
marg_ie1 <- margins(mod_ie, at = list(cyl = c(4,6,8))) |
| 89 |
|
| 90 |
# summarize model fit with tidiers |
| 91 |
tidy(marg_ie1) |
| 92 |
|
| 93 |
# marginal effects of one interaction variable (here: wt), modulated at |
| 94 |
# specific values of the two other interaction variables (here: cyl and drat) |
| 95 |
marg_ie2 <- margins(mod_ie, |
| 96 |
variables = "wt", |
| 97 |
at = list(cyl = c(4,6,8), drat = c(3, 3.5, 4))) |
| 98 |
|
| 99 |
# summarize model fit with tidiers |
| 100 |
tidy(marg_ie2) |
| 101 |
\dontshow{\}) # examplesIf} |
| 102 |
} |
| 103 |
\seealso{ |
| 104 |
\code{\link[=tidy]{tidy()}}, \code{\link[margins:margins]{margins::margins()}} |
| 105 |
} |
| 106 |
\concept{margins tidiers} |
| 107 |
\value{ |
| 108 |
A \code{\link[tibble:tibble]{tibble::tibble()}} with columns: |
| 109 |
\item{conf.high}{Upper bound on the confidence interval for the estimate.} |
| 110 |
\item{conf.low}{Lower bound on the confidence interval for the estimate.} |
| 111 |
\item{estimate}{The estimated value of the regression term.} |
| 112 |
\item{p.value}{The two-sided p-value associated with the observed statistic.} |
| 113 |
\item{statistic}{The value of a T-statistic to use in a hypothesis that the regression term is non-zero.} |
| 114 |
\item{std.error}{The standard error of the regression term.} |
| 115 |
\item{term}{The name of the regression term.} |
| 116 |
|
| 117 |
} |