有什么方法可以 select 基于 R 中的 wilcoxon 检验的单变量特征?
any way to select univariate features based on wilcoxon test in R?
我打算使用 care::sbf
进行单变量特征选择,其中我的输入是具有多个变量(a.k.a、其列)、候选特征列表和标签(a.k.a、分类变量)。在阅读 caret
包文档后,我尝试使用 sbf
、sbfController
进行功能选择,但我 运行 出现以下错误:
Error in contrasts<-
(*tmp*
, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
谁能告诉我如何解决这个错误?使用 caret::sbf
进行特征选择的正确方法是什么?有什么想法吗?
可重现的例子:
这里是 reproducible example on public gist 我用它作为输入的地方。
我目前的尝试:
library(caret)
library(e1071)
library(randomForest)
df=read.csv("df.csv", header=True)
sbfCtrl <- sbfControl(method = 'cv', number = 10, returnResamp = 'final', functions = caretFuncs, saveDetails = TRUE)
model <- sbf(form= ventil_status~ .,
data= df,
methods='knn',
trControl=trainControl(method = 'cv', classProbs = TRUE),
tuneGrid=data.frame(k=1:10),
sbfControl=sbfControl(functions = sbfCtrl,
methods='repeatedcv', number = 10, repeats = 10))
print(model)
print(model$fit$results)
> model <- sbf(ventil_status~ ., data=df, sizes=c(1,5,10,20),
+ method= 'knn', trControl=trainControl(method = 'cv', classProbs = TRUE),
+ tuneGrid = data.frame(k=1:10),
+ sbfControl=sbfCtrl)
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
我用谷歌搜索了这个错误,但仍然无法解决。有什么想法可以让上面的代码工作吗?使用 caret::sbf
进行过滤器选择的正确方法是什么?
我想要的是输出数据框必须具有附加了 p 值的选定特征。所以这是我的尝试:
newdf <- df[ , -which(names(df) %in% c("subject"))]
p_value_vector <- sapply(names(newdf), function(i)
tryCatch(
wilcox.test(newdf[newdf$ventil_status %in% "0", i],
newdf[newdf$ventil_status %in% "1", i],
na.action(na.omit))$p.value),
warning = function(w) return(NA),
error = function (e) return(NA)
)
预期输出:
我期待具有选定特征的输出数据帧,其中 wilcox.test
返回的 p 值应附加到相应的特征。任何想法让这发生在 r 中?如何使用 caret::sbf
正确操作特征选择?任何想法?
这是我的 R 会话信息:
> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggpubr_0.2.5 magrittr_1.5 reshape2_1.4.3
[4] forcats_0.5.0 purrr_0.3.3 readr_1.3.1
[7] tibble_2.1.3 tidyverse_1.3.0 stringr_1.4.0
[10] dplyr_0.8.5 scales_1.1.0 tidyr_1.0.2
[13] aws.s3_0.3.20 randomForest_4.6-14 e1071_1.7-3
[16] mlbench_2.1-1 caret_6.0-86 ggplot2_3.3.0
[19] lattice_0.20-38
要使用 sbf,您可以使用 caretSBF,然后根据需要添加分数和过滤器:
library(mlbench)
library(caret)
knnSBF = caretSBF
knnSBF$summary <- twoClassSummary
knnSBF$score <- function(x, y) {
wilcox.test(x ~ y)$p.value
}
knnSBF$filter <- function(score, x, y) {
score <= 0.05
}
然后定义训练参数和sbf参数:
sbfCtrl <- sbfControl(method = "cv",number = 3,
functions = knnSBF,saveDetails = TRUE)
trn_grid <- expand.grid(k=c(2,6,10))
trCtrl <- trainControl(method = "cv",number = 3,
classProbs = TRUE,verboseIter = TRUE)
然后运行火车:
data(Sonar)
y = Sonar$Class
x = Sonar[,-ncol(Sonar)]
set.seed(111)
model1 <- sbf(x,y,trControl = trCtrl,
sbfControl = sbfCtrl,
method = "knn",
tuneGrid = trn_grid)
model1$variables
$selectedVars
[1] "V1" "V2" "V3" "V4" "V5" "V6" "V8" "V9" "V10" "V11" "V12" "V13"
[13] "V14" "V20" "V21" "V22" "V36" "V37" "V42" "V43" "V44" "V45" "V46" "V47"
[25] "V48" "V49" "V50" "V51" "V52" "V54" "V58"
$selectedVars
[1] "V4" "V5" "V6" "V9" "V10" "V11" "V12" "V13" "V14" "V20" "V21" "V22"
[13] "V28" "V31" "V34" "V35" "V36" "V37" "V43" "V44" "V45" "V46" "V47" "V48"
[25] "V49" "V51" "V52"
$selectedVars
[1] "V1" "V2" "V3" "V4" "V5" "V6" "V7" "V8" "V9" "V10" "V11" "V12"
[13] "V13" "V14" "V21" "V22" "V23" "V34" "V35" "V36" "V37" "V43" "V44" "V45"
[25] "V46" "V47" "V48" "V49" "V50" "V51" "V52" "V53" "V56" "V58"
我不认为他们 return 你的 p 值,尽管我可能是错的。对于计算 p 值的函数,使用上面的示例
p_value_vector <- apply(x,2,function(i)wilcox.test(i~y)$p.value)
我打算使用 care::sbf
进行单变量特征选择,其中我的输入是具有多个变量(a.k.a、其列)、候选特征列表和标签(a.k.a、分类变量)。在阅读 caret
包文档后,我尝试使用 sbf
、sbfController
进行功能选择,但我 运行 出现以下错误:
Error in
contrasts<-
(*tmp*
, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
谁能告诉我如何解决这个错误?使用 caret::sbf
进行特征选择的正确方法是什么?有什么想法吗?
可重现的例子:
这里是 reproducible example on public gist 我用它作为输入的地方。
我目前的尝试:
library(caret)
library(e1071)
library(randomForest)
df=read.csv("df.csv", header=True)
sbfCtrl <- sbfControl(method = 'cv', number = 10, returnResamp = 'final', functions = caretFuncs, saveDetails = TRUE)
model <- sbf(form= ventil_status~ .,
data= df,
methods='knn',
trControl=trainControl(method = 'cv', classProbs = TRUE),
tuneGrid=data.frame(k=1:10),
sbfControl=sbfControl(functions = sbfCtrl,
methods='repeatedcv', number = 10, repeats = 10))
print(model)
print(model$fit$results)
> model <- sbf(ventil_status~ ., data=df, sizes=c(1,5,10,20),
+ method= 'knn', trControl=trainControl(method = 'cv', classProbs = TRUE),
+ tuneGrid = data.frame(k=1:10),
+ sbfControl=sbfCtrl)
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
我用谷歌搜索了这个错误,但仍然无法解决。有什么想法可以让上面的代码工作吗?使用 caret::sbf
进行过滤器选择的正确方法是什么?
我想要的是输出数据框必须具有附加了 p 值的选定特征。所以这是我的尝试:
newdf <- df[ , -which(names(df) %in% c("subject"))]
p_value_vector <- sapply(names(newdf), function(i)
tryCatch(
wilcox.test(newdf[newdf$ventil_status %in% "0", i],
newdf[newdf$ventil_status %in% "1", i],
na.action(na.omit))$p.value),
warning = function(w) return(NA),
error = function (e) return(NA)
)
预期输出:
我期待具有选定特征的输出数据帧,其中 wilcox.test
返回的 p 值应附加到相应的特征。任何想法让这发生在 r 中?如何使用 caret::sbf
正确操作特征选择?任何想法?
这是我的 R 会话信息:
> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggpubr_0.2.5 magrittr_1.5 reshape2_1.4.3
[4] forcats_0.5.0 purrr_0.3.3 readr_1.3.1
[7] tibble_2.1.3 tidyverse_1.3.0 stringr_1.4.0
[10] dplyr_0.8.5 scales_1.1.0 tidyr_1.0.2
[13] aws.s3_0.3.20 randomForest_4.6-14 e1071_1.7-3
[16] mlbench_2.1-1 caret_6.0-86 ggplot2_3.3.0
[19] lattice_0.20-38
要使用 sbf,您可以使用 caretSBF,然后根据需要添加分数和过滤器:
library(mlbench)
library(caret)
knnSBF = caretSBF
knnSBF$summary <- twoClassSummary
knnSBF$score <- function(x, y) {
wilcox.test(x ~ y)$p.value
}
knnSBF$filter <- function(score, x, y) {
score <= 0.05
}
然后定义训练参数和sbf参数:
sbfCtrl <- sbfControl(method = "cv",number = 3,
functions = knnSBF,saveDetails = TRUE)
trn_grid <- expand.grid(k=c(2,6,10))
trCtrl <- trainControl(method = "cv",number = 3,
classProbs = TRUE,verboseIter = TRUE)
然后运行火车:
data(Sonar)
y = Sonar$Class
x = Sonar[,-ncol(Sonar)]
set.seed(111)
model1 <- sbf(x,y,trControl = trCtrl,
sbfControl = sbfCtrl,
method = "knn",
tuneGrid = trn_grid)
model1$variables
$selectedVars
[1] "V1" "V2" "V3" "V4" "V5" "V6" "V8" "V9" "V10" "V11" "V12" "V13"
[13] "V14" "V20" "V21" "V22" "V36" "V37" "V42" "V43" "V44" "V45" "V46" "V47"
[25] "V48" "V49" "V50" "V51" "V52" "V54" "V58"
$selectedVars
[1] "V4" "V5" "V6" "V9" "V10" "V11" "V12" "V13" "V14" "V20" "V21" "V22"
[13] "V28" "V31" "V34" "V35" "V36" "V37" "V43" "V44" "V45" "V46" "V47" "V48"
[25] "V49" "V51" "V52"
$selectedVars
[1] "V1" "V2" "V3" "V4" "V5" "V6" "V7" "V8" "V9" "V10" "V11" "V12"
[13] "V13" "V14" "V21" "V22" "V23" "V34" "V35" "V36" "V37" "V43" "V44" "V45"
[25] "V46" "V47" "V48" "V49" "V50" "V51" "V52" "V53" "V56" "V58"
我不认为他们 return 你的 p 值,尽管我可能是错的。对于计算 p 值的函数,使用上面的示例
p_value_vector <- apply(x,2,function(i)wilcox.test(i~y)$p.value)