| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/mfx.R |
| 3 |
\name{augment.mfx} |
| 4 |
\alias{augment.mfx} |
| 5 |
\alias{augment.logitmfx} |
| 6 |
\alias{augment.negbinmfx} |
| 7 |
\alias{augment.poissonmfx} |
| 8 |
\alias{augment.probitmfx} |
| 9 |
\title{Augment data with information from a(n) mfx object} |
| 10 |
\usage{ |
| 11 |
\method{augment}{mfx}( |
| 12 |
x, |
| 13 |
data = model.frame(x$fit), |
| 14 |
newdata = NULL, |
| 15 |
type.predict = c("link", "response", "terms"), |
| 16 |
type.residuals = c("deviance", "pearson"), |
| 17 |
se_fit = FALSE, |
| 18 |
... |
| 19 |
) |
| 20 |
|
| 21 |
\method{augment}{logitmfx}( |
| 22 |
x, |
| 23 |
data = model.frame(x$fit), |
| 24 |
newdata = NULL, |
| 25 |
type.predict = c("link", "response", "terms"), |
| 26 |
type.residuals = c("deviance", "pearson"), |
| 27 |
se_fit = FALSE, |
| 28 |
... |
| 29 |
) |
| 30 |
|
| 31 |
\method{augment}{negbinmfx}( |
| 32 |
x, |
| 33 |
data = model.frame(x$fit), |
| 34 |
newdata = NULL, |
| 35 |
type.predict = c("link", "response", "terms"), |
| 36 |
type.residuals = c("deviance", "pearson"), |
| 37 |
se_fit = FALSE, |
| 38 |
... |
| 39 |
) |
| 40 |
|
| 41 |
\method{augment}{poissonmfx}( |
| 42 |
x, |
| 43 |
data = model.frame(x$fit), |
| 44 |
newdata = NULL, |
| 45 |
type.predict = c("link", "response", "terms"), |
| 46 |
type.residuals = c("deviance", "pearson"), |
| 47 |
se_fit = FALSE, |
| 48 |
... |
| 49 |
) |
| 50 |
|
| 51 |
\method{augment}{probitmfx}( |
| 52 |
x, |
| 53 |
data = model.frame(x$fit), |
| 54 |
newdata = NULL, |
| 55 |
type.predict = c("link", "response", "terms"), |
| 56 |
type.residuals = c("deviance", "pearson"), |
| 57 |
se_fit = FALSE, |
| 58 |
... |
| 59 |
) |
| 60 |
} |
| 61 |
\arguments{ |
| 62 |
\item{x}{A \code{logitmfx}, \code{negbinmfx}, \code{poissonmfx}, or \code{probitmfx} object. |
| 63 |
(Note that \code{betamfx} objects receive their own set of tidiers.)} |
| 64 |
|
| 65 |
\item{data}{A \link[base:data.frame]{base::data.frame} or \code{\link[tibble:tibble]{tibble::tibble()}} containing the original |
| 66 |
data that was used to produce the object \code{x}. Defaults to |
| 67 |
\code{stats::model.frame(x)} so that \code{augment(my_fit)} returns the augmented |
| 68 |
original data. \strong{Do not} pass new data to the \code{data} argument. |
| 69 |
Augment will report information such as influence and cooks distance for |
| 70 |
data passed to the \code{data} argument. These measures are only defined for |
| 71 |
the original training data.} |
| 72 |
|
| 73 |
\item{newdata}{A \code{\link[base:data.frame]{base::data.frame()}} or \code{\link[tibble:tibble]{tibble::tibble()}} containing all |
| 74 |
the original predictors used to create \code{x}. Defaults to \code{NULL}, indicating |
| 75 |
that nothing has been passed to \code{newdata}. If \code{newdata} is specified, |
| 76 |
the \code{data} argument will be ignored.} |
| 77 |
|
| 78 |
\item{type.predict}{Passed to \code{\link[stats:predict.glm]{stats::predict.glm()}} \code{type} argument. |
| 79 |
Defaults to \code{"link"}.} |
| 80 |
|
| 81 |
\item{type.residuals}{Passed to \code{\link[stats:glm.summaries]{stats::residuals.glm()}} and |
| 82 |
to \code{\link[stats:influence.measures]{stats::rstandard.glm()}} \code{type} arguments. Defaults to \code{"deviance"}.} |
| 83 |
|
| 84 |
\item{se_fit}{Logical indicating whether or not a \code{.se.fit} column should be |
| 85 |
added to the augmented output. For some models, this calculation can be |
| 86 |
somewhat time-consuming. Defaults to \code{FALSE}.} |
| 87 |
|
| 88 |
\item{...}{Additional arguments. Not used. Needed to match generic |
| 89 |
signature only. \strong{Cautionary note:} Misspelled arguments will be |
| 90 |
absorbed in \code{...}, where they will be ignored. If the misspelled |
| 91 |
argument has a default value, the default value will be used. |
| 92 |
For example, if you pass \code{conf.lvel = 0.9}, all computation will |
| 93 |
proceed using \code{conf.level = 0.95}. Two exceptions here are: |
| 94 |
\itemize{ |
| 95 |
\item \code{tidy()} methods will warn when supplied an \code{exponentiate} argument if |
| 96 |
it will be ignored. |
| 97 |
\item \code{augment()} methods will warn when supplied a \code{newdata} argument if it |
| 98 |
will be ignored. |
| 99 |
}} |
| 100 |
} |
| 101 |
\description{ |
| 102 |
Augment accepts a model object and a dataset and adds |
| 103 |
information about each observation in the dataset. Most commonly, this |
| 104 |
includes predicted values in the \code{.fitted} column, residuals in the |
| 105 |
\code{.resid} column, and standard errors for the fitted values in a \code{.se.fit} |
| 106 |
column. New columns always begin with a \code{.} prefix to avoid overwriting |
| 107 |
columns in the original dataset. |
| 108 |
|
| 109 |
Users may pass data to augment via either the \code{data} argument or the |
| 110 |
\code{newdata} argument. If the user passes data to the \code{data} argument, |
| 111 |
it \strong{must} be exactly the data that was used to fit the model |
| 112 |
object. Pass datasets to \code{newdata} to augment data that was not used |
| 113 |
during model fitting. This still requires that at least all predictor |
| 114 |
variable columns used to fit the model are present. If the original outcome |
| 115 |
variable used to fit the model is not included in \code{newdata}, then no |
| 116 |
\code{.resid} column will be included in the output. |
| 117 |
|
| 118 |
Augment will often behave differently depending on whether \code{data} or |
| 119 |
\code{newdata} is given. This is because there is often information |
| 120 |
associated with training observations (such as influences or related) |
| 121 |
measures that is not meaningfully defined for new observations. |
| 122 |
|
| 123 |
For convenience, many augment methods provide default \code{data} arguments, |
| 124 |
so that \code{augment(fit)} will return the augmented training data. In these |
| 125 |
cases, augment tries to reconstruct the original data based on the model |
| 126 |
object with varying degrees of success. |
| 127 |
|
| 128 |
The augmented dataset is always returned as a \link[tibble:tibble]{tibble::tibble} with the |
| 129 |
\strong{same number of rows} as the passed dataset. This means that the passed |
| 130 |
data must be coercible to a tibble. If a predictor enters the model as part |
| 131 |
of a matrix of covariates, such as when the model formula uses |
| 132 |
\code{\link[splines:ns]{splines::ns()}}, \code{\link[stats:poly]{stats::poly()}}, or \code{\link[survival:Surv]{survival::Surv()}}, it is represented |
| 133 |
as a matrix column. |
| 134 |
|
| 135 |
We are in the process of defining behaviors for models fit with various |
| 136 |
\code{na.action} arguments, but make no guarantees about behavior when data is |
| 137 |
missing at this time. |
| 138 |
} |
| 139 |
\details{ |
| 140 |
This generic augment method wraps \code{\link[=augment.glm]{augment.glm()}} for applicable |
| 141 |
objects from the \code{mfx} package. |
| 142 |
} |
| 143 |
\examples{ |
| 144 |
\dontshow{if (rlang::is_installed("mfx")) withAutoprint(\{ # examplesIf} |
| 145 |
|
| 146 |
# load libraries for models and data |
| 147 |
library(mfx) |
| 148 |
|
| 149 |
# get the marginal effects from a logit regression |
| 150 |
mod_logmfx <- logitmfx(am ~ cyl + hp + wt, atmean = TRUE, data = mtcars) |
| 151 |
|
| 152 |
tidy(mod_logmfx, conf.int = TRUE) |
| 153 |
|
| 154 |
# compare with the naive model coefficients of the same logit call |
| 155 |
tidy( |
| 156 |
glm(am ~ cyl + hp + wt, family = binomial, data = mtcars), |
| 157 |
conf.int = TRUE |
| 158 |
) |
| 159 |
|
| 160 |
augment(mod_logmfx) |
| 161 |
glance(mod_logmfx) |
| 162 |
|
| 163 |
# another example, this time using probit regression |
| 164 |
mod_probmfx <- probitmfx(am ~ cyl + hp + wt, atmean = TRUE, data = mtcars) |
| 165 |
|
| 166 |
tidy(mod_probmfx, conf.int = TRUE) |
| 167 |
augment(mod_probmfx) |
| 168 |
glance(mod_probmfx) |
| 169 |
\dontshow{\}) # examplesIf} |
| 170 |
} |
| 171 |
\seealso{ |
| 172 |
\code{\link[=augment.glm]{augment.glm()}}, \code{\link[mfx:logitmfx]{mfx::logitmfx()}}, \code{\link[mfx:negbinmfx]{mfx::negbinmfx()}}, |
| 173 |
\code{\link[mfx:poissonmfx]{mfx::poissonmfx()}}, \code{\link[mfx:probitmfx]{mfx::probitmfx()}} |
| 174 |
|
| 175 |
Other mfx tidiers: |
| 176 |
\code{\link{augment.betamfx}()}, |
| 177 |
\code{\link{glance.betamfx}()}, |
| 178 |
\code{\link{glance.mfx}()}, |
| 179 |
\code{\link{tidy.betamfx}()}, |
| 180 |
\code{\link{tidy.mfx}()} |
| 181 |
} |
| 182 |
\concept{mfx tidiers} |
| 183 |
\value{ |
| 184 |
A \code{\link[tibble:tibble]{tibble::tibble()}} with columns: |
| 185 |
\item{.cooksd}{Cooks distance.} |
| 186 |
\item{.fitted}{Fitted or predicted value.} |
| 187 |
\item{.hat}{Diagonal of the hat matrix.} |
| 188 |
\item{.resid}{The difference between observed and fitted values.} |
| 189 |
\item{.se.fit}{Standard errors of fitted values.} |
| 190 |
\item{.sigma}{Estimated residual standard deviation when corresponding observation is dropped from model.} |
| 191 |
\item{.std.resid}{Standardised residuals.} |
| 192 |
|
| 193 |
} |