Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

as.ggplot() whithin functions shows error #14

Open
chagas98 opened this issue Aug 23, 2022 · 1 comment
Open

as.ggplot() whithin functions shows error #14

chagas98 opened this issue Aug 23, 2022 · 1 comment

Comments

@chagas98
Copy link

chagas98 commented Aug 23, 2022

I wanted to code a simple function to check lm() and glm() models using the DHARMa package, which results in r-base plots. So I was using as.ggplot() to convert it into a ggplot object. This procedure worked well until I gathered a function like this:

check_models <- function(cm, modeltype) {

  if (modeltype == "lm"){
    
    qqres <-  ggplot(cm, aes(sample = rstudent(cm))) +
      geom_qq() + 
      stat_qq_line() +
      xlab("Theoretical Quantiles") +
      ylab("Studentized Residuals") +
      ggtitle("Q-Q Plot") +
      theme_bw()
    
    denres <- ggplot(cm) + 
      geom_density(aes(x = residuals(cm))) +
      xlab("Residuals") +
      ylab("Density") +
      ggtitle("Density") +
      theme_bw()
    return(qqres+denres)
    }

  if (modeltype == "glm"){

    cm_simres <- DHARMa::simulateResiduals(fittedModel = cm, n = 250)
    print(cm_simres)
    plot1 <- ggplotify::as.ggplot(~plot(cm_simres, asFactor = FALSE))
    
    cm_simres_refit <- DHARMa::simulateResiduals(fittedModel = cm,
                                                   n = 250,
                                                   refit = TRUE)
    plot2 <- ggplotify::as.ggplot(~DHARMa::testDispersion(cm))

    return(plot2) 
  }   

And got this error message:

Error in plot(cm_simres, asFactor = FALSE) : object 'cm_simres' not found

NOTE:

  • cm is a model
  • It showed the correct output in cm_simres when I rand the function with some lm() and "glm()
  • when I removed ~ from plot1, I got this error: Error in UseMethod("as.grob") :
    no applicable method for 'as.grob' applied to an object of class "NULL"

I opened this issue because it is probably a ggplotfy problem, I tried to run fix() to find bugs, but It was tricky.

Thanks in advance for any help.

@GuangchuangYu
Copy link
Owner

see #11 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants