I want to execute logistic regression for each predictor seperately using glm. Total predictors are 50 and churn_flag is target variable and What I have done so far as:-
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)
})`
After running a function when I passed a variable name inside function as,
univariate_logisitic(A1)
it gives error as,
Error in univariate_logistic(A1) :
could not find function "univariate_logistic"
It seems that your function is not found in the current workspace, because you are defining the function inside of the lapply
context. It should work if you write the function, outside that block of code. This should give you an idea. Just as an advice and in order to improve readability, I always write the function as another entity; that way you can reuse your code. So your current code could be written as:
univariate_logistic <- function(var) {
cat("Current value: ", var , "\n")
}
lapply(c('A1','A2','A3','A4','A50'),
univariate_logistic
)
This prints the following output:
Current value: A1
Current value: A2
Current value: A3
Current value: A4
Current value: A50