evaluate_model: Evaluate a model for specified inputs

Description Usage Arguments Details Value Examples

Description

Find the model outputs for specified inputs. This is equivalent to the generic predict() function, except it will choose sensible values by default. This simplifies getting a quick look at model values.

Usage

1
2
evaluate_model(model = NULL, data = NULL, on_training = FALSE,
  nlevels = 3, at = NULL, ...)

Arguments

model

the model to display graphically

data

optional set of cases from which to extract levels for explanatory variables

on_training

flag whether to use the training data for evaluation. Only needed when there are random terms, e.g. from rand(), shuffle(), .... See details.

nlevels

how many levels to construct for input variables. For quantitative variables, this is a suggestion. pretty() will refine your choice. (default: 3)

at

named list giving specific values at which to hold the variables. Use this to override the automatic generation of levels for any or all explanatory variables. Unlike data = the variables given in at = or ... will be crossed, so that the evaluation will occur at all combinations of the various levels.

...

arguments about or values at which to evaluate the model or the kind of output to be passed along to predict().

Details

This function is set up to let you look easily at typical outputs. The function will choose "typical" levels of the explanatory variables at which to evaluate the model. (See the examples.) The nlevels controls how many levels of these levels to use. If you wish to choose your own levels for one or more explanatory variables, give those variables as named arguments assigned to the levels you want. If you have a data frame with the desired inputs for some or all of the explanatory variables, use the data argument to pass those values.

There are two ways to evaluate the model on the training data. The first is to set the data argument to the same data frame used to train the model. The second is to use the on_training = TRUE argument. These are equivalent unless there is some random component among the explanatory terms, as with 'mosaic::rand()', 'mosaic::shuffle()' and so on. When you want to restrict/force the evaluation at specified values of an explanatory variable, include a vector of those variables in ... for instance sex = "F" will restrict the evaluation to females.

Value

A dataframe containing both the explanatory variable inputs and the resulting model output (in the 'model_value' field). This differs from the output of predict(), which for many model classes/architectures may be a vector or matrix.

A data frame containing both the inputs to the model and the corresponding outputs.

Examples

1
2
3
4
5
6
7
8
## Not run: mod1 <- lm(wage ~ age * sex + sector, data = mosaicData::CPS85)
evaluate_model(mod1)
mod3 <- glm(married == "Married" ~ age + sex * sector,
            data = mosaicData::CPS85, family = "binomial")
evaluate_model(mod3, nlevels = 2, type = "response")
evaluate_model(mod3, nlevels = 2, type = "response", at = list(sex = "F"))

## End(Not run)

dtkaplan/gghelper documentation built on May 15, 2019, 5 p.m.