我正在尝试构建一个函数,该函数接受banktest(测试)和banktrain(训练)数据帧,并在调用时运行ctree,rpart,朴素贝叶斯和逻辑回归。 我的问题是关于“ x_model <-model(x_data_train,subscribed〜。,family =“ binomial”)”,它没有将给定的模型作为函数。我不确定如何做到这一点。
Model_diff_ <- function(model){
if (model == "glm"){
x_data_train$subscribed <- ifelse(x_data_train$subscribed == "yes", 1,0)
x_model <- model(x_data_train,subscribed~.,family = "binomial")
x_predction <- predict(x_model, banktest, type = "response")
x_predction <- ifelse(x_predction > 0.5, "yes", "no")
} else {
x_model <- model(subscribed~.,x_data_train)
x_predction <- predict(x_model, banktest, type = "class")
}
# Confusion matric
confusionMatrixx <- table(banktest$subscribed, x_predction,
dnn = c("Actual", "Prediction"))
df <- data.frame(banktest, Prediction = x_predction)
accuracy <- function(x){
accuracy <- (x[2,2] + x[1,1]) / sum(x)
}
recall <- function(x){
recall <- x[2,2] / (x[2,2] + x[1,2])
}
precision <- function(x){
precision <- x[2,2] / (x[2,2] + x[2,1])
}
Objects <- list("df" = df,
"accuracy" = accuracy(confusionMatrixx),
"recall" = recall(confusionMatrixx),
"precision"= precision(confusionMatrixx),
"model" = x_model)
return(Objects)
}
我希望它采用我在function()中输入的任何模型并采用其功能。