2323# '
2424# ' @param x A modelbased object.
2525# ' @param show_data Logical, if `TRUE`, display the "raw" data as a background
26- # ' to the model-based estimation. This argument will be ignored for plotting
27- # ' objects returned by `estimate_slopes()` or `estimate_grouplevel()`.
26+ # ' to the model-based estimation. For mixed models, you can additionally use the
27+ # ' `collapse_group` argument to "collapse" data points by random effects
28+ # ' grouping factors. Argument `show_data` will be ignored for plotting objects
29+ # ' returned by `estimate_slopes()` or `estimate_grouplevel()`.
2830# ' @param join_dots Logical, if `TRUE` (default) and for categorical focal terms
2931# ' in `by`, dots (estimates) are connected by lines, i.e. plots will be a
3032# ' combination of dots with error bars and connecting lines. If `FALSE`, only
3941# ' predictor. Use `FALSE` to always use continuous color scales for numeric
4042# ' predictors. It is possible to set a global default value using `options()`,
4143# ' e.g. `options(modelbased_numeric_as_discrete = 10)`.
42- # ' @param show_residuals Logical, if `TRUE`, display residuals of the model
43- # ' as a background to the model-based estimation. Residuals will be computed
44- # ' for the predictors in the data grid, using [`residualize_over_grid()`].
44+ # ' @param show_residuals Logical, if `TRUE`, display residuals of the model as a
45+ # ' background to the model-based estimation. Residuals will be computed for the
46+ # ' predictors in the data grid, using [`residualize_over_grid()`]. For mixed
47+ # ' models, you can additionally use the `collapse_group` argument to "collapse"
48+ # ' data points from residuals by random effects grouping factors.
49+ # ' @param collapse_group This argument only takes effect when either `show_data`
50+ # ' or `show_residuals` is `TRUE`. For mixed effects models, name of the grouping
51+ # ' variable of random effects. If `collapse_group = TRUE`, data points
52+ # ' "collapsed" by the first random effect groups are added to the plot. Else, if
53+ # ' `collapse_group` is a name of a group factor, data is collapsed by that
54+ # ' specific random effect. See [`collapse_by_group()`] for further details.
4555# ' @param point,line,pointrange,ribbon,facet,grid Additional
4656# ' aesthetics and parameters for the geoms (see customization example).
4757# ' @param ... Arguments passed from `plot()` to `visualisation_recipe()`, or
7484# ' will set a default value for the `dodge` argument (spacing between geoms)
7585# ' when using `tinyplot::plt()`. Should be a number between `0` and `1`.
7686# '
77- # ' @examplesIf all(insight::check_if_installed(c("marginaleffects", "see", "ggplot2"), quietly = TRUE)) && getRversion() >= "4.1.0"
87+ # ' @examplesIf all(insight::check_if_installed(c("marginaleffects", "see", "ggplot2", "lme4" ), quietly = TRUE)) && getRversion() >= "4.1.0"
7888# ' library(ggplot2)
7989# ' library(see)
8090# ' # ==============================================
152162# ' x <- estimate_means(model, by = c("cyl", "wt"))
153163# ' plot(x)
154164# '
155- # '
156165# ' # GLMs ---------------------
157166# ' data <- data.frame(vs = mtcars$vs, cyl = as.factor(mtcars$cyl))
158167# ' x <- estimate_means(glm(vs ~ cyl, data = data, family = "binomial"), by = c("cyl"))
159168# ' plot(x)
169+ # '
170+ # ' # ==============================================
171+ # ' # Adding original data to the plot
172+ # ' # ==============================================
173+ # ' data(efc, package = "modelbased")
174+ # ' efc$e15relat <- as.factor(efc$e15relat)
175+ # ' efc$c161sex <- as.factor(efc$c161sex)
176+ # ' levels(efc$c161sex) <- c("male", "female")
177+ # ' model <- lme4::lmer(neg_c_7 ~ c161sex + (1 | e15relat), data = efc)
178+ # '
179+ # ' me <- estimate_means(model, "c161sex")
180+ # ' plot(me, show_data = TRUE)
181+ # '
182+ # ' # data points: collapse by / average over random effects groups -------
183+ # ' plot(me, show_data = TRUE, collapse_group = "e15relat")
160184# ' }
161185# ' @export
162- visualisation_recipe.estimate_predicted <- function (x ,
163- show_data = FALSE ,
164- show_residuals = FALSE ,
165- point = NULL ,
166- line = NULL ,
167- pointrange = NULL ,
168- ribbon = NULL ,
169- facet = NULL ,
170- grid = NULL ,
171- join_dots = NULL ,
172- numeric_as_discrete = NULL ,
173- ... ) {
186+ visualisation_recipe.estimate_predicted <- function (
187+ x ,
188+ show_data = FALSE ,
189+ show_residuals = FALSE ,
190+ collapse_group = NULL ,
191+ point = NULL ,
192+ line = NULL ,
193+ pointrange = NULL ,
194+ ribbon = NULL ,
195+ facet = NULL ,
196+ grid = NULL ,
197+ join_dots = NULL ,
198+ numeric_as_discrete = NULL ,
199+ ...
200+ ) {
174201 # Process argument ---------------------------------------------------------
175202 # --------------------------------------------------------------------------
176203
@@ -186,6 +213,7 @@ visualisation_recipe.estimate_predicted <- function(x,
186213 x ,
187214 show_data = show_data ,
188215 show_residuals = show_residuals ,
216+ collapse_by = collapse_group ,
189217 point = point ,
190218 line = line ,
191219 pointrange = pointrange ,
@@ -233,13 +261,15 @@ visualisation_recipe.estimate_means <- visualisation_recipe.estimate_predicted
233261# ' plot(visualisation_recipe(x))
234262# ' }
235263# ' @export
236- visualisation_recipe.estimate_slopes <- function (x ,
237- line = NULL ,
238- pointrange = NULL ,
239- ribbon = NULL ,
240- facet = NULL ,
241- grid = NULL ,
242- ... ) {
264+ visualisation_recipe.estimate_slopes <- function (
265+ x ,
266+ line = NULL ,
267+ pointrange = NULL ,
268+ ribbon = NULL ,
269+ facet = NULL ,
270+ grid = NULL ,
271+ ...
272+ ) {
243273 .visualization_recipe(
244274 x ,
245275 show_data = FALSE ,
@@ -285,13 +315,15 @@ visualisation_recipe.estimate_slopes <- function(x,
285315# ' plot(x)
286316# ' }
287317# ' @export
288- visualisation_recipe.estimate_grouplevel <- function (x ,
289- line = NULL ,
290- pointrange = NULL ,
291- ribbon = NULL ,
292- facet = NULL ,
293- grid = NULL ,
294- ... ) {
318+ visualisation_recipe.estimate_grouplevel <- function (
319+ x ,
320+ line = NULL ,
321+ pointrange = NULL ,
322+ ribbon = NULL ,
323+ facet = NULL ,
324+ grid = NULL ,
325+ ...
326+ ) {
295327 if (is.null(facet )) {
296328 facet <- list (scales = " free" )
297329 } else {
0 commit comments