My aplogies if this is a duplicate question with a possible solution elsewhere, @I_O helped me figure out how to generate check_model
type of plots from a list
of lm
objects. Thankful to him for that.
library(patchwork)
library(performance)
library(ggplot)
data("mtcars")
head("mtcars")
the_models <-
split(mtcars, mtcars$cyl) |>
Map(f = \(d) lm(mpg ~ hp + wt, data = d))
the_plots <-
the_models |>
Map(f = \(m) check_model(m) |>
plot() +
## lay out diagnostic plots
## into 6 columns, 1 row per model:
plot_layout(6, 1)
)
the_plots |> Reduce(f = `/`)
This does what it is supposed to, but I am not sure how to add title on top of each row to indicate which model the plot represents ? I have tried several options , for example , inserting a line lab( ... =names(the_models )..)
, labs...names(the_models)[m]
, none of them worked. So I am not sure how to proceed.
Expecting a plot like this
Any suggestion is very helpful. Thanks.