RobustTurd
RobustTurd

Reputation: 93

"Evaluation error: object '.' not found." when applying function to models in a list column

I ran glm models for multiple groups with the dplyr "do" function. Now I want to apply the PseudoR2() function from the DescTool package to every model in the resulting list column. However, I get "Evaluation error: object '.' not found." apparently because the PseudoR2() wants to use the data used to generate the models, but the models have data = ., which does not exist in the environment.

The general question is, how to apply functions that need to access the original data to models that are generated using '.'?

Below is a reproducible example:

require(dplyr)
require(DescTools)

dta = tibble(id = c(rep("A", 4), rep("B", 4)),
             y = rnorm(8),
             x = rnorm(8))

dta %>% group_by(id) %>%
  do(fit = glm(y ~ x, data = .)) %>% 
  mutate(R2 = PseudoR2(fit))

Upvotes: 0

Views: 837

Answers (2)

RobustTurd
RobustTurd

Reputation: 93

This provides the desired outcome by calculating the McFadden's R2 directly, but does not solve the issue of applying functions when "data = .". Note that the problem in the PseudoR2() is the update() base function that wants to refit the model.

require(dplyr)
require(DescTools)

dta = tibble(id = c(rep("A", 4), rep("B", 4)),
             y = rnorm(8),
             x = rnorm(8))

dta %>% group_by(id) %>%
  do(fit = glm(y ~ x, data = .),
     fitNULL = glm(y ~ x, data = .)) %>% 
  mutate(PseudoR2 = 1 - logLik(fit)/logLik(fitNULL))

Upvotes: 0

akrun
akrun

Reputation: 887851

An option would be to build the model after nesting

library(tidyverse)
library(DescTools)
dta %>%
   group_by(id) %>% 
   nest %>% 
   mutate(data = map(data, ~ 
       .x %>% 
       mutate(fit = list(glm(y ~ x)), R2 = map(fit, PseudoR2)))) %>% 
  unnest %>%
  unnest(R2)
# A tibble: 8 x 5
#  id         y      x fit              R2
#  <chr>  <dbl>  <dbl> <list>        <dbl>
#1 A     -1.28   0.537 <S3: glm> 0.0000992
#2 A     -0.576 -0.508 <S3: glm> 0.0000992
#3 A     -0.635 -1.28  <S3: glm> 0.0000992
#4 A      0.324  0.272 <S3: glm> 0.0000992
#5 B     -0.329  0.290 <S3: glm> 0.0878   
#6 B     -1.13   0.432 <S3: glm> 0.0878   
#7 B     -1.42  -0.924 <S3: glm> 0.0878   
#8 B      0.551 -1.47  <S3: glm> 0.0878   

Upvotes: 1

Related Questions