我试图在R中创建一个简单的包装函数。但它会引发错误并且我不明白为什么。
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
)
}
尝试运行时:
mlog_reg_p(y ~ a + b, d1)
它会抛出错误:
Error in stats::model.frame(formula = f, data = d): object 'f' not found
我搞砸了什么?