2

I am comparing multiple models with modelsummary table. The models include generalized linear models, simple linear models, and generalized additive models. However, it would be very important for me to compare the R squared values and in the case of GLM which doesn't have R-squared, the McFadden’s R-Squared number which also gets values between 0 and 1. Is there a way to modify the R2 column of modelsummary table and add the McFadden's value manually? I tried to do some tricks with gof_map by changing the column values but didn't know how to add the modifications to the modelsummary table.

Is there a way to add values to modelsummary table or to modify the existing values?

user438383
  • 5,716
  • 8
  • 28
  • 43
eve1234
  • 21
  • 2

1 Answers1

1

The performance:::r2.glm method only return the Tjurr R2, so the following will work:

iris$foo = iris$Species == "virginica" 
m1 <- lm(foo ~ Sepal.Width, data = iris)
m2 <- lm(foo ~ Sepal.Width, data = iris, family  = binomial())
modelsummary(list(m1, m2), gof_map= c("r.squared", "r2.tjur"))

One hack to also get the Nagelkerke R2, is to write your own r2 function for your own model type:

# Add custom class to our model
class(m2) <- c("myglm", "glm", "lm")
r2.myglm <- function(model, ci = NULL, verbose = TRUE, ...) { 
  r2 <- # your calculation here
  list(R2_Nagelkerke = r2)
}
# we can then use
modelsummary(list(m1, m2), gof_map= c("r.squared", "r2.nagelkerke"))

You can use performance::r2.glm as the basis for your method.

Edit: the OP asked for McFadden's R-squared. In principle the solution above should also work in that case. gof_map has to be modified then

Jan van der Laan
  • 8,005
  • 1
  • 20
  • 35
  • Thank you! I did the above but now I'm wondering if it is fine to use the "R2 Nagelkerke" as the name for the McFadden's R-squared? Or should it be modified? In that case, I'm not sure ho to modify the name of a row in gof_map. – eve1234 Feb 20 '23 at 14:21
  • See https://vincentarelbundock.github.io/modelsummary/articles/modelsummary.html#gof_map. I think something like `gof_map = list(list(raw = "r2.nagelkerke", clean = "McFadden R2", fmt = "%f"))` should work. – Jan van der Laan Feb 20 '23 at 14:40
  • Oh thanks it worked! I indeed tried to look up this modelsummary page you linked, but only now I understood how the gof_map actually works :) – eve1234 Feb 20 '23 at 14:54