Skip to content

Commit 7d9d548

Browse files
committed
docs
1 parent 5b60de3 commit 7d9d548

File tree

7 files changed

+80
-34
lines changed

7 files changed

+80
-34
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
- The `"marginaleffects"` backend for `estimate_means()` is now fully implemented
1313
and no longer work-in-progress.
1414

15+
- `estimate_expectation()` and related functions also get a `by` argument, as
16+
alternative to create a datagrid for the `data` argument.
17+
1518
# modelbased 0.8.9
1619

1720
- Fixed issues related to updates of other *easystats* packages.

R/estimate_predicted.R

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,21 @@
129129
#' @inheritParams estimate_means
130130
#' @inheritParams bayestestR::describe_posterior
131131
#' @param data A data frame with model's predictors to estimate the response. If
132-
#' `NULL`, the model's data is used. If "grid", the model matrix is obtained
133-
#' (through [insight::get_datagrid()]).
132+
#' `NULL`, the model's data is used. If `"grid"`, the model matrix is obtained
133+
#' (through [insight::get_datagrid()]).
134+
#' @param by The predictor variable(s) at which to estimate the response. Other
135+
#' predictors of the model that are not included here will be set to their mean
136+
#' value (for numeric predictors), reference level (for factors) or mode (other
137+
#' types). The `by` argument will be used to create a data grid via
138+
#' `insight::get_datagrid()`, which will then be used as `data` argument. Thus,
139+
#' you cannot specify both `data` and `by` but only of these two arguments.
134140
#' @param ... You can add all the additional control arguments from
135-
#' [insight::get_datagrid()] (used when `data = "grid"`) and
136-
#' [insight::get_predicted()].
141+
#' [insight::get_datagrid()] (used when `data = "grid"`) and
142+
#' [insight::get_predicted()].
143+
#'
144+
#' @return A data frame of predicted values and uncertainty intervals, with
145+
#' class `"estimate_predicted"`. Methods for [`visualisation_recipe()`][visualisation_recipe.estimate_predicted]
146+
#' and [`plot()`][visualisation_recipe.estimate_predicted] are available.
137147
#'
138148
#' @examplesIf all(insight::check_if_installed(c("see", "lme4", "rstanarm"), quietly = TRUE))
139149
#' library(modelbased)
@@ -176,18 +186,17 @@
176186
#' estimate_expectation(model)
177187
#' estimate_relation(model)
178188
#' }
179-
#' @return A data frame of predicted values and uncertainty intervals, with
180-
#' class `"estimate_predicted"`. Methods for [`visualisation_recipe()`][visualisation_recipe.estimate_predicted]
181-
#' and [`plot()`][visualisation_recipe.estimate_predicted] are available.
182189
#' @export
183190
estimate_expectation <- function(model,
184191
data = NULL,
192+
by = NULL,
185193
ci = 0.95,
186194
keep_iterations = FALSE,
187195
...) {
188196
.estimate_predicted(
189197
model,
190198
data = data,
199+
by = by,
191200
ci = ci,
192201
keep_iterations = keep_iterations,
193202
predict = "expectation",
@@ -200,12 +209,14 @@ estimate_expectation <- function(model,
200209
#' @export
201210
estimate_link <- function(model,
202211
data = "grid",
212+
by = NULL,
203213
ci = 0.95,
204214
keep_iterations = FALSE,
205215
...) {
206216
.estimate_predicted(
207217
model,
208218
data = data,
219+
by = by,
209220
ci = ci,
210221
keep_iterations = keep_iterations,
211222
predict = "link",
@@ -217,12 +228,14 @@ estimate_link <- function(model,
217228
#' @export
218229
estimate_prediction <- function(model,
219230
data = NULL,
231+
by = NULL,
220232
ci = 0.95,
221233
keep_iterations = FALSE,
222234
...) {
223235
.estimate_predicted(
224236
model,
225237
data = data,
238+
by = by,
226239
ci = ci,
227240
keep_iterations = keep_iterations,
228241
predict = "prediction",
@@ -234,12 +247,14 @@ estimate_prediction <- function(model,
234247
#' @export
235248
estimate_relation <- function(model,
236249
data = "grid",
250+
by = NULL,
237251
ci = 0.95,
238252
keep_iterations = FALSE,
239253
...) {
240254
.estimate_predicted(
241255
model,
242256
data = data,
257+
by = by,
243258
ci = ci,
244259
keep_iterations = keep_iterations,
245260
predict = "expectation",
@@ -254,10 +269,16 @@ estimate_relation <- function(model,
254269
#' @keywords internal
255270
.estimate_predicted <- function(model,
256271
data = "grid",
272+
by = NULL,
257273
predict = "expectation",
258274
ci = 0.95,
259275
keep_iterations = FALSE,
260276
...) {
277+
# only "by" or "data", but not both
278+
if (!is.null(by) && !is.null(data)) {
279+
insight::format_error("You can only specify one of `by` or `data`, but not both.")
280+
}
281+
261282
# call "get_data()" only once...
262283
model_data <- insight::get_data(model, verbose = FALSE)
263284
is_model <- insight::is_model(model)
@@ -275,6 +296,11 @@ estimate_relation <- function(model,
275296
is_nullmodel <- FALSE
276297
}
277298

299+
# if "by" is provided, get datagrid
300+
if (!is.null(by)) {
301+
data <- insight::get_datagrid(model, by = by, include_response = is_nullmodel, ...)
302+
}
303+
278304
is_grid <- identical(data, "grid")
279305

280306
# check for correct attributes - a data frame of class `datagrid` may

R/estimate_slopes.R

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
#' @inheritParams estimate_means
1717
#'
1818
#' @details
19-
#'
2019
#' The [estimate_slopes()], [estimate_means()] and [estimate_contrasts()]
2120
#' functions are forming a group, as they are all based on *marginal*
22-
#' estimations (estimations based on a model). All three are also built on the
23-
#' \pkg{emmeans} package, so reading its documentation (for instance for
24-
#' [emmeans::emmeans()] and [emmeans::emtrends()]) is recommended to understand
25-
#' the idea behind these types of procedures.
21+
#' estimations (estimations based on a model). All three are built on the
22+
#' **emmeans** or **marginaleffects** package (depending on the `backend`
23+
#' argument), so reading its documentation (for instance [emmeans::emmeans()],
24+
#' [emmeans::emtrends()] or this [website](https://marginaleffects.com/) is
25+
#' recommended to understand the idea behind these types of procedures.
2626
#'
2727
#' - Model-based **predictions** is the basis for all that follows. Indeed,
2828
#' the first thing to understand is how models can be used to make predictions
@@ -70,7 +70,9 @@
7070
#' the effect of x averaged over all conditions, or instead within each
7171
#' condition (`using [estimate_slopes]`).
7272
#'
73-
#' @examplesIf require("emmeans") && require("ggplot2") && require("see")
73+
#' @return A data.frame of class `estimate_slopes`.
74+
#'
75+
#' @examplesIf all(insight::check_if_installed(c("emmeans", "mgcv", "ggplot2", "see"), quietly = TRUE))
7476
#' # Get an idea of the data
7577
#' ggplot(iris, aes(x = Petal.Length, y = Sepal.Width)) +
7678
#' geom_point(aes(color = Species)) +
@@ -86,10 +88,8 @@
8688
#'
8789
#' # Plot it
8890
#' plot(slopes)
89-
#'
9091
#' standardize(slopes)
9192
#'
92-
#' @examplesIf require("mgcv") && require("emmeans") && require("see")
9393
#' model <- mgcv::gam(Sepal.Width ~ s(Petal.Length), data = iris)
9494
#' slopes <- estimate_slopes(model, by = "Petal.Length", length = 50)
9595
#' summary(slopes)
@@ -102,7 +102,6 @@
102102
#' )
103103
#' summary(slopes)
104104
#' plot(slopes)
105-
#' @return A data.frame of class `estimate_slopes`.
106105
#' @export
107106
estimate_slopes <- function(model,
108107
trend = NULL,

man/estimate_contrasts.Rd

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/estimate_expectation.Rd

Lines changed: 19 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/estimate_means.Rd

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/estimate_slopes.Rd

Lines changed: 7 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)