我想使用glm分别对每个预测器执行逻辑回归。总预测变量是50,churn_flag是目标变量,我到目前为止做了什么: -
lapply(c('A1','A2','A3','A4',.................'A50'),
univariate_logistic <- function(var) {
formula <- as.formula(paste('churn_flag ~ ', var))
res.logist <- glm(formula, data = split_train, family = binomial)
summary(res.logist)
})`
当我在函数中传递变量名时运行函数,
univariate_logisitic(A1)
它给出了错误,
Error in univariate_logistic(A1) :
could not find function "univariate_logistic"
答案 0 :(得分:0)
似乎在当前工作空间中找不到您的函数,因为您正在lapply
上下文中定义函数。如果您在该代码块之外编写函数,它应该可以工作。这应该给你一个想法。作为一个建议,为了提高可读性,我总是把函数写成另一个实体;这样你可以重用你的代码。所以你当前的代码可以写成:
univariate_logistic <- function(var) {
cat("Current value: ", var , "\n")
}
lapply(c('A1','A2','A3','A4','A50'),
univariate_logistic
)
打印以下输出:
Current value: A1
Current value: A2
Current value: A3
Current value: A4
Current value: A50