pretty_predictions_1d | R Documentation |
This function plots pretty one-dimensional predictions from a statistical model
. Given a model
, for each predictor, the function plots the predicted values of the response and associated 95 percent confidence intervals. Other predictors are held at the first level (for factors) or an average (e.g., mean or median, as specified) value (for doubles) or at custom values specified in a dataframe called newdata
.
pretty_predictions_1d(
model,
data = NULL,
newdata = NULL,
constants = NULL,
x_var = NULL,
n_pred = 100,
average = mean,
extract_fit = function(p) p$fit,
extract_se = function(p) p$se.fit,
transform_x = NULL,
xlim = NULL,
ylim = NULL,
ylim_fix = TRUE,
pretty_axis_args = list(),
add_points = list(cex = 0.5, lwd = 0.5, col = "grey20"),
add_points_jitter = list(),
add_error_bars = list(add_fit = list(pch = 3)),
add_error_envelope = list(),
add_order = c("predictions", "points"),
add_xlab = list(line = 2.5),
add_ylab = list(line = 2.5),
add_main = list(adj = 0, font = 2),
one_page = TRUE,
...
)
model |
A model (e.g. an output from |
data |
(optional) The dataframe used to fit the model. If missing, this is extracted via |
x_var , n_pred , average , newdata , constants |
(optional) Prediction controls.
|
extract_fit , extract_se |
Functions that extract fitted values and standard errors, respectively, from the object returned by |
transform_x |
A function to transform values of the predictor(s) for plotting. |
xlim , ylim , ylim_fix , pretty_axis_args |
Axis controls. |
add_points |
(optional) A named list of arguments, passed to |
add_points_jitter |
A named list of jitter values for points. If supplied, element names should correspond to selected predictor variables in the model. Each element should contain two non-negative numbers that define the amount to jitter points in the x and y directions. Jittering is implemented using |
add_error_bars , add_error_envelope |
(optional) Named lists of arguments, passed to |
add_order |
A character vector or a list that defines the order in which you want to add |
add_xlab , add_ylab , add_main |
(optional) Named lists of arguments, passed to |
one_page |
A logical variable that defines whether or not to plot all plots on one page. |
... |
Additional arguments passed to |
Interactions are not currently supported.
The function plots predictions from a model. A list of axis parameters, with one element (from pretty_axis
) for each variable, is returned invisibly.
Edward Lavender
pretty_predictions_2d
#### Define a model for predictions
mod_1 <- stats::lm(Sepal.Length ~ Sepal.Width + Species, data = iris)
summary(mod_1)
#### Example (1): Plot predictions using default options
pretty_predictions_1d(mod_1)
#### Example (2): Plot predictions for specified variables
pretty_predictions_1d(mod_1, x_var = c("Sepal.Width"))
pretty_predictions_1d(mod_1, x_var = c("Sepal.Width", "Species"))
#### Example (3): Plot predictions using custom newdata
p_dat <- data.frame(Sepal.Width = median(iris$Sepal.Width),
Species = factor(levels(iris$Species),
levels = levels(iris$Species)))
pretty_predictions_1d(mod_1,
x_var = "Species",
newdata = p_dat)
# Or use constants to use custom constants but standard values for x_var
pretty_predictions_1d(mod_1,
constants = data.frame(Sepal.Width = 3),
x_var = "Species")
#### Example (4): Customise uncertainty
pretty_predictions_1d(mod_1,
add_error_bars = list(cex = 5, bg = "black"),
add_error_envelope = list(type = "lines"))
#### Example (5): Customise axes
pretty_predictions_1d(mod_1,
ylim = c(NA, 10))
pretty_predictions_1d(mod_1,
ylim_fix = FALSE)
pretty_predictions_1d(mod_1,
pretty_axis_args = list(control_digits = 2))
#### Example (6): Customise titles
pretty_predictions_1d(mod_1,
add_xlab = list(text = c("Width", "Species"), line = 2),
add_ylab = list(line = -2),
add_main = NULL)
#### Example (7) Back transformations
# The function can be implemented with tranformed variables, but note:
# * Transformations must be applied to the dataframe used to fit the model
# ... (and not in the model fitting function);
# * Transformations should not change variable types (e.g. scale() transforms
# ... numbers into matrices, and this is not permitted).
# * All numeric variables are affected by transform_x
## (A) Scale variable and plot predictions on transformed scale
# Define function to scale numbers that doesn't change variable types
scale_num <- function(x) {
y <- scale(x)
attributes(y)$dim <- NULL
y
}
# Scale Sepal.Width
iris$Sepal.Width.S <- scale_num(iris$Sepal.Width)
# Implement model
mod_2 <- stats::lm(Sepal.Length ~ Sepal.Width.S + Species, data = iris)
# Visualise predictions
pretty_predictions_1d(mod_2)
## (B) Back-transform predictions
unscale <- function(x) {
mu <- attr(x, "scaled:center")
sigma <- attr(x, "scaled:scale")
x * sigma + mu
}
pretty_predictions_1d(mod_2, transform_x = unscale)
#### Example (8) Jitter points
# Jitter points horizontally for species
pretty_predictions_1d(mod_1, add_points_jitter = list(Species = c(0.1, 0)))
#### Example (9) Modify order of points/predictions
# Customise plot order across all predictors
pretty_predictions_1d(mod_1, add_order = c("points", "predictions"))
# Predictor-specific customisation
pretty_predictions_1d(mod_1,
add_order = list(Sepal.Width = c("predictions", "points"),
Species = c("points", "predictions")))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.