Model-based response estimates and uncertainty
Source:R/estimate_predicted.R
estimate_expectation.Rd
After fitting a model, it is useful generate model-based estimates of the response variables for different combinations of predictor values. Such estimates can be used to make inferences about relationships between variables and to make predictions about individual cases.
Model-based response estimates and uncertainty can be generated for both the conditional average response values (the regression line or expectation) and for predictions about individual cases. See below for details.
Usage
estimate_expectation(
model,
data = NULL,
by = NULL,
ci = 0.95,
keep_iterations = FALSE,
...
)
estimate_link(
model,
data = "grid",
by = NULL,
ci = 0.95,
keep_iterations = FALSE,
...
)
estimate_prediction(
model,
data = NULL,
by = NULL,
ci = 0.95,
keep_iterations = FALSE,
...
)
estimate_relation(
model,
data = "grid",
by = NULL,
ci = 0.95,
keep_iterations = FALSE,
...
)
Arguments
- model
A statistical model.
- data
A data frame with model's predictors to estimate the response. If
NULL
, the model's data is used. If"grid"
, the model matrix is obtained (throughinsight::get_datagrid()
).- by
The predictor variable(s) at which to estimate the response. Other predictors of the model that are not included here will be set to their mean value (for numeric predictors), reference level (for factors) or mode (other types). The
by
argument will be used to create a data grid viainsight::get_datagrid()
, which will then be used asdata
argument. Thus, you cannot specify bothdata
andby
but only of these two arguments.- ci
Confidence Interval (CI) level. Default to
0.95
(95%
).- keep_iterations
If
TRUE
, will keep all iterations (draws) of bootstrapped or Bayesian models. They will be added as additional columns namediter_1, iter_2, ...
. You can reshape them to a long format by runningreshape_iterations()
.- ...
You can add all the additional control arguments from
insight::get_datagrid()
(used whendata = "grid"
) andinsight::get_predicted()
.
Value
A data frame of predicted values and uncertainty intervals, with
class "estimate_predicted"
. Methods for visualisation_recipe()
and plot()
are available.
Note
These functions are built on top of insight::get_predicted()
and correspond
to different specifications of its parameters. It may be useful to read its
documentation,
in particular the description of the predict
argument for additional
details on the difference between expected vs. predicted values and link vs.
response scales.
Additional control parameters can be used to control results from
insight::get_datagrid()
(when data = "grid"
) and from
insight::get_predicted()
(the function used internally to compute
predictions).
For plotting, check the examples in visualisation_recipe()
. Also check out
the Vignettes and README examples for
various examples, tutorials and usecases.
Expected (average) values
The most important way that various types of response estimates differ is in terms of what quantity is being estimated and the meaning of the uncertainty intervals. The major choices are expected values for uncertainty in the regression line and predicted values for uncertainty in the individual case predictions.
Expected values refer to the fitted regression line - the estimated average response value (i.e., the "expectation") for individuals with specific predictor values. For example, in a linear model y = 2 + 3x + 4z + e, the estimated average y for individuals with x = 1 and z = 2 is 11.
For expected values, uncertainty intervals refer to uncertainty in the estimated conditional average (where might the true regression line actually fall)? Uncertainty intervals for expected values are also called "confidence intervals".
Expected values and their uncertainty intervals are useful for describing the relationship between variables and for describing how precisely a model has been estimated.
For generalized linear models, expected values are reported on one of two scales:
The link scale refers to scale of the fitted regression line, after transformation by the link function. For example, for a logistic regression (logit binomial) model, the link scale gives expected log-odds. For a log-link Poisson model, the link scale gives the expected log-count.
The response scale refers to the original scale of the response variable (i.e., without any link function transformation). Expected values on the link scale are back-transformed to the original response variable metric (e.g., expected probabilities for binomial models, expected counts for Poisson models).
Individual case predictions
In contrast to expected values, predicted values refer to predictions for individual cases. Predicted values are also called "posterior predictions" or "posterior predictive draws".
For predicted values, uncertainty intervals refer to uncertainty in the individual response values for each case (where might any single case actually fall)? Uncertainty intervals for predicted values are also called "prediction intervals" or "posterior predictive intervals".
Predicted values and their uncertainty intervals are useful for forecasting the range of values that might be observed in new data, for making decisions about individual cases, and for checking if model predictions are reasonable ("posterior predictive checks").
Predicted values and intervals are always on the scale of the original response variable (not the link scale).
Functions for estimating predicted values and uncertainty
modelbased provides 4 functions for generating model-based response estimates and their uncertainty:
estimate_expectation()
:Generates expected values (conditional average) on the response scale.
The uncertainty interval is a confidence interval.
By default, values are computed using the data used to fit the model.
estimate_link()
:Generates expected values (conditional average) on the link scale.
The uncertainty interval is a confidence interval.
By default, values are computed using a reference grid spanning the observed range of predictor values (see
visualisation_matrix()
).
estimate_prediction()
:Generates predicted values (for individual cases) on the response scale.
The uncertainty interval is a prediction interval.
By default, values are computed using the data used to fit the model.
estimate_relation()
:Like
estimate_expectation()
.Useful for visualizing a model.
Generates expected values (conditional average) on the response scale.
The uncertainty interval is a confidence interval.
By default, values are computed using a reference grid spanning the observed range of predictor values (see
visualisation_matrix()
).
Data for predictions
If the data = NULL
, values are estimated using the data used to fit the
model. If data = "grid"
, values are computed using a reference grid
spanning the observed range of predictor values with
visualisation_matrix()
. This can be useful for model visualization. The
number of predictor values used for each variable can be controlled with the
length
argument. data
can also be a data frame containing columns with
names matching the model frame (see insight::get_data()
). This can be used
to generate model predictions for specific combinations of predictor values.
Examples
library(modelbased)
# Linear Models
model <- lm(mpg ~ wt, data = mtcars)
# Get predicted and prediction interval (see insight::get_predicted)
estimate_expectation(model)
#> Model-based Expectation
#>
#> wt | Predicted | SE | 95% CI | Residuals
#> ----------------------------------------------------
#> 2.62 | 23.28 | 0.63 | [21.99, 24.58] | -2.28
#> 2.88 | 21.92 | 0.57 | [20.75, 23.09] | -0.92
#> 2.32 | 24.89 | 0.74 | [23.38, 26.39] | -2.09
#> 3.21 | 20.10 | 0.54 | [19.00, 21.20] | 1.30
#> 3.44 | 18.90 | 0.55 | [17.77, 20.03] | -0.20
#> 3.46 | 18.79 | 0.56 | [17.66, 19.93] | -0.69
#> 3.57 | 18.21 | 0.57 | [17.03, 19.38] | -3.91
#> 3.19 | 20.24 | 0.54 | [19.14, 21.34] | 4.16
#> 3.15 | 20.45 | 0.54 | [19.35, 21.55] | 2.35
#> 3.44 | 18.90 | 0.55 | [17.77, 20.03] | 0.30
#> 3.44 | 18.90 | 0.55 | [17.77, 20.03] | -1.10
#> 4.07 | 15.53 | 0.72 | [14.06, 17.00] | 0.87
#> 3.73 | 17.35 | 0.61 | [16.10, 18.60] | -0.05
#> 3.78 | 17.08 | 0.62 | [15.81, 18.36] | -1.88
#> 5.25 | 9.23 | 1.26 | [ 6.66, 11.80] | 1.17
#> 5.42 | 8.30 | 1.35 | [ 5.55, 11.05] | 2.10
#> 5.34 | 8.72 | 1.31 | [ 6.05, 11.39] | 5.98
#> 2.20 | 25.53 | 0.78 | [23.93, 27.13] | 6.87
#> 1.61 | 28.65 | 1.05 | [26.52, 30.79] | 1.75
#> 1.83 | 27.48 | 0.94 | [25.55, 29.40] | 6.42
#> 2.46 | 24.11 | 0.68 | [22.72, 25.51] | -2.61
#> 3.52 | 18.47 | 0.56 | [17.32, 19.63] | -2.97
#> 3.44 | 18.93 | 0.55 | [17.80, 20.05] | -3.73
#> 3.84 | 16.76 | 0.64 | [15.45, 18.07] | -3.46
#> 3.85 | 16.74 | 0.64 | [15.42, 18.05] | 2.46
#> 1.94 | 26.94 | 0.90 | [25.11, 28.77] | 0.36
#> 2.14 | 25.85 | 0.81 | [24.20, 27.50] | 0.15
#> 1.51 | 29.20 | 1.09 | [26.96, 31.43] | 1.20
#> 3.17 | 20.34 | 0.54 | [19.24, 21.44] | -4.54
#> 2.77 | 22.48 | 0.59 | [21.27, 23.69] | -2.78
#> 3.57 | 18.21 | 0.57 | [17.03, 19.38] | -3.21
#> 2.78 | 22.43 | 0.59 | [21.22, 23.64] | -1.03
#>
#> Variable predicted: mpg
#>
# Get expected values with confidence interval
pred <- estimate_relation(model)
pred
#> Model-based Expectation
#>
#> wt | Predicted | SE | 95% CI
#> ----------------------------------------
#> 1.51 | 29.20 | 1.09 | [26.96, 31.43]
#> 1.95 | 26.88 | 0.89 | [25.06, 28.70]
#> 2.38 | 24.55 | 0.71 | [23.10, 26.01]
#> 2.82 | 22.23 | 0.58 | [21.04, 23.42]
#> 3.25 | 19.91 | 0.54 | [18.81, 21.01]
#> 3.69 | 17.59 | 0.60 | [16.36, 18.81]
#> 4.12 | 15.26 | 0.74 | [13.76, 16.77]
#> 4.55 | 12.94 | 0.92 | [11.06, 14.82]
#> 4.99 | 10.62 | 1.13 | [ 8.32, 12.92]
#> 5.42 | 8.30 | 1.35 | [ 5.55, 11.05]
#>
#> Variable predicted: mpg
#> Predictors modulated: wt
#>
# Visualisation (see visualisation_recipe())
plot(pred)
# Standardize predictions
pred <- estimate_relation(lm(mpg ~ wt + am, data = mtcars))
z <- standardize(pred, include_response = FALSE)
z
#> Model-based Expectation (standardized)
#>
#> wt | am | Predicted | SE | 95% CI
#> -------------------------------------------------
#> -1.74 | -0.81 | 29.22 | 1.91 | [25.31, 33.14]
#> -1.30 | -0.81 | 26.90 | 1.60 | [23.62, 30.17]
#> -0.85 | -0.81 | 24.57 | 1.30 | [21.90, 27.24]
#> -0.41 | -0.81 | 22.24 | 1.03 | [20.13, 24.36]
#> 0.03 | -0.81 | 19.92 | 0.82 | [18.24, 21.59]
#> 0.48 | -0.81 | 17.59 | 0.71 | [16.13, 19.05]
#> 0.92 | -0.81 | 15.27 | 0.76 | [13.71, 16.83]
#> 1.37 | -0.81 | 12.94 | 0.94 | [11.01, 14.87]
#> 1.81 | -0.81 | 10.61 | 1.20 | [ 8.17, 13.06]
#> 2.26 | -0.81 | 8.29 | 1.49 | [ 5.25, 11.33]
#> -1.74 | -0.59 | 29.22 | 1.78 | [25.58, 32.86]
#> -1.30 | -0.59 | 26.89 | 1.46 | [23.90, 29.89]
#> -0.85 | -0.59 | 24.57 | 1.17 | [22.19, 26.95]
#> -0.41 | -0.59 | 22.24 | 0.90 | [20.40, 24.08]
#> 0.03 | -0.59 | 19.92 | 0.70 | [18.48, 21.35]
#> 0.48 | -0.59 | 17.59 | 0.64 | [16.28, 18.90]
#> 0.92 | -0.59 | 15.26 | 0.75 | [13.73, 16.80]
#> 1.37 | -0.59 | 12.94 | 0.98 | [10.94, 14.93]
#> 1.81 | -0.59 | 10.61 | 1.26 | [ 8.04, 13.18]
#> 2.26 | -0.59 | 8.29 | 1.56 | [ 5.09, 11.48]
#> -1.74 | -0.37 | 29.22 | 1.65 | [25.85, 32.59]
#> -1.30 | -0.37 | 26.89 | 1.33 | [24.17, 29.62]
#> -0.85 | -0.37 | 24.57 | 1.04 | [22.45, 26.68]
#> -0.41 | -0.37 | 22.24 | 0.78 | [20.65, 23.83]
#> 0.03 | -0.37 | 19.91 | 0.61 | [18.67, 21.16]
#> 0.48 | -0.37 | 17.59 | 0.61 | [16.34, 18.83]
#> 0.92 | -0.37 | 15.26 | 0.78 | [13.67, 16.85]
#> 1.37 | -0.37 | 12.93 | 1.04 | [10.81, 15.06]
#> 1.81 | -0.37 | 10.61 | 1.33 | [ 7.88, 13.34]
#> 2.26 | -0.37 | 8.28 | 1.65 | [ 4.91, 11.66]
#> -1.74 | -0.15 | 29.21 | 1.53 | [26.10, 32.33]
#> -1.30 | -0.15 | 26.89 | 1.21 | [24.41, 29.37]
#> -0.85 | -0.15 | 24.56 | 0.92 | [22.68, 26.45]
#> -0.41 | -0.15 | 22.24 | 0.68 | [20.85, 23.63]
#> 0.03 | -0.15 | 19.91 | 0.56 | [18.77, 21.05]
#> 0.48 | -0.15 | 17.58 | 0.63 | [16.30, 18.86]
#> 0.92 | -0.15 | 15.26 | 0.84 | [13.54, 16.98]
#> 1.37 | -0.15 | 12.93 | 1.12 | [10.64, 15.23]
#> 1.81 | -0.15 | 10.61 | 1.43 | [ 7.68, 13.53]
#> 2.26 | -0.15 | 8.28 | 1.75 | [ 4.70, 11.86]
#> -1.74 | 0.08 | 29.21 | 1.41 | [26.32, 32.10]
#> -1.30 | 0.08 | 26.89 | 1.11 | [24.62, 29.15]
#> -0.85 | 0.08 | 24.56 | 0.83 | [22.87, 26.25]
#> -0.41 | 0.08 | 22.23 | 0.61 | [20.98, 23.49]
#> 0.03 | 0.08 | 19.91 | 0.55 | [18.78, 21.04]
#> 0.48 | 0.08 | 17.58 | 0.69 | [16.18, 18.98]
#> 0.92 | 0.08 | 15.26 | 0.93 | [13.35, 17.16]
#> 1.37 | 0.08 | 12.93 | 1.23 | [10.42, 15.44]
#> 1.81 | 0.08 | 10.60 | 1.54 | [ 7.46, 13.75]
#> 2.26 | 0.08 | 8.28 | 1.86 | [ 4.47, 12.09]
#> -1.74 | 0.30 | 29.21 | 1.31 | [26.52, 31.90]
#> -1.30 | 0.30 | 26.88 | 1.02 | [24.80, 28.96]
#> -0.85 | 0.30 | 24.56 | 0.76 | [23.01, 26.11]
#> -0.41 | 0.30 | 22.23 | 0.59 | [21.02, 23.44]
#> 0.03 | 0.30 | 19.91 | 0.60 | [18.67, 21.14]
#> 0.48 | 0.30 | 17.58 | 0.78 | [15.98, 19.17]
#> 0.92 | 0.30 | 15.25 | 1.04 | [13.12, 17.39]
#> 1.37 | 0.30 | 12.93 | 1.34 | [10.18, 15.67]
#> 1.81 | 0.30 | 10.60 | 1.66 | [ 7.21, 13.99]
#> 2.26 | 0.30 | 8.27 | 1.98 | [ 4.22, 12.33]
#> -1.74 | 0.52 | 29.21 | 1.23 | [26.69, 31.73]
#> -1.30 | 0.52 | 26.88 | 0.95 | [24.93, 28.83]
#> -0.85 | 0.52 | 24.55 | 0.73 | [23.07, 26.04]
#> -0.41 | 0.52 | 22.23 | 0.62 | [20.96, 23.50]
#> 0.03 | 0.52 | 19.90 | 0.69 | [18.49, 21.32]
#> 0.48 | 0.52 | 17.58 | 0.90 | [15.74, 19.41]
#> 0.92 | 0.52 | 15.25 | 1.17 | [12.86, 17.64]
#> 1.37 | 0.52 | 12.92 | 1.47 | [ 9.92, 15.93]
#> 1.81 | 0.52 | 10.60 | 1.79 | [ 6.94, 14.25]
#> 2.26 | 0.52 | 8.27 | 2.11 | [ 3.95, 12.59]
#> -1.74 | 0.74 | 29.20 | 1.17 | [26.81, 31.59]
#> -1.30 | 0.74 | 26.88 | 0.91 | [25.01, 28.75]
#> -0.85 | 0.74 | 24.55 | 0.73 | [23.05, 26.05]
#> -0.41 | 0.74 | 22.23 | 0.69 | [20.81, 23.64]
#> 0.03 | 0.74 | 19.90 | 0.81 | [18.25, 21.55]
#> 0.48 | 0.74 | 17.57 | 1.03 | [15.47, 19.68]
#> 0.92 | 0.74 | 15.25 | 1.30 | [12.58, 17.92]
#> 1.37 | 0.74 | 12.92 | 1.61 | [ 9.64, 16.21]
#> 1.81 | 0.74 | 10.60 | 1.92 | [ 6.67, 14.53]
#> 2.26 | 0.74 | 8.27 | 2.24 | [ 3.68, 12.86]
#> -1.74 | 0.97 | 29.20 | 1.13 | [26.89, 31.51]
#> -1.30 | 0.97 | 26.88 | 0.91 | [25.02, 28.73]
#> -0.85 | 0.97 | 24.55 | 0.78 | [22.95, 26.15]
#> -0.41 | 0.97 | 22.22 | 0.79 | [20.60, 23.85]
#> 0.03 | 0.97 | 19.90 | 0.94 | [17.97, 21.82]
#> 0.48 | 0.97 | 17.57 | 1.17 | [15.17, 19.97]
#> 0.92 | 0.97 | 15.25 | 1.45 | [12.28, 18.21]
#> 1.37 | 0.97 | 12.92 | 1.75 | [ 9.34, 16.50]
#> 1.81 | 0.97 | 10.59 | 2.06 | [ 6.38, 14.81]
#> 2.26 | 0.97 | 8.27 | 2.38 | [ 3.39, 13.14]
#> -1.74 | 1.19 | 29.20 | 1.11 | [26.92, 31.48]
#> -1.30 | 1.19 | 26.87 | 0.93 | [24.96, 28.78]
#> -0.85 | 1.19 | 24.55 | 0.86 | [22.79, 26.30]
#> -0.41 | 1.19 | 22.22 | 0.92 | [20.35, 24.10]
#> 0.03 | 1.19 | 19.89 | 1.08 | [17.68, 22.11]
#> 0.48 | 1.19 | 17.57 | 1.32 | [14.86, 20.27]
#> 0.92 | 1.19 | 15.24 | 1.60 | [11.97, 18.51]
#> 1.37 | 1.19 | 12.92 | 1.90 | [ 9.04, 16.79]
#> 1.81 | 1.19 | 10.59 | 2.21 | [ 6.08, 15.10]
#> 2.26 | 1.19 | 8.26 | 2.53 | [ 3.10, 13.43]
#>
#> Variable predicted: mpg
#> Predictors modulated: wt, am
#>
unstandardize(z, include_response = FALSE)
#> Model-based Expectation (standardized)
#>
#> wt | am | Predicted | SE | 95% CI
#> -----------------------------------------------
#> 1.51 | 0.00 | 29.22 | 1.91 | [25.31, 33.14]
#> 1.95 | 0.00 | 26.90 | 1.60 | [23.62, 30.17]
#> 2.38 | 0.00 | 24.57 | 1.30 | [21.90, 27.24]
#> 2.82 | 0.00 | 22.24 | 1.03 | [20.13, 24.36]
#> 3.25 | 0.00 | 19.92 | 0.82 | [18.24, 21.59]
#> 3.69 | 0.00 | 17.59 | 0.71 | [16.13, 19.05]
#> 4.12 | 0.00 | 15.27 | 0.76 | [13.71, 16.83]
#> 4.55 | 0.00 | 12.94 | 0.94 | [11.01, 14.87]
#> 4.99 | 0.00 | 10.61 | 1.20 | [ 8.17, 13.06]
#> 5.42 | 0.00 | 8.29 | 1.49 | [ 5.25, 11.33]
#> 1.51 | 0.11 | 29.22 | 1.78 | [25.58, 32.86]
#> 1.95 | 0.11 | 26.89 | 1.46 | [23.90, 29.89]
#> 2.38 | 0.11 | 24.57 | 1.17 | [22.19, 26.95]
#> 2.82 | 0.11 | 22.24 | 0.90 | [20.40, 24.08]
#> 3.25 | 0.11 | 19.92 | 0.70 | [18.48, 21.35]
#> 3.69 | 0.11 | 17.59 | 0.64 | [16.28, 18.90]
#> 4.12 | 0.11 | 15.26 | 0.75 | [13.73, 16.80]
#> 4.55 | 0.11 | 12.94 | 0.98 | [10.94, 14.93]
#> 4.99 | 0.11 | 10.61 | 1.26 | [ 8.04, 13.18]
#> 5.42 | 0.11 | 8.29 | 1.56 | [ 5.09, 11.48]
#> 1.51 | 0.22 | 29.22 | 1.65 | [25.85, 32.59]
#> 1.95 | 0.22 | 26.89 | 1.33 | [24.17, 29.62]
#> 2.38 | 0.22 | 24.57 | 1.04 | [22.45, 26.68]
#> 2.82 | 0.22 | 22.24 | 0.78 | [20.65, 23.83]
#> 3.25 | 0.22 | 19.91 | 0.61 | [18.67, 21.16]
#> 3.69 | 0.22 | 17.59 | 0.61 | [16.34, 18.83]
#> 4.12 | 0.22 | 15.26 | 0.78 | [13.67, 16.85]
#> 4.55 | 0.22 | 12.93 | 1.04 | [10.81, 15.06]
#> 4.99 | 0.22 | 10.61 | 1.33 | [ 7.88, 13.34]
#> 5.42 | 0.22 | 8.28 | 1.65 | [ 4.91, 11.66]
#> 1.51 | 0.33 | 29.21 | 1.53 | [26.10, 32.33]
#> 1.95 | 0.33 | 26.89 | 1.21 | [24.41, 29.37]
#> 2.38 | 0.33 | 24.56 | 0.92 | [22.68, 26.45]
#> 2.82 | 0.33 | 22.24 | 0.68 | [20.85, 23.63]
#> 3.25 | 0.33 | 19.91 | 0.56 | [18.77, 21.05]
#> 3.69 | 0.33 | 17.58 | 0.63 | [16.30, 18.86]
#> 4.12 | 0.33 | 15.26 | 0.84 | [13.54, 16.98]
#> 4.55 | 0.33 | 12.93 | 1.12 | [10.64, 15.23]
#> 4.99 | 0.33 | 10.61 | 1.43 | [ 7.68, 13.53]
#> 5.42 | 0.33 | 8.28 | 1.75 | [ 4.70, 11.86]
#> 1.51 | 0.44 | 29.21 | 1.41 | [26.32, 32.10]
#> 1.95 | 0.44 | 26.89 | 1.11 | [24.62, 29.15]
#> 2.38 | 0.44 | 24.56 | 0.83 | [22.87, 26.25]
#> 2.82 | 0.44 | 22.23 | 0.61 | [20.98, 23.49]
#> 3.25 | 0.44 | 19.91 | 0.55 | [18.78, 21.04]
#> 3.69 | 0.44 | 17.58 | 0.69 | [16.18, 18.98]
#> 4.12 | 0.44 | 15.26 | 0.93 | [13.35, 17.16]
#> 4.55 | 0.44 | 12.93 | 1.23 | [10.42, 15.44]
#> 4.99 | 0.44 | 10.60 | 1.54 | [ 7.46, 13.75]
#> 5.42 | 0.44 | 8.28 | 1.86 | [ 4.47, 12.09]
#> 1.51 | 0.56 | 29.21 | 1.31 | [26.52, 31.90]
#> 1.95 | 0.56 | 26.88 | 1.02 | [24.80, 28.96]
#> 2.38 | 0.56 | 24.56 | 0.76 | [23.01, 26.11]
#> 2.82 | 0.56 | 22.23 | 0.59 | [21.02, 23.44]
#> 3.25 | 0.56 | 19.91 | 0.60 | [18.67, 21.14]
#> 3.69 | 0.56 | 17.58 | 0.78 | [15.98, 19.17]
#> 4.12 | 0.56 | 15.25 | 1.04 | [13.12, 17.39]
#> 4.55 | 0.56 | 12.93 | 1.34 | [10.18, 15.67]
#> 4.99 | 0.56 | 10.60 | 1.66 | [ 7.21, 13.99]
#> 5.42 | 0.56 | 8.27 | 1.98 | [ 4.22, 12.33]
#> 1.51 | 0.67 | 29.21 | 1.23 | [26.69, 31.73]
#> 1.95 | 0.67 | 26.88 | 0.95 | [24.93, 28.83]
#> 2.38 | 0.67 | 24.55 | 0.73 | [23.07, 26.04]
#> 2.82 | 0.67 | 22.23 | 0.62 | [20.96, 23.50]
#> 3.25 | 0.67 | 19.90 | 0.69 | [18.49, 21.32]
#> 3.69 | 0.67 | 17.58 | 0.90 | [15.74, 19.41]
#> 4.12 | 0.67 | 15.25 | 1.17 | [12.86, 17.64]
#> 4.55 | 0.67 | 12.92 | 1.47 | [ 9.92, 15.93]
#> 4.99 | 0.67 | 10.60 | 1.79 | [ 6.94, 14.25]
#> 5.42 | 0.67 | 8.27 | 2.11 | [ 3.95, 12.59]
#> 1.51 | 0.78 | 29.20 | 1.17 | [26.81, 31.59]
#> 1.95 | 0.78 | 26.88 | 0.91 | [25.01, 28.75]
#> 2.38 | 0.78 | 24.55 | 0.73 | [23.05, 26.05]
#> 2.82 | 0.78 | 22.23 | 0.69 | [20.81, 23.64]
#> 3.25 | 0.78 | 19.90 | 0.81 | [18.25, 21.55]
#> 3.69 | 0.78 | 17.57 | 1.03 | [15.47, 19.68]
#> 4.12 | 0.78 | 15.25 | 1.30 | [12.58, 17.92]
#> 4.55 | 0.78 | 12.92 | 1.61 | [ 9.64, 16.21]
#> 4.99 | 0.78 | 10.60 | 1.92 | [ 6.67, 14.53]
#> 5.42 | 0.78 | 8.27 | 2.24 | [ 3.68, 12.86]
#> 1.51 | 0.89 | 29.20 | 1.13 | [26.89, 31.51]
#> 1.95 | 0.89 | 26.88 | 0.91 | [25.02, 28.73]
#> 2.38 | 0.89 | 24.55 | 0.78 | [22.95, 26.15]
#> 2.82 | 0.89 | 22.22 | 0.79 | [20.60, 23.85]
#> 3.25 | 0.89 | 19.90 | 0.94 | [17.97, 21.82]
#> 3.69 | 0.89 | 17.57 | 1.17 | [15.17, 19.97]
#> 4.12 | 0.89 | 15.25 | 1.45 | [12.28, 18.21]
#> 4.55 | 0.89 | 12.92 | 1.75 | [ 9.34, 16.50]
#> 4.99 | 0.89 | 10.59 | 2.06 | [ 6.38, 14.81]
#> 5.42 | 0.89 | 8.27 | 2.38 | [ 3.39, 13.14]
#> 1.51 | 1.00 | 29.20 | 1.11 | [26.92, 31.48]
#> 1.95 | 1.00 | 26.87 | 0.93 | [24.96, 28.78]
#> 2.38 | 1.00 | 24.55 | 0.86 | [22.79, 26.30]
#> 2.82 | 1.00 | 22.22 | 0.92 | [20.35, 24.10]
#> 3.25 | 1.00 | 19.89 | 1.08 | [17.68, 22.11]
#> 3.69 | 1.00 | 17.57 | 1.32 | [14.86, 20.27]
#> 4.12 | 1.00 | 15.24 | 1.60 | [11.97, 18.51]
#> 4.55 | 1.00 | 12.92 | 1.90 | [ 9.04, 16.79]
#> 4.99 | 1.00 | 10.59 | 2.21 | [ 6.08, 15.10]
#> 5.42 | 1.00 | 8.26 | 2.53 | [ 3.10, 13.43]
#>
#> Variable predicted: mpg
#> Predictors modulated: wt, am
#>
# Logistic Models
model <- glm(vs ~ wt, data = mtcars, family = "binomial")
estimate_expectation(model)
#> Model-based Expectation
#>
#> wt | Predicted | SE | 95% CI | Residuals
#> --------------------------------------------------
#> 2.62 | 0.67 | 0.12 | [0.40, 0.86] | -0.67
#> 2.88 | 0.56 | 0.12 | [0.33, 0.76] | -0.56
#> 2.32 | 0.78 | 0.12 | [0.47, 0.94] | 0.22
#> 3.21 | 0.39 | 0.11 | [0.21, 0.61] | 0.61
#> 3.44 | 0.30 | 0.11 | [0.14, 0.53] | -0.30
#> 3.46 | 0.29 | 0.10 | [0.13, 0.53] | 0.71
#> 3.57 | 0.25 | 0.10 | [0.10, 0.50] | -0.25
#> 3.19 | 0.41 | 0.11 | [0.22, 0.62] | 0.59
#> 3.15 | 0.42 | 0.11 | [0.24, 0.64] | 0.58
#> 3.44 | 0.30 | 0.11 | [0.14, 0.53] | 0.70
#> 3.44 | 0.30 | 0.11 | [0.14, 0.53] | 0.70
#> 4.07 | 0.11 | 0.08 | [0.02, 0.39] | -0.11
#> 3.73 | 0.20 | 0.10 | [0.07, 0.46] | -0.20
#> 3.78 | 0.18 | 0.10 | [0.06, 0.45] | -0.18
#> 5.25 | 0.01 | 0.02 | [0.00, 0.24] | -0.01
#> 5.42 | 9.49e-03 | 0.02 | [0.00, 0.23] | -9.49e-03
#> 5.34 | 0.01 | 0.02 | [0.00, 0.23] | -0.01
#> 2.20 | 0.82 | 0.12 | [0.49, 0.96] | 0.18
#> 1.61 | 0.93 | 0.07 | [0.58, 0.99] | 0.07
#> 1.83 | 0.90 | 0.09 | [0.55, 0.99] | 0.10
#> 2.46 | 0.73 | 0.13 | [0.44, 0.91] | 0.27
#> 3.52 | 0.27 | 0.10 | [0.11, 0.51] | -0.27
#> 3.44 | 0.30 | 0.11 | [0.14, 0.53] | -0.30
#> 3.84 | 0.16 | 0.10 | [0.05, 0.43] | -0.16
#> 3.85 | 0.16 | 0.10 | [0.05, 0.43] | -0.16
#> 1.94 | 0.88 | 0.10 | [0.54, 0.98] | 0.12
#> 2.14 | 0.84 | 0.11 | [0.50, 0.96] | -0.84
#> 1.51 | 0.94 | 0.07 | [0.60, 0.99] | 0.06
#> 3.17 | 0.42 | 0.11 | [0.23, 0.63] | -0.42
#> 2.77 | 0.60 | 0.12 | [0.36, 0.80] | -0.60
#> 3.57 | 0.25 | 0.10 | [0.10, 0.50] | -0.25
#> 2.78 | 0.60 | 0.12 | [0.36, 0.80] | 0.40
#>
#> Variable predicted: vs
#>
estimate_relation(model)
#> Model-based Expectation
#>
#> wt | Predicted | SE | 95% CI
#> --------------------------------------
#> 1.51 | 0.94 | 0.07 | [0.60, 0.99]
#> 1.95 | 0.88 | 0.10 | [0.53, 0.98]
#> 2.38 | 0.76 | 0.12 | [0.46, 0.92]
#> 2.82 | 0.58 | 0.12 | [0.35, 0.78]
#> 3.25 | 0.38 | 0.11 | [0.20, 0.60]
#> 3.69 | 0.21 | 0.10 | [0.07, 0.47]
#> 4.12 | 0.10 | 0.08 | [0.02, 0.38]
#> 4.55 | 0.05 | 0.05 | [0.01, 0.32]
#> 4.99 | 0.02 | 0.03 | [0.00, 0.27]
#> 5.42 | 9.49e-03 | 0.02 | [0.00, 0.23]
#>
#> Variable predicted: vs
#> Predictors modulated: wt
#>
# Mixed models
model <- lme4::lmer(mpg ~ wt + (1 | gear), data = mtcars)
estimate_expectation(model)
#> Model-based Expectation
#>
#> wt | gear | Predicted | SE | 95% CI | Residuals
#> -----------------------------------------------------------
#> 2.62 | 4.00 | 24.04 | 0.97 | [22.07, 26.02] | -3.04
#> 2.88 | 4.00 | 22.76 | 0.93 | [20.86, 24.65] | -1.76
#> 2.32 | 4.00 | 25.56 | 1.04 | [23.42, 27.70] | -2.76
#> 3.21 | 3.00 | 19.64 | 0.92 | [17.77, 21.52] | 1.76
#> 3.44 | 3.00 | 18.51 | 0.94 | [16.59, 20.42] | 0.19
#> 3.46 | 3.00 | 18.41 | 0.94 | [16.48, 20.33] | -0.31
#> 3.57 | 3.00 | 17.85 | 0.96 | [15.89, 19.81] | -3.55
#> 3.19 | 4.00 | 21.17 | 0.91 | [19.30, 23.04] | 3.23
#> 3.15 | 4.00 | 21.37 | 0.91 | [19.50, 23.24] | 1.43
#> 3.44 | 4.00 | 19.91 | 0.94 | [17.99, 21.83] | -0.71
#> 3.44 | 4.00 | 19.91 | 0.94 | [17.99, 21.83] | -2.11
#> 4.07 | 3.00 | 15.33 | 1.10 | [13.08, 17.58] | 1.07
#> 3.73 | 3.00 | 17.04 | 0.99 | [15.01, 19.08] | 0.26
#> 3.78 | 3.00 | 16.79 | 1.01 | [14.73, 18.86] | -1.59
#> 5.25 | 3.00 | 9.37 | 1.64 | [ 6.01, 12.74] | 1.03
#> 5.42 | 3.00 | 8.50 | 1.74 | [ 4.94, 12.06] | 1.90
#> 5.34 | 3.00 | 8.90 | 1.70 | [ 5.42, 12.37] | 5.80
#> 2.20 | 4.00 | 26.16 | 1.08 | [23.94, 28.38] | 6.24
#> 1.61 | 4.00 | 29.11 | 1.32 | [26.40, 31.82] | 1.29
#> 1.83 | 4.00 | 28.00 | 1.23 | [25.49, 30.51] | 5.90
#> 2.46 | 3.00 | 23.42 | 1.00 | [21.37, 25.48] | -1.92
#> 3.52 | 3.00 | 18.10 | 0.95 | [16.16, 20.05] | -2.60
#> 3.44 | 3.00 | 18.53 | 0.94 | [16.61, 20.45] | -3.33
#> 3.84 | 3.00 | 16.49 | 1.02 | [14.39, 18.59] | -3.19
#> 3.85 | 3.00 | 16.46 | 1.03 | [14.36, 18.56] | 2.74
#> 1.94 | 4.00 | 27.50 | 1.18 | [25.08, 29.92] | -0.20
#> 2.14 | 5.00 | 24.65 | 1.10 | [22.39, 26.91] | 1.35
#> 1.51 | 5.00 | 27.81 | 1.37 | [25.01, 30.62] | 2.59
#> 3.17 | 5.00 | 19.45 | 0.91 | [17.58, 21.33] | -3.65
#> 2.77 | 5.00 | 21.47 | 0.94 | [19.55, 23.40] | -1.77
#> 3.57 | 5.00 | 17.44 | 0.96 | [15.47, 19.40] | -2.44
#> 2.78 | 4.00 | 23.24 | 0.94 | [21.32, 25.16] | -1.84
#>
#> Variable predicted: mpg
#>
estimate_relation(model)
#> Model-based Expectation
#>
#> wt | gear | Predicted | SE | 95% CI
#> -----------------------------------------------
#> 1.51 | 0.00 | 28.56 | 1.37 | [25.75, 31.37]
#> 1.95 | 0.00 | 26.36 | 1.18 | [23.95, 28.78]
#> 2.38 | 0.00 | 24.17 | 1.03 | [22.07, 26.27]
#> 2.82 | 0.00 | 21.98 | 0.93 | [20.07, 23.89]
#> 3.25 | 0.00 | 19.79 | 0.92 | [17.91, 21.67]
#> 3.69 | 0.00 | 17.60 | 0.98 | [15.58, 19.61]
#> 4.12 | 0.00 | 15.40 | 1.12 | [13.11, 17.69]
#> 4.55 | 0.00 | 13.21 | 1.30 | [10.55, 15.87]
#> 4.99 | 0.00 | 11.02 | 1.51 | [ 7.93, 14.11]
#> 5.42 | 0.00 | 8.83 | 1.74 | [ 5.27, 12.39]
#>
#> Variable predicted: mpg
#> Predictors modulated: wt
#>
# Bayesian models
# \donttest{
model <- suppressWarnings(rstanarm::stan_glm(
mpg ~ wt,
data = mtcars, refresh = 0, iter = 200
))
estimate_expectation(model)
#> Model-based Expectation
#>
#> wt | Predicted | SE | 95% CI | Residuals
#> ----------------------------------------------------
#> 2.62 | 23.29 | 0.60 | [22.17, 24.53] | -2.29
#> 2.88 | 21.93 | 0.54 | [20.97, 23.11] | -0.93
#> 2.32 | 24.89 | 0.69 | [23.49, 26.24] | -2.09
#> 3.21 | 20.12 | 0.51 | [19.17, 21.33] | 1.28
#> 3.44 | 18.92 | 0.53 | [17.94, 20.13] | -0.22
#> 3.46 | 18.82 | 0.53 | [17.83, 20.02] | -0.72
#> 3.57 | 18.23 | 0.55 | [17.23, 19.41] | -3.93
#> 3.19 | 20.25 | 0.51 | [19.30, 21.44] | 4.15
#> 3.15 | 20.47 | 0.51 | [19.51, 21.66] | 2.33
#> 3.44 | 18.92 | 0.53 | [17.94, 20.13] | 0.28
#> 3.44 | 18.92 | 0.53 | [17.94, 20.13] | -1.12
#> 4.07 | 15.57 | 0.69 | [14.26, 16.83] | 0.83
#> 3.73 | 17.38 | 0.59 | [16.23, 18.60] | -0.08
#> 3.78 | 17.11 | 0.60 | [15.93, 18.39] | -1.91
#> 5.25 | 9.29 | 1.20 | [ 7.06, 11.45] | 1.11
#> 5.42 | 8.36 | 1.29 | [ 5.99, 10.66] | 2.04
#> 5.34 | 8.78 | 1.25 | [ 6.49, 11.02] | 5.92
#> 2.20 | 25.53 | 0.74 | [24.02, 26.91] | 6.87
#> 1.61 | 28.64 | 0.98 | [26.65, 30.41] | 1.76
#> 1.83 | 27.47 | 0.89 | [25.67, 29.09] | 6.43
#> 2.46 | 24.11 | 0.64 | [22.86, 25.43] | -2.61
#> 3.52 | 18.50 | 0.54 | [17.51, 19.68] | -3.00
#> 3.44 | 18.95 | 0.53 | [17.97, 20.16] | -3.75
#> 3.84 | 16.79 | 0.62 | [15.58, 18.08] | -3.49
#> 3.85 | 16.77 | 0.62 | [15.56, 18.05] | 2.43
#> 1.94 | 26.94 | 0.84 | [25.22, 28.48] | 0.36
#> 2.14 | 25.84 | 0.76 | [24.31, 27.25] | 0.16
#> 1.51 | 29.18 | 1.03 | [27.10, 31.02] | 1.22
#> 3.17 | 20.36 | 0.51 | [19.41, 21.55] | -4.56
#> 2.77 | 22.49 | 0.56 | [21.48, 23.69] | -2.79
#> 3.57 | 18.23 | 0.55 | [17.23, 19.41] | -3.23
#> 2.78 | 22.44 | 0.56 | [21.42, 23.63] | -1.04
#>
#> Variable predicted: mpg
#>
estimate_relation(model)
#> Model-based Expectation
#>
#> wt | Predicted | SE | 95% CI
#> ----------------------------------------
#> 1.51 | 29.18 | 1.03 | [27.10, 31.02]
#> 1.95 | 26.87 | 0.84 | [25.17, 28.40]
#> 2.38 | 24.56 | 0.67 | [23.22, 25.90]
#> 2.82 | 22.24 | 0.55 | [21.25, 23.41]
#> 3.25 | 19.93 | 0.51 | [18.94, 21.15]
#> 3.69 | 17.62 | 0.57 | [16.50, 18.79]
#> 4.12 | 15.30 | 0.71 | [13.95, 16.60]
#> 4.55 | 12.99 | 0.88 | [11.30, 14.63]
#> 4.99 | 10.67 | 1.08 | [ 8.64, 12.62]
#> 5.42 | 8.36 | 1.29 | [ 5.99, 10.66]
#>
#> Variable predicted: mpg
#> Predictors modulated: wt
#>
# }