Giter VIP home page Giter VIP logo

Comments (5)

bwiernik avatar bwiernik commented on July 18, 2024 1

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.

strengejacke avatar strengejacke commented on July 18, 2024 1

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.

strengejacke avatar strengejacke commented on July 18, 2024 1

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.

abanihas avatar abanihas commented on July 18, 2024

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.

abanihas avatar abanihas commented on July 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.