【问题标题】:how R recursive feature elimination with logistic regression如何使用逻辑回归消除 R 递归特征
【发布时间】:2017-03-23 18:52:56
【问题描述】:

其实也有类似的问答,但是对我不起作用。见下文。诀窍在于重写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 帮助中,据说参数 'functions' 可以与插入符号的训练函数 (caretFuncs) 一起使用。它的真正含义是什么? 有什么细节和例子吗?谢谢

【问题讨论】:

标签: r r-caret feature-selection


【解决方案1】:

我在自定义 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-05
    • 2014-01-19
    • 2021-06-10
    • 2014-08-06
    • 1970-01-01
    • 2019-05-29
    • 2016-01-25
    • 2018-01-04
    相关资源
    最近更新 更多