Search code examples
rmodelsummary

Modelsummary gof_function to include AICc of a model


Recently I stumbled upon the modelsummary package, which seems to produce nice tables. However, I seem to be unable to edit the gof_function part of the modelsummary function.

Here is my example:

lm_a <- lm(Sepal.Length ~ Sepal.Width, data = iris)
lm_b <- lm(Sepal.Length ~ Sepal.Width + Petal.Length, data = iris)
lm_list <- list(a = lm_a, b = lm_b)

And then running modelsummary(lm_list) creates a neat table. However, I need the AICc to be reported, which is in the performance::model_performance function.

I tried the following:

# Create a function that tries to resemble the get_gof(model) dataframe. 
new_gof <- function(model){
  df_gof <- performance::model_performance(model)
  names(df_gof) <- tolower(names(df_gof))
  return(df_gof)
  }

# Run as gof_function
modelsummary(lm_list, gof_function = new_gof)

Which to me seems like the functions described in the Vignette's examples of the modelsummary package: https://modelsummary.com/vignettes/modelsummary.html#gof_function.

However, even the formula from the example does not add the 'DV' variable in the table. The only Goodness-of-Fit statistics that are reported are: Number of observations, R2, R2-adj, AIC, BIC, Log.Lik, F and RSME (see the dput below). These are consistent in both my functions or the example below:

fun <- function(model) data.frame("DV" = insight::find_response(model))
modelsummary(lm_list, gof_function = fun)

What am I doing wrong that the gof_function does not work?

N.B. new here at SO, if any relevant info is missing, let me know.

dput of my output modelsummary dataframe

structure(list(part = c("estimates", "estimates", "estimates", 
"estimates", "estimates", "estimates", "gof", "gof", "gof", "gof", 
"gof", "gof", "gof", "gof"), term = c("(Intercept)", "(Intercept)", 
"Sepal.Width", "Sepal.Width", "Petal.Length", "Petal.Length", 
"Num.Obs.", "R2", "R2 Adj.", "AIC", "BIC", "Log.Lik.", "F", "RMSE"
), statistic = c("estimate", "std.error", "estimate", "std.error", 
"estimate", "std.error", "", "", "", "", "", "", "", ""), a = c("6.526", 
"(0.479)", "-0.223", "(0.155)", "", "", "150", "0.014", "0.007", 
"372.0", "381.0", "-182.996", "2.074", "0.82"), b = c("2.249", 
"(0.248)", "0.596", "(0.069)", "0.472", "(0.017)", "150", "0.840", 
"0.838", "101.0", "113.1", "-46.513", "386.386", "0.33")), row.names = c(NA, 
-14L), class = "data.frame", align = c("c", "c", "c", "c", "c"
), hrule = 7L)

Solution

  • You are using version 1.4.5 of modelsummary, but the gof_function feature was only introduced in version 2.1.0, as documented in the NEWS file.

    Please update your package and restart R completely before trying again.

    install.packages("modelsummary")