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?