1

I'm trying to make a simple wrapper function in R. However it throws and error and I don't understand why.

set.seed(98)
d1 <- data.frame(
          y = factor(rep(c("a","b","c","d"),25)), 
          a = rnorm(100), 
          b = rnorm(100)
)


mlog_reg_p = function(f, d, ...) {

    if(!(require("nnet"))) {
        stop("Package 'nnet' is not installed. install.packages('nnet')")
    }

    syx = match.call()
    su = summary(multinom(formula = f, data = d))
    z = su$coefficients / su$standard.errors

    p = (1 - pnorm(abs(z), 0, 1)) * 2

    list(
        syntax = syx,
        model_summary = su,
        p_values = p
    )
}

And when trying to run it:

mlog_reg_p(y ~ a + b, d1)

it throws an error:

Error in stats::model.frame(formula = f, data = d): object 'f' not found

What am I messing up?

hmnhmn
  • 173
  • 2
  • 9
  • This is one of these function scope type things... quick fix try changing the relevant line to `su = summary(eval(substitute(multinom(formula = f, data = d))))`. [relevant](http://stackoverflow.com/questions/10858318/r-pass-argument-to-glm-inside-an-r-function?answertab=votes#tab-top) – user20650 May 31 '15 at 15:26

0 Answers0