6

I was running a bestglm analysis from the package bestglm:

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

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

and I started getting this error:

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

I inspected my model data and it looks good -- the right classes, no missingness, variables I've used before in this same way with no problem.

After unsucessfully troubleshooting and web searching the error, I tried a regular glm with the data and started getting a nearly identical error:

> 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"

I'm not sure how to make a reproducible example for this problem. Here's the structure of my data:

> 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 ...

This problem persists regardless of my model specification and if I remove columns from the dataframes, so I do not think that it's a data issue.

> 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)

At the suggestion of @BenBolker, I tried some debugging options and got a little more info:

> 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"))
})()

I also tried:

rm(res.bestglm) # Because of a message you can see in the comments
install.packages("bestglm")
require(bestglm)
Hack-R
  • 22,422
  • 14
  • 75
  • 131
  • 1
    try starting from a clean (`--vanilla`) session? you probably have something like an object called `function` that's causing trouble – Ben Bolker Dec 31 '14 at 17:03
  • @BondedDust: sure it is. `d <- data.frame(y=c(1,1,0)); glm(y~1,data=d,family=binomial(link="probit"))` (the link function only needs to be monotonic, continuous, and differentiable, I think ...) – Ben Bolker Dec 31 '14 at 17:05
  • @BondedDust True -- one is a probit, one isn't. `bestglm` does not support probits or links. The families were `binomial`. – Hack-R Dec 31 '14 at 17:05
  • another guess is that you might have overwritten `binomial`? what are the results of `traceback()` ? – Ben Bolker Dec 31 '14 at 17:07
  • @BenBolker Trying that now, will take a couple minutes due to big data, thanks – Hack-R Dec 31 '14 at 17:07
  • @BondedDest I'll add the traceback, good idea – Hack-R Dec 31 '14 at 17:09
  • have you checked http://stackoverflow.com/questions/4442518/general-suggestions-for-debugging-r ? – Ben Bolker Dec 31 '14 at 17:20
  • I think I just got a big clue as to what the problem is. I started a vanilla session then loaded a `.Rdata` file and got this message: `Warning: namespace ‘bestglm’ is not available and has been replaced by .GlobalEnv when processing object ‘res.bestglm’` – Hack-R Dec 31 '14 at 17:59
  • @BenBolker that's a good link and I'm trying to debug with the suggestions but so far still no luck. – Hack-R Dec 31 '14 at 18:07
  • `?model.frame.default` tells me that `drop.unused.levels` defaults to `FALSE` but appears `TRUE` in my errors... it shouldn't matter since these are numeric not factor, but maybe it's a clue... – Hack-R Dec 31 '14 at 18:27

1 Answers1

3

Many thanks to the folks in the comments.

I'm not really sure what caused this error, but here's what fixed it:

  1. Removed a problematic object, res.bestglm
  2. Re-installed bestglm
  3. Saved image and closed the project
  4. Re-opened the project and loaded packages

I'm not sure why the object in #1 was problematic, but it was somehow cluttering the Global Environment. Moreover, R could not find bestglm after I launched a vanilla session without removing the object first and re-installing the package.

Hack-R
  • 22,422
  • 14
  • 75
  • 131