Debian Codegraph @

man/augment.coxph.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/survival-coxph.R
3 \name{augment.coxph}
4 \alias{augment.coxph}
5 \title{Augment data with information from a(n) coxph object}
6 \usage{
7 \method{augment}{coxph}(
8 x,
9 data = model.frame(x),
10 newdata = NULL,
11 type.predict = "lp",
12 type.residuals = "martingale",
13 ...
14 )
15 }
16 \arguments{
17 \item{x}{A \code{coxph} object returned from \code{\link[survival:coxph]{survival::coxph()}}.}
18
19 \item{data}{A \link[base:data.frame]{base::data.frame} or \code{\link[tibble:tibble]{tibble::tibble()}} containing the original
20 data that was used to produce the object \code{x}. Defaults to
21 \code{stats::model.frame(x)} so that \code{augment(my_fit)} returns the augmented
22 original data. \strong{Do not} pass new data to the \code{data} argument.
23 Augment will report information such as influence and cooks distance for
24 data passed to the \code{data} argument. These measures are only defined for
25 the original training data.}
26
27 \item{newdata}{A \code{\link[base:data.frame]{base::data.frame()}} or \code{\link[tibble:tibble]{tibble::tibble()}} containing all
28 the original predictors used to create \code{x}. Defaults to \code{NULL}, indicating
29 that nothing has been passed to \code{newdata}. If \code{newdata} is specified,
30 the \code{data} argument will be ignored.}
31
32 \item{type.predict}{Character indicating type of prediction to use. Passed
33 to the \code{type} argument of the \code{\link[stats:predict]{stats::predict()}} generic. Allowed arguments
34 vary with model class, so be sure to read the \code{predict.my_class}
35 documentation.}
36
37 \item{type.residuals}{Character indicating type of residuals to use. Passed
38 to the \code{type} argument of \code{\link[stats:residuals]{stats::residuals()}} generic. Allowed arguments
39 vary with model class, so be sure to read the \code{residuals.my_class}
40 documentation.}
41
42 \item{...}{Additional arguments. Not used. Needed to match generic
43 signature only. \strong{Cautionary note:} Misspelled arguments will be
44 absorbed in \code{...}, where they will be ignored. If the misspelled
45 argument has a default value, the default value will be used.
46 For example, if you pass \code{conf.lvel = 0.9}, all computation will
47 proceed using \code{conf.level = 0.95}. Two exceptions here are:
48 \itemize{
49 \item \code{tidy()} methods will warn when supplied an \code{exponentiate} argument if
50 it will be ignored.
51 \item \code{augment()} methods will warn when supplied a \code{newdata} argument if it
52 will be ignored.
53 }}
54 }
55 \description{
56 Augment accepts a model object and a dataset and adds
57 information about each observation in the dataset. Most commonly, this
58 includes predicted values in the \code{.fitted} column, residuals in the
59 \code{.resid} column, and standard errors for the fitted values in a \code{.se.fit}
60 column. New columns always begin with a \code{.} prefix to avoid overwriting
61 columns in the original dataset.
62
63 Users may pass data to augment via either the \code{data} argument or the
64 \code{newdata} argument. If the user passes data to the \code{data} argument,
65 it \strong{must} be exactly the data that was used to fit the model
66 object. Pass datasets to \code{newdata} to augment data that was not used
67 during model fitting. This still requires that at least all predictor
68 variable columns used to fit the model are present. If the original outcome
69 variable used to fit the model is not included in \code{newdata}, then no
70 \code{.resid} column will be included in the output.
71
72 Augment will often behave differently depending on whether \code{data} or
73 \code{newdata} is given. This is because there is often information
74 associated with training observations (such as influences or related)
75 measures that is not meaningfully defined for new observations.
76
77 For convenience, many augment methods provide default \code{data} arguments,
78 so that \code{augment(fit)} will return the augmented training data. In these
79 cases, augment tries to reconstruct the original data based on the model
80 object with varying degrees of success.
81
82 The augmented dataset is always returned as a \link[tibble:tibble]{tibble::tibble} with the
83 \strong{same number of rows} as the passed dataset. This means that the passed
84 data must be coercible to a tibble. If a predictor enters the model as part
85 of a matrix of covariates, such as when the model formula uses
86 \code{\link[splines:ns]{splines::ns()}}, \code{\link[stats:poly]{stats::poly()}}, or \code{\link[survival:Surv]{survival::Surv()}}, it is represented
87 as a matrix column.
88
89 We are in the process of defining behaviors for models fit with various
90 \code{na.action} arguments, but make no guarantees about behavior when data is
91 missing at this time.
92 }
93 \details{
94 When the modeling was performed with \code{na.action = "na.omit"}
95 (as is the typical default), rows with NA in the initial data are omitted
96 entirely from the augmented data frame. When the modeling was performed
97 with \code{na.action = "na.exclude"}, one should provide the original data
98 as a second argument, at which point the augmented data will contain those
99 rows (typically with NAs in place of the new columns). If the original data
100 is not provided to \code{\link[=augment]{augment()}} and \code{na.action = "na.exclude"}, a
101 warning is raised and the incomplete rows are dropped.
102 }
103 \examples{
104 \dontshow{if (rlang::is_installed(c("survival", "ggplot2"))) withAutoprint(\{ # examplesIf}
105
106 # load libraries for models and data
107 library(survival)
108
109 # fit model
110 cfit <- coxph(Surv(time, status) ~ age + sex, lung)
111
112 # summarize model fit with tidiers
113 tidy(cfit)
114 tidy(cfit, exponentiate = TRUE)
115
116 lp <- augment(cfit, lung)
117 risks <- augment(cfit, lung, type.predict = "risk")
118 expected <- augment(cfit, lung, type.predict = "expected")
119
120 glance(cfit)
121
122 # also works on clogit models
123 resp <- levels(logan$occupation)
124 n <- nrow(logan)
125 indx <- rep(1:n, length(resp))
126 logan2 <- data.frame(
127 logan[indx, ],
128 id = indx,
129 tocc = factor(rep(resp, each = n))
130 )
131
132 logan2$case <- (logan2$occupation == logan2$tocc)
133
134 cl <- clogit(case ~ tocc + tocc:education + strata(id), logan2)
135
136 tidy(cl)
137 glance(cl)
138
139 library(ggplot2)
140
141 ggplot(lp, aes(age, .fitted, color = sex)) +
142 geom_point()
143
144 ggplot(risks, aes(age, .fitted, color = sex)) +
145 geom_point()
146
147 ggplot(expected, aes(time, .fitted, color = sex)) +
148 geom_point()
149 \dontshow{\}) # examplesIf}
150 }
151 \seealso{
152 \link[stats:na.action]{stats::na.action}
153
154 \code{\link[=augment]{augment()}}, \code{\link[survival:coxph]{survival::coxph()}}
155
156 Other coxph tidiers:
157 \code{\link{glance.coxph}()},
158 \code{\link{tidy.coxph}()}
159
160 Other survival tidiers:
161 \code{\link{augment.survreg}()},
162 \code{\link{glance.aareg}()},
163 \code{\link{glance.cch}()},
164 \code{\link{glance.coxph}()},
165 \code{\link{glance.pyears}()},
166 \code{\link{glance.survdiff}()},
167 \code{\link{glance.survexp}()},
168 \code{\link{glance.survfit}()},
169 \code{\link{glance.survreg}()},
170 \code{\link{tidy.aareg}()},
171 \code{\link{tidy.cch}()},
172 \code{\link{tidy.coxph}()},
173 \code{\link{tidy.pyears}()},
174 \code{\link{tidy.survdiff}()},
175 \code{\link{tidy.survexp}()},
176 \code{\link{tidy.survfit}()},
177 \code{\link{tidy.survreg}()}
178 }
179 \concept{coxph tidiers}
180 \concept{survival tidiers}
181 \value{
182 A \code{\link[tibble:tibble]{tibble::tibble()}} with columns:
183 \item{.fitted}{Fitted or predicted value.}
184 \item{.resid}{The difference between observed and fitted values.}
185 \item{.se.fit}{Standard errors of fitted values.}
186
187 }