easystats / easystats/performance

r2_nakagawa breaking when used with purrr::map

Open
#145 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3 investigators :grey_question::question: Help us :eyes:
Dominant language
R
Stars
1.2k
Forks
109
Avg merge
6h 34m
Merged PRs (30d)
8

Description

I've found that r2_nakagawa breaks when used in a purrr::map context.

Here's a simple example where r2_nakagawa() is used with purrr::map. The same two models are fitted to each species in the Salamanders data set, and Nakagawa's R^2 is collected for each.

library(performance)
library(glmmTMB)
library(tidyverse)

mod_func <- function(df){
  
  form_list <- list()
  form_list[[1]] <- formula(count ~ mined + (1|site))
  form_list[[2]] <- formula(count ~ mined + DOY + (1|site))
  fam <- "poisson"
  
  r2_out <- NULL
  for (i in 1:length(form_list)){
    mod <- glmmTMB(form_list[[i]], data = df, family = fam)
    r2_out[[i]] <- r2_nakagawa(mod)
  }
  return(r2_out)
}

nested_mods <- 
  Salamanders %>% 
  group_by(spp) %>% 
  nest() %>% 
  mutate(model = map(data, mod_func))

nested_mods$model

The output gives conditional R^2 = 1 for each model

However, when models are specified individually, r2_nakagawa works as expected

# but in isolation r2_nakagawa works, even when formula is specified as a list
form_list <- list()
form_list[[1]] <- formula(count ~ mined + (1|site))
fam <- "poisson"
mod <- glmmTMB(form_list[[1]], data = Salamanders %>% 
                 dplyr::filter(spp == "GP"), family = fam)
r2_nakagawa(mod)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported purrr::map example with r2_nakagawa(), glmmTMB, and the Salamanders data, then compare its conditional R^2 results with the isolated model example. Trace the r2_nakagawa() entry point and identify why mapped models produce conditional R^2 = 1. Done means the mapped and individually specified models return consistent, expected R^2 values.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.