Skip to contents

plot.model() and plot.multimodel() fit the model(s) in x and draw scatter plot(s) of actual response values versus fitted values in case of a continuous response. With binary response, a violin plot of the fitted values versus the response variable is produced (using geom_violin()).

plot.cv creates a similar plot, using the predictions resulting from cross-validation (generated with cv_predict) as fitted values.

Usage

# S3 method for model
plot(x, plot = TRUE, n_max = 5000, ...)

# S3 method for multimodel
plot(x, plot = TRUE, n_max = 5000, ...)

# S3 method for cv
plot(x, plot = TRUE, n_max = 5000, ...)

Arguments

x

Object of appropriate class.

plot

Logical: If TRUE, a ggplot is returned, if FALSE a data.frame. plot() first prepares a data.frame and then draws some ggplot using this data, with limited options for customization. If you want to design your own plot, you can set plot=FALSE, and use the data.frame returned by plot() to create your plot.

n_max

Integer: Maximal number of points to draw in a scatter plot. If size of data is larger than n_max, a random sample will be displayed.

...

Passed to geom_point() or geom_violin() (in case of binary response), respectively.

Value

These plot() methods return either a ggplot or, if plot=FALSE, a data.frame.

Details

The lengths of the errorbars corresponds to +/-1 standard error.

See also

Examples

# Simulate data 
set.seed(1)
n <- 50
x <- rnorm(n)
y <- 3*x + rnorm(n)
mymodel <- model(lm(y~x))
# Plot in-sample and out-of-sample predictions:
if (require(ggplot2) && require(gridExtra)){
  plot(gridExtra::arrangeGrob(
       plot(mymodel) + ggtitle("response vs. in-sample predictions"),
       plot(cv(mymodel)) + ggtitle("response vs. out-of-sample predictions"), 
       nrow = 1))
}   
#> Loading required package: ggplot2
#> Loading required package: gridExtra


# Binary response: binomial response
# Simulate data 
n <- 100
p <- 10
x <- matrix(rnorm(p*n), nrow = n)
y <- (0.1 * rowSums(x) + rnorm(n)) > 0
mymodel <- model(glm(y~x, family = binomial))
# Plot in-sample and out-of-sample predictions:
if (require(ggplot2) && require(gridExtra)){
  plot(gridExtra::arrangeGrob(
       plot(mymodel) + ggtitle("response vs. in-sample predictions"),
       plot(cv(mymodel)) + ggtitle("response vs. out-of-sample predictions"), 
       nrow = 1))
}