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)
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
183190estimate_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
201210estimate_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
218229estimate_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
235248estimate_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
0 commit comments