首页 > 解决方案 > eval 中的错误(object$call$data):对象“。” 在 gls 模型对象上运行 Anova 时找不到

问题描述

我已成功在amelia使用以下代码 生成的估算数据集上运行 gls

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

现在,我想运行car::Anova以测试每个预测变量的重要性。为此,我使用了:

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

并收到以下错误消息:Error in eval(object$call$data) : object '.' not found

但是,如果我通过 单独运行每个估算数据集car::Anova,则效果很好(但我不想单独运行每个数据集)。如果我将以前的代码更改为该代码mod %>% mutate(gls.Anova = mod_gls %>% purrr::map(print(~anova(.))))
也可以正常工作。但是,我需要car::Anova获得 II 型 SS。我无法解释此错误消息。任何有关解释错误消息的帮助都会很棒。

标签: rpurrrdplyranovaleast-squares

解决方案


我得到了同样的错误(使用mtcars,见下文)。与模型的Error in eval(object$call$data) : object '.' not found估计有关,map并且源自data = .。例如,从 切换..x,即运行mod <- impute_data %>% mutate(gls_mod = data %>% purrr::map(~nlme::gls(resp_var ~ pred_var1 + pred_var2 + pred_var2), data = .x)))将错误消息更改为Error in eval(object$call$data) : object '.x' not found。换句话说car::Anova,就是寻找一个它找不到的data对象。.我很确定几个月前我遇到了这个问题,但我不记得我是如何解决的。):

一个简单的解决方案是使用“老式”for 循环来估计模型。使用作为示例数据,按公式mtcars拆分和使用。不如优雅,但它有效:cylmpg ~ hpmap

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

reprex 包于 2020-03-11 创建(v0.3.0)


推荐阅读