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:
- A basic explanation of how to pass these arguments as values (rather than just references, if that's what I'm currently doing)
- Any pitfalls in this approach, particularly when executed within a function.