如何使用逻辑回归消除 R 递归特征



事实上,有一个类似的问题和答案,但它对我不起作用。诀窍在于重写lmFunc的契合度。

"{ 中的错误:任务 1 失败 - "结果长度不相等",许多警告:glm.fit:发生了数字 0 或 1 的拟合概率">

故障在哪里?

lmFuncs$fit=function (x, y, first, last, ...) 
{
  tmp <- as.data.frame(x) 
  tmp$y <- y
  glm(y ~ ., data = tmp, family=binomial(link='logit'))
}
ctrl <- rfeControl(functions = lmFuncs,method = 'cv',number=10)
fit.rfe=rfe(df.preds,df.depend, rfeControl=ctrl)

在 rfeControl 帮助中,据说参数"函数"可以与插入符号的训练函数 (caretFuncs( 一起使用。这到底是什么意思?任何细节和示例?谢谢

我在自定义 lmFunc 时遇到了类似的问题。

对于逻辑回归,请确保使用 lrFuncs 并将大小设置为等于预测变量的数量。这不会导致任何问题。

示例(仅用于功能目的(

library(caret)
#Reproducible data
set.seed(1) 
x <- data.frame(runif(10),runif(10),runif(10),runif(10),runif(10),runif(10),runif(10),runif(10),runif(10),runif(10),runif(10),runif(10),runif(10),runif(10),runif(10),runif(10),runif(10),runif(10),runif(10),runif(10),runif(10),runif(10),runif(10),runif(10),runif(10),runif(10),runif(10),runif(10))
x$dpen <- sample(c(0,1), replace=TRUE, size=10)
x$dpen <- factor(x$dpen)
#Spliting training set into two parts based on outcome: 80% and 20%
index <- createDataPartition(x$dpen, p=0.8, list=FALSE)
trainSet <- x[ index,]
testSet <- x[-index,]
control <- rfeControl(functions = lrFuncs,
                   method = "cv", #cross validation
                   verbose = FALSE, #prevents copious amounts of output from being produced.
                   )
##RFE
rfe(trainSet[,1:28] #predictor varia, 
    trainSet[,9], 
    sizes = c(1:28) #size of predictor variables, 
    rfeControl = control)

最新更新