R:找不到函数“函数(对象,...\nObject“在 glm() 和 bestglm() 中

R: could not find function "function (object, ...) \nobject" in glm() and bestglm()

提问人:Hack-R 提问时间:1/1/2015 最后编辑:Hack-R 更新时间:1/1/2015 访问量:7298

问:

我正在从包中运行分析:bestglmbestglm

res.bestglm <-
  bestglm(Xy = offermodel,
          family = binomial,
          IC = "BIC",
          method = "exhaustive")

summary(res.bestglm$BestModel) 
Morgan-Tatar search since family is non-gaussian.

我开始收到这个错误:

Error in model.frame.default(formula = y ~ 1, weights = weights, drop.unused.levels = TRUE) : 
  could not find function "function (object, ...) \nobject"

我检查了我的模型数据,它看起来不错——正确的类,没有缺失,我以前以同样的方式使用过的变量没有问题。

在对错误进行故障排除和网络搜索失败后,我尝试了常规数据,并开始收到几乎相同的错误:glm

> glm.mba <- glm(y ~  female + DNC + SE_region + enr_before_offer + FA,
+                family = binomial(link = "probit"), data = offermodel)
Error in model.frame.default(formula = y ~ female + DNC + SE_region +  : 
  could not find function "function (object, ...) \nobject"

我不确定如何为这个问题制作一个可重现的例子。这是我的数据结构:

> str(offermodel)
'data.frame':   2559 obs. of  10 variables:
 $ online_chan     : num  1 1 1 1 1 1 1 1 1 1 ...
 $ SE_region       : num  1 0 0 0 1 0 0 0 0 0 ...
 $ recruited       : num  0 0 0 0 0 0 0 0 0 0 ...
 $ referral        : num  0 0 0 0 0 0 0 0 0 0 ...
 $ FA              : num  1 1 0 0 1 1 1 1 1 1 ...
 $ female          : num  0 0 0 0 0 0 0 0 0 0 ...
 $ enr_before_offer: num  0 0 0 0 0 0 0 0 0 0 ...
 $ reg_lag_high    : num  0 0 0 0 0 0 0 0 0 0 ...
 $ DNC             : num  0 0 0 0 1 0 0 0 0 0 ...
 $ y               : num  1 1 1 0 0 1 1 1 1 1 ...

无论我的模型规范如何,以及我是否从数据帧中删除列,这个问题仍然存在,因此我不认为这是数据问题。

> traceback()
5: model.frame.default(formula = y ~ female + DNC + SE_region + 
       enr_before_offer + FA, data = offermodel, drop.unused.levels = TRUE)
4: stats::model.frame(formula = y ~ female + DNC + SE_region + enr_before_offer + 
       FA, data = offermodel, drop.unused.levels = TRUE)
3: eval(expr, envir, enclos)
2: eval(mf, parent.frame())
1: glm(y ~ female + DNC + SE_region + enr_before_offer + FA, family = binomial(link = "probit"), 
       data = offermodel)

在@BenBolker的建议下,我尝试了一些调试选项,并得到了更多信息:

> glm.mba <- glm(y ~  female + DNC + SE_region + enr_before_offer + FA, data = offermodel)
Error in model.frame.default(formula = y ~ female + DNC + SE_region +  : 
  could not find function "function (object, ...) \nobject"

Enter a frame number, or 0 to exit   

1: glm(y ~ female + DNC + SE_region + enr_before_offer + FA, data = offermodel)
2: eval(mf, parent.frame())
3: eval(expr, envir, enclos)
4: stats::model.frame(formula = y ~ female + DNC + SE_region + enr_before_offer + FA, data = offermodel, drop.unu
5: model.frame.default(formula = y ~ female + DNC + SE_region + enr_before_offer + FA, data = offermodel, drop.un

Selection: 5
Called from: (function () 
{
    if (.isMethodsDispatchOn()) {
        tState <- tracingState(FALSE)
        on.exit(tracingState(tState))
    }
    calls <- sys.calls()
    from <- 0L
    n <- length(calls)
    if (identical(sys.function(n), recover)) 
        n <- n - 1L
    for (i in rev(seq_len(n))) {
        calli <- calls[[i]]
        fname <- calli[[1L]]
        if (!is.na(match(deparse(fname)[1L], c("methods::.doTrace", 
            ".doTrace")))) {
            from <- i - 1L
            break
        }
    }
    if (from == 0L) 
        for (i in rev(seq_len(n))) {
            calli <- calls[[i]]
            fname <- calli[[1L]]
            if (!is.name(fname) || is.na(match(as.character(fname), 
                c("recover", "stop", "Stop")))) {
                from <- i
                break
            }
        }
    if (from > 0L) {
        if (!interactive()) {
            try(dump.frames())
            cat(gettext("recover called non-interactively; frames dumped, use debugger() to view\n"))
            return(NULL)
        }
        else if (identical(getOption("show.error.messages"), 
            FALSE)) 
            return(NULL)
        calls <- limitedLabels(calls[1L:from])
        repeat {
            which <- menu(calls, title = "\nEnter a frame number, or 0 to exit  ")
            if (which) 
                eval(substitute(browser(skipCalls = skip), list(skip = 7 - 
                  which)), envir = sys.frame(which))
            else break
        }
    }
    else cat(gettext("No suitable frames for recover()\n"))
})()

我也试过:

rm(res.bestglm) # Because of a message you can see in the comments
install.packages("bestglm")
require(bestglm)
R GLM系列

评论

1赞 Ben Bolker 1/1/2015
尝试从干净 () 会话开始?你可能有一个类似物体的东西,叫做它正在引起麻烦--vanillafunction
0赞 Ben Bolker 1/1/2015
@BondedDust:当然可以。 (链接函数只需要是单调的、连续的、可微的,我认为......d <- data.frame(y=c(1,1,0)); glm(y~1,data=d,family=binomial(link="probit"))
0赞 Hack-R 1/1/2015
@BondedDust 没错,一个是概率,一个不是。 不支持概率或链接。这些家庭是.bestglmbinomial
0赞 Ben Bolker 1/1/2015
另一个猜测是你可能已经覆盖了?结果是什么?binomialtraceback()
0赞 Hack-R 1/1/2015
@BenBolker 现在尝试,由于大数据,需要几分钟,谢谢

答:

3赞 Hack-R 1/1/2015 #1

非常感谢评论中的人。

我不太确定是什么导致了这个错误,但这是修复它的方法:

  1. 删除了一个有问题的对象,res.bestglm
  2. 重新安装bestglm
  3. 保存图像并关闭项目
  4. 重新打开项目并加载包

我不确定为什么 #1 中的对象有问题,但它在某种程度上使全球环境变得混乱。此外,在我启动原版会话后,R 找不到,而没有先删除对象并重新安装包。bestglm

评论

0赞 Heisenberg 3/10/2015
重新启动 R 会话也对我有用,即使没有。rm(list=ls())
1赞 Matt Bannert 5/31/2015
遇到同样的问题。 随后的会话重启足以让我解决症状。还不知道是什么原因导致了这个问题。在传递全局环境中也存在的公式时,确实会引起混淆。rm(list=ls())
0赞 Carlos Cinelli 5/18/2017
您是否在使用 RStudio?
2赞 Carlos Cinelli 5/20/2017
@Hack还不确定,但我怀疑这是 RStudio 的错误。
1赞 coletl 2/8/2020
我只使用时遇到了同样的错误。退出 RStudio 对我有用。stats::lm()