2

I am building models within a function, something along the lines of:

some_form <- formula("Species == 'virginica' ~  Petal.Width")
some_fam <- "binomial"
some_fit <- glm(formula = some_form, family = some_fam, data = iris )

When I call summary on the result, the function call includes the variable names.

summary(some_fit) # could also just use some_fit$call
# Returns
# Call:
#   glm(formula = some_form, family = some_fam, data = iris)

But I would love to have it return the original (evaluated) formula and family name. My desired output looks like:

# To produce desired output
summary(glm(Species=='virginica' ~ Petal.Width, family = binomial, data = iris))
# Call:
#   glm(formula = Species == "virginica" ~ Petal.Width, family = binomial, data = iris)
#   ^ This is what I want!

What I have tried:

some_fit <- glm(eval(some_form), family = eval(some_fam), data = iris )
summary(some_fit)
# Returns
# Call:
#   glm(formula = eval(some_form), family = eval(some_fam), data = iris)

Replacing the eval() with eval(quote()) or eval(substitute()) I get a similar result -- the $call just copies whatever expression I pass into it.

This might be a basic question (and by all means if it's a duplicate, please flag -- I probably didn't use the right search terms), but I would sincerely appreciate:

  1. A basic explanation of how to pass these arguments as values (rather than just references, if that's what I'm currently doing)
  2. Any pitfalls in this approach, particularly when executed within a function.
C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134
  • I'm sure there is a more "correct" way to do this, but `(some_fit <- do.call("glm", args = list(formula = some_form, family = some_fam, data = quote(iris))))`. – nrussell Jun 10 '16 at 14:10
  • 1
    @nrussell I should have thought of that -- a solid answer, if you wish to write it... thanks either way! – C8H10N4O2 Jun 10 '16 at 14:41
  • Thanks; but after writing that comment I came across [this question](http://stackoverflow.com/questions/17024685/how-to-use-a-character-string-in-formula) where Aaron suggests essentially the same approach with a much better explanation, so I'm going to propose this as a duplicate. – nrussell Jun 10 '16 at 14:45
  • @nrussell thanks. I figured there was a dupe, but my search terms were too high-level I guess – C8H10N4O2 Jun 10 '16 at 14:50

0 Answers0