I'm using the check_model function within the performance library in r to check the assumptions of a simple linear regression model using lm.
I am fitting 3 models on different subsets of the data.
data("mtcars")
head("mtcars")
table(mtcars$cyl, useNA = "ifany")
foo <- mtcars %>%
group_by(cyl) %>%
nest() %>%
mutate(model= map(data, lm(mpg ~ hp + wt, data = .))
foo %>%
{map(.$model, summary)}
I want check_model to plot all the 6 diagnostic plots per model in one row. Which means 3 rows, 6 plots in each row, using the model summaries which is stored as a list object.
check_model(foo$model..cylinder 4)...all 6 plots in 1 row
check_model(foo$model..cylinder 6)...all 6 plots in 1 row
check_model(foo$model..cylinder 8)...all 6 plots in 1 row
Any suggestions for how to accomplish this is much appreciated. Thanks.
one approach:
cyl(here: byspliting andMapping):plotting the result ofcheck_modelhave, besides being ggplot-objects, classpatchwork, meaning that you can specify theplot_layout(a function provided by package {patchwork}):Reduce) the list of plots into one stack. With {patchwork}, you stack plots with/:edit The output for a larger number of models (= rows) may exceed the device dimensions (
Error: The viewport may be too small to show this patchwork. Please make it larger.) If you don't need live preview, you can assign the combined plots to an object and save it like so:(
ggsavecan save in various raster and vector formats, guessing from the filename's extension.)