Search code examples
rpurrrdplyranovaleast-squares

Error in eval(object$call$data) : object '.' not found when running Anova on gls model object


I have successfully ran a gls on an imputed data set generated by amelia with the following codes

mod <- impute_data %>% mutate(gls_mod = data %>% purrr::map(~nlme::gls(resp_var ~ pred_var1 + pred_var2 + pred_var2), data = .)))

Now, I wanted to run car::Anova to test for the significance of each predictor variable. For this, I used:

Anova_mod <- mod  %>% mutate(gls.Anova = mod_gls %>% purrr::map(print(~car::Anova(.))))

and got the following error message:Error in eval(object$call$data) : object '.' not found

However, if I ran each imputed data set separately, via car::Anova, that works fine (But I do not want to run each data set separately). If I changed the previous code to mod %>% mutate(gls.Anova = mod_gls %>% purrr::map(print(~anova(.))))
that also works fine. But, I need car::Anova to get type II SS. I am unable to interpret this error message. Any help on interpreting the error message would be great.


Solution

  • I got the same error (using mtcars, see below). The Error in eval(object$call$data) : object '.' not found is related to the estimation of the models via map and stems from data = .. For example, switching from . to .x , i.e running mod <- impute_data %>% mutate(gls_mod = data %>% purrr::map(~nlme::gls(resp_var ~ pred_var1 + pred_var2 + pred_var2), data = .x))) changes the error message to Error in eval(object$call$data) : object '.x' not found. Put differently car::Anova is looking for a data object called . which it can not find. I'm quite sure that I had this problem some months ago but I can't remember how I solved it. ):

    A simple solution would be to use an "old-school" for-loop to estimate the models. Using mtcars as example data, splitting by cyl and using the formula mpg ~ hp. Not as elegant as map but it works:

    library(dplyr)
    
    df_list <- mtcars %>% 
      split(.$cyl)
    
    mod_gls_loop <- list()
    for (i in seq_along(df_list)) {
      mod_gls_loop[[i]] <- nlme::gls(mpg ~ hp, data = df_list[[i]])  
    }
    Anova_gls <- mod_gls_loop %>% purrr::map(~ print(car::Anova(.)))
    #> Analysis of Deviance Table (Type II tests)
    #> 
    #> Response: mpg
    #>    Df  Chisq Pr(>Chisq)  
    #> hp  1 3.3976    0.06529 .
    #> ---
    #> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    #> Analysis of Deviance Table (Type II tests)
    #> 
    #> Response: mpg
    #>    Df  Chisq Pr(>Chisq)
    #> hp  1 0.0821     0.7745
    #> Analysis of Deviance Table (Type II tests)
    #> 
    #> Response: mpg
    #>    Df  Chisq Pr(>Chisq)
    #> hp  1 1.0498     0.3055
    

    Created on 2020-03-11 by the reprex package (v0.3.0)