Search code examples
rdplyrregressionplyr

r `dlply` error handling when model fails


I am executing multiple models by subgroups using dlply

library(dplyr)
library(geepack)

data("mtcars")

mtcars <- mtcars[,c("mpg", "cyl", "hp")]

models = plyr::dlply(mtcars, "cyl", function(df) lm(mpg ~ hp,data=df))
lapply(models, summary)

There is a scenario where some models do not converge as a result fail. To make my point, I am modifying the code above so that it intentionally fails.

mtcars_test <- mtcars[,c("mpg", "cyl", "hp")]

mtcars_test <- mtcars_test %>% mutate(mpg = replace(mpg, which(cyl == 6 & mpg < 20), "9as34"))

models = plyr::dlply(mtcars_test, "cyl", function(df) geeglm(mpg ~ hp,data=df))
lapply(models, summary)

How can I force dlply to store a message "Failed to Converge" instead of crashing midway ?

Expecting results like this.

 > models
$`4`

Call:
lm(formula = mpg ~ hp, data = df)

Coefficients:
(Intercept)           hp  
    35.9830      -0.1128  


$`6`

"Failed to converge"


$`8`

Call:
lm(formula = mpg ~ hp, data = df)

Coefficients:
(Intercept)           hp  
   18.08007     -0.01424  

Here assuming a scenario that the model fails to converge on subset , cylinder = 6 Thanks ?


Solution

  • We can use tryCatch

    models <- plyr::dlply(mtcars_test, "cyl", function(df) 
       tryCatch(geeglm(mpg ~ hp,data=df), error = function(e) "Failed to converge"))