如何调试"无效的下标类型' list'" R中的错误(genalg包)

时间:2014-04-07 19:42:37

标签: r genetic-algorithm feature-selection mass-package

我是遗传算法的新手,我正在尝试基于genalg软件包文档示例的简单变量选择代码:

data(iris)
library(MASS)
X <- cbind(scale(iris[,1:4]), matrix(rnorm(36*150), 150, 36))
Y <- iris[,5]
iris.evaluate <- function(indices) {
result = 1
if (sum(indices) > 2) {
huhn <- lda(X[,indices==1], Y, CV=TRUE)$posterior
result = sum(Y != dimnames(huhn)[[2]][apply(huhn, 1,
function(x)
which(x == max(x)))]) / length(Y)
}
result
}
monitor <- function(obj) {
minEval = min(obj$evaluations);
plot(obj, type="hist");
}
woppa <- rbga.bin(size=40, mutationChance=0.05, zeroToOneRatio=10,
evalFunc=iris.evaluate, verbose=TRUE, monitorFunc=monitor)

代码本身就可以正常工作,但是当我尝试应用我的数据集(here)时,我收到以下错误:

X <- reducedScaledTrain[,-c(541,542)]
Y <- reducedScaledTrain[,542]

ga <- rbga.bin(size=540, mutationChance=0.05, zeroToOneRatio=10,
     evalFunc=iris.evaluate, verbose=TRUE, monitorFunc=monitor)
Testing the sanity of parameters...
Not showing GA settings...
Starting with random values in the given domains...
Starting iteration 1 
Calucating evaluation values... Error in dimnames(huhn)[[2]][apply(huhn, 1, function(x) which(x == max(x)))] : 
  invalid subscript type 'list'

我正在尝试使用LDA对540个变量进行特征选择(我已经消除了100%相关的变量)。我已经尝试将我的数据转换为数字或列表,但无济于事。我也尝试逐条输入线条,'huhn'线可以很好地处理我的数据。请帮忙,我可能会遗漏一些东西......

0 个答案:

没有答案