Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

glm

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)
like image 605
Hack-R Avatar asked Dec 31 '14 16:12

Hack-R


1 Answers

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.

like image 55
Hack-R Avatar answered Nov 03 '22 08:11

Hack-R