2016-01-20 197 views
0

我正在使用R和插入符号包进行分类任务。为了消除特征,我使用了rfe,它具有不同的选项,其中,我想要最大化/最小化的度量标准是什么。R rfe特征选择插入符号

问题是,rfe接受诸如RMSE,kappa等度量标准,并且我希望使用不同的度量标准来最大化,但在我想从度量标准库中最大化ScoreQuadraticWeightedKappa的情况下,但我不知道该怎么做那。

我有以下代码:

control <- rfeControl(functions = rfFuncs, method="cv", number=2) 
results <- rfe(dataset[, -59], dataset[, 59], 
       sizes = c(1:58), rfeControl = control) 

如何修改它,因为RFE最大化ScoreQuadraticWeightedKappa?

回答

0

您需要修改postResample函数,或创建自己的类似函数,然后将其插入到rfFuncs$summary中。这里下面的默认postResample功能:

> postResample 
function (pred, obs) 
{ 
    isNA <- is.na(pred) 
    pred <- pred[!isNA] 
    obs <- obs[!isNA] 
    if (!is.factor(obs) & is.numeric(obs)) { 
     if (length(obs) + length(pred) == 0) { 
      out <- rep(NA, 2) 
     } 
     else { 
      if (length(unique(pred)) < 2 || length(unique(obs)) < 
       2) { 
       resamplCor <- NA 
      } 
      else { 
       resamplCor <- try(cor(pred, obs, use = "pairwise.complete.obs"), 
        silent = TRUE) 
       if (class(resamplCor) == "try-error") 
        resamplCor <- NA 
      } 
      mse <- mean((pred - obs)^2) 
      n <- length(obs) 
      out <- c(sqrt(mse), resamplCor^2) 
     } 
     names(out) <- c("RMSE", "Rsquared") 
    } 
    else { 
     if (length(obs) + length(pred) == 0) { 
      out <- rep(NA, 2) 
     } 
     else { 
      pred <- factor(pred, levels = levels(obs)) 
      requireNamespaceQuietStop("e1071") 
      out <- unlist(e1071::classAgreement(table(obs, pred)))[c("diag", 
       "kappa")] 
     } 
     names(out) <- c("Accuracy", "Kappa") 
    } 
    if (any(is.nan(out))) 
     out[is.nan(out)] <- NA 
    out 
} 

更具体地说,因为你正在做分类,你将需要修改说的postResample部分:

else { 
     if (length(obs) + length(pred) == 0) { 
      out <- rep(NA, 2) 
     } 
     else { 
      pred <- factor(pred, levels = levels(obs)) 
      requireNamespaceQuietStop("e1071") 
      out <- unlist(e1071::classAgreement(table(obs, pred)))[c("diag", 
            "kappa")] 
     } 
     names(out) <- c("Accuracy", "Kappa") 
    } 

你编辑postResample后,或创建您自己的等效功能,您可以运行:

rfFuncs$summary <- function (data, lev = NULL, model = NULL) { 
    if (is.character(data$obs)) 
     data$obs <- factor(data$obs, levels = lev) 
    postResample(data[, "pred"], data[, "obs"]) 
} 

只要确保postResample已被编辑或替换它与您的等效功能的名称。