Skip to content

Commit 3d67907

Browse files
authored
Deprecate fortify() for models (#6193)
* mark deprecation in docs * add `broom` to suggests * throw deprecation warnings * highlight {broom} functions in examples * collect in single file * add news bullet
1 parent cf7ec18 commit 3d67907

File tree

7 files changed

+215
-241
lines changed

7 files changed

+215
-241
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Imports:
4444
vctrs (>= 0.6.0),
4545
withr (>= 2.5.0)
4646
Suggests:
47+
broom,
4748
covr,
4849
dplyr,
4950
ggplot2movies,
@@ -126,9 +127,8 @@ Collate:
126127
'facet-grid-.R'
127128
'facet-null.R'
128129
'facet-wrap.R'
129-
'fortify-lm.R'
130130
'fortify-map.R'
131-
'fortify-multcomp.R'
131+
'fortify-models.R'
132132
'fortify-spatial.R'
133133
'fortify.R'
134134
'stat-.R'

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# ggplot2 (development version)
22

3+
* The following methods have been deprecated: `fortify.lm()`, `fortify.glht()`,
4+
`fortify.confint.glht()`, `fortify.summary.glht()` and `fortify.cld()`. It
5+
is recommend to use `broom::augment()` and `broom::tidy()` instead
6+
(@teunbrand, #3816).
37
* Custom and raster annotation now respond to scale transformations, and can
48
use AsIs variables for relative placement (@teunbrand based on
59
@yutannihilation's prior work, #3120)

R/fortify-lm.R

Lines changed: 0 additions & 87 deletions
This file was deleted.

R/fortify-models.R

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
#' Supplement the data fitted to a linear model with model fit statistics.
2+
#'
3+
#' @description
4+
#' `r lifecycle::badge("deprecated")`
5+
#'
6+
#' This method is deprecated because using `broom::augment()` is a better
7+
#' solution to supplement data from a linear model.
8+
#' If you have missing values in your model data, you may need to refit
9+
#' the model with `na.action = na.exclude`.
10+
#'
11+
#' @return The original data with extra columns:
12+
#' \item{.hat}{Diagonal of the hat matrix}
13+
#' \item{.sigma}{Estimate of residual standard deviation when
14+
#' corresponding observation is dropped from model}
15+
#' \item{.cooksd}{Cooks distance, [cooks.distance()]}
16+
#' \item{.fitted}{Fitted values of model}
17+
#' \item{.resid}{Residuals}
18+
#' \item{.stdresid}{Standardised residuals}
19+
#' @param model linear model
20+
#' @param data data set, defaults to data used to fit model
21+
#' @param ... not used by this method
22+
#' @keywords internal
23+
#' @export
24+
#' @examplesIf require("broom")
25+
#' mod <- lm(mpg ~ wt, data = mtcars)
26+
#'
27+
#' # Show augmented model
28+
#' head(augment(mod))
29+
#' head(fortify(mod))
30+
#'
31+
#' # Using augment to convert model to ready-to-plot data
32+
#' ggplot(augment(mod), aes(.fitted, .resid)) +
33+
#' geom_point() +
34+
#' geom_hline(yintercept = 0) +
35+
#' geom_smooth(se = FALSE)
36+
#'
37+
#' # Colouring by original data not included in the model
38+
#' ggplot(augment(mod, mtcars), aes(.fitted, .std.resid, colour = factor(cyl))) +
39+
#' geom_point()
40+
fortify.lm <- function(model, data = model$model, ...) {
41+
lifecycle::deprecate_warn(
42+
"3.6.0", I("`fortify(<lm>)`"), I("`broom::augment(<lm>)`")
43+
)
44+
infl <- stats::influence(model, do.coef = FALSE)
45+
data$.hat <- infl$hat
46+
data$.sigma <- infl$sigma
47+
data$.cooksd <- stats::cooks.distance(model, infl)
48+
49+
data$.fitted <- stats::predict(model)
50+
data$.resid <- stats::resid(model)
51+
data$.stdresid <- stats::rstandard(model, infl)
52+
53+
data
54+
}
55+
56+
#' Fortify methods for objects produced by \pkg{multcomp}
57+
#'
58+
#' @description
59+
#' `r lifecycle::badge("deprecated")`
60+
#'
61+
#' This function is deprecated because using `broom::tidy()` is a better
62+
#' solution to convert model objects.
63+
#'
64+
#' @param model an object of class `glht`, `confint.glht`,
65+
#' `summary.glht` or [multcomp::cld()]
66+
#' @param data,... other arguments to the generic ignored in this method.
67+
#' @name fortify-multcomp
68+
#' @keywords internal
69+
#' @examplesIf require("multcomp") && require("broom")
70+
#' amod <- aov(breaks ~ wool + tension, data = warpbreaks)
71+
#' wht <- multcomp::glht(amod, linfct = multcomp::mcp(tension = "Tukey"))
72+
#'
73+
#' tidy(wht) # recommended
74+
#' fortify(wht)
75+
#'
76+
#' ggplot(tidy(wht), aes(contrast, estimate)) + geom_point()
77+
#'
78+
#' ci <- confint(wht)
79+
#' tidy(ci) # recommended
80+
#' fortify(ci)
81+
#'
82+
#' ggplot(tidy(confint(wht)),
83+
#' aes(contrast, estimate, ymin = conf.low, ymax = conf.high)) +
84+
#' geom_pointrange()
85+
#'
86+
#' smry <- summary(wht)
87+
#' tidy(smry) # recommended
88+
#' fortify(smry)
89+
#'
90+
#' ggplot(mapping = aes(contrast, estimate)) +
91+
#' geom_linerange(aes(ymin = conf.low, ymax = conf.high), data = tidy(ci)) +
92+
#' geom_point(aes(size = adj.p.value), data = tidy(smry)) +
93+
#' scale_size(transform = "reverse")
94+
#'
95+
#' cld <- multcomp::cld(wht)
96+
#' tidy(cld) # recommended
97+
#' fortify(cld)
98+
NULL
99+
100+
#' @method fortify glht
101+
#' @rdname fortify-multcomp
102+
#' @export
103+
fortify.glht <- function(model, data, ...) {
104+
lifecycle::deprecate_warn(
105+
"3.6.0", I("`fortify(<glht>)`"), I("`broom::tidy(<glht>)`")
106+
)
107+
base::data.frame(
108+
lhs = rownames(model$linfct),
109+
rhs = model$rhs,
110+
estimate = stats::coef(model),
111+
check.names = FALSE,
112+
stringsAsFactors = FALSE
113+
)
114+
}
115+
116+
#' @rdname fortify-multcomp
117+
#' @method fortify confint.glht
118+
#' @export
119+
fortify.confint.glht <- function(model, data, ...) {
120+
lifecycle::deprecate_warn(
121+
"3.6.0", I("`fortify(<confint.glht>)`"), I("`broom::tidy(<confint.glht>)`")
122+
)
123+
coef <- model$confint
124+
colnames(coef) <- to_lower_ascii(colnames(coef))
125+
126+
base::data.frame(
127+
lhs = rownames(coef),
128+
rhs = model$rhs,
129+
coef,
130+
check.names = FALSE,
131+
stringsAsFactors = FALSE
132+
)
133+
}
134+
135+
#' @method fortify summary.glht
136+
#' @rdname fortify-multcomp
137+
#' @export
138+
fortify.summary.glht <- function(model, data, ...) {
139+
lifecycle::deprecate_warn(
140+
"3.6.0", I("`fortify(<summary.glht>)`"), I("`broom::tidy(<summary.glht>)`")
141+
)
142+
coef <- as.data.frame(
143+
model$test[c("coefficients", "sigma", "tstat", "pvalues")])
144+
names(coef) <- c("estimate", "se", "t", "p")
145+
146+
base::data.frame(
147+
lhs = rownames(coef),
148+
rhs = model$rhs,
149+
coef,
150+
check.names = FALSE,
151+
stringsAsFactors = FALSE
152+
)
153+
}
154+
155+
156+
#' @method fortify cld
157+
#' @rdname fortify-multcomp
158+
#' @export
159+
fortify.cld <- function(model, data, ...) {
160+
lifecycle::deprecate_warn(
161+
"3.6.0", I("`fortify(<summary.glht>)`"), I("`broom::tidy(<summary.glht>)`")
162+
)
163+
base::data.frame(
164+
lhs = names(model$mcletters$Letters),
165+
letters = model$mcletters$Letters,
166+
check.names = FALSE,
167+
stringsAsFactors = FALSE
168+
)
169+
}

R/fortify-multcomp.R

Lines changed: 0 additions & 89 deletions
This file was deleted.

0 commit comments

Comments
 (0)