Comments (5)
I would recommend using ggsave()
over pdf()
.
To add a title:
perf_plot <- performance::check_model(mod) |>
plot()
perf_plot +
patchwork::plot_annotation(
title = "Title"
)
from performance.
check_model()
returns an arranged grid of single ggplot-objects, using patchwork. However, you could also return the single plots, to get a list of ggplot-plots, and arrange them manually using see::plots()
. see::plots()
has some arguments to tweak the plot, including adding a title. If you're familiar with the patchwork package, Brenton's suggestion should be easier and faster, though.
Here are some examples:
library(performance)
data(iris)
m1 <- lm(Sepal.Width ~ Species + Petal.Length + Petal.Width, data = iris)
p <- plot(check_model(m1, panel = FALSE))
see::plots(p, title = "My title", subtitle = "My subtitle")
see::plots(p, title = "My title", subtitle = "My subtitle", n_colum = 2, tags = "A")
see::plots(p, title = "My title", caption = "Small caption at the bottom")
Created on 2023-11-23 with reprex v2.0.2
from performance.
In a similar fashion, you can edit the single plots before re-arranging them again, e.g. changing the axis titles, as you resquested:
library(performance)
data(iris)
m1 <- lm(Sepal.Width ~ Species + Petal.Length + Petal.Width, data = iris)
p <- plot(check_model(m1, panel = FALSE))
p[[1]] <- p[[1]] + ggplot2::xlab("New Outcome Name")
see::plots(p)
Created on 2023-11-23 with reprex v2.0.2
from performance.
Okay, saving was easy! Sorry I didn't figure it out. Can a title be added?
pdf(file="plot1.pdf", width=10, height=10)
performance::check_model(m1$finalModel)
dev.off()
from performance.
Ah! I see were my problem was. I had to feed the output of check_model()
into plot()
for it to be a ggplot. Before doing so is.ggplot() was FALSE.
Thanks!
The last question I asked seemed obvious when I zoomed in on the saved plot. The key for pp_check lists line thickness and colour separately in 4 keys but in the plot these are used together, so model predicted data is with thin blue lines and observed data with a thick green line.
Here's the complete solution I had in mind for future reference:
# Create multiple models
# Function to iteratively add values to a string
# Source: https://purrr.tidyverse.org/reference/reduce.html
paste2 <- function(x, y, sep = " + ") paste(x, y, sep = sep)
# Iteratively add predictors to a formula
# Source: https://stackoverflow.com/questions/61419718/iteration-with-purrrreduce
model_formulas <- Reduce(f = paste2, x = mtcars[4:9] |> colnames(), init = "mpg ~ cyl + disp", accumulate = TRUE)
# Save models to a list
model_object_list <- lapply(X = model_formulas,
FUN = \(x) caret::train(
formula(x),
data = mtcars,
method = "lm"
# Name models with their formula
)) |> setNames(model_formulas)
#> Loading required package: ggplot2
#> Loading required package: lattice
# Plot making function
plot_func <- function(x, y) {
p <- performance::check_model(x$finalModel, panel = FALSE) |>
plot() |>
# Add subtitle
see::plots(subtitle = y)
# Add response variable from model formula as x axis label of pp_check plot
p[[1]] <- p[[1]] + ggplot2::xlab(sub(" ~.*", "", y))
return(p)
}
plot_list <- purrr::map2(
.f = plot_func,
.x = model_object_list,
# Add model formula to each plot as subtitle
.y = names(model_object_list)) |>
setNames(model_formulas)
# Save each plot with a modified version of model formula name, make output silent
invisible(
plot_list |>
purrr::map2(.y = model_formulas, .f = function(x = .x, y = .y)
ggplot2::ggsave(plot = x, filename = paste0(y |> janitor::make_clean_names(), ".png"), height=10, width=20))
)
Created on 2023-11-23 with reprex v2.0.2
from performance.
Related Issues (20)
- check_model error suggestions are not complete HOT 5
- Error and Incomplete Output Using performance::check_collinearity with Cox Models HOT 1
- Normality of Residuals of check_model is abnormal. HOT 2
- Revise compare_models() for Bayesian models HOT 5
- R-squared for glmmTMB (binomial) HOT 9
- check_model() bugged for lmer models *only* when run as part of an RMD chunk HOT 3
- check_predictions() fails when outcome is log-transformed and named like a valid function HOT 2
- Error in `check_model(<glmer>)` HOT 3
- Problems using `r2_nakagawa()` HOT 3
- check_model fails if dependent variable is labelled HOT 5
- Remove unnecessary `tryCatch()` statements targeting `insight::download_model()` HOT 2
- check_collinearity() does not work with orthogonal polynomials HOT 10
- Should check_overdispersion give warning when applied to quasipoisson? HOT 1
- Check for influential observations of GLM w/o numeric variables
- `check_model` not working for `brms::brm` with `Beta()` family HOT 3
- Comparing frequentist and Bayesian models
- 95 CI for rmse HOT 9
- ICC for `beta_family` not accurate HOT 25
- `check_zeroinflation()` not working with glmmTMB? HOT 2
- Confidence intervals for ICC, it seems that none of the `ci_method`s work (neither default, `boot` or `analytical`). HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from performance.