插入符号 loclda 方法的变量重要性
Variable Importance for caret loclda method
我正在尝试从插入符号 'loclda' 模型中计算变量重要性。
我的训练数据集包含 20 个数字预测变量和 4 个离散结果 类(2500 个样本)。
我构建了模型并使用以下命令计算了变量重要性:
# fit the model
control <- trainControl(
method="repeatedcv",
number=10,
repeats=3,
savePredictions='final',
summaryFunction=multiClassSummary,
returnData=F,
trim=T,
allowParallel=F
)
my_loclda_model <- train(MyClass ~ ., data=trainData, method='loclda', trControl=control, importance=T)
# compute variable importance
varImp(my_loclda_model)
Error : is.list(x) is not TRUE
traceback()
7: stop(msg, call. = FALSE, domain = NA)
6: stopifnot(is.list(x), is.list(val))
5: modifyList(data, lapply(data[, fc], as.numeric))
4: asNumeric(x)
3: filterVarImp(x_dat, y_dat, nonpara = nonpara, ...)
2: varImp.train(model_name)
1: varImp(model_name)
从 caret 用户手册中我们可以读到以下内容:
For models that do not have corresponding varImp methods, see filterVarImp
假设 loclda
模型没有 varImp
方法,我尝试直接从最终模型使用 filterVarImp
函数:
str(my_loclda_model$finalModel)
List of 12
$ learn : num [1:2500, 1:20] -0.404 -0.336 -0.655 -0.618 -0.151 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:2500] "X2" "X3" "X5" "X6" ...
.. ..$ : chr [1:20] "indexA1" "indexF1" "index4" "index5" ...
$ grouping : Factor w/ 4 levels "group1","group2","group3",..: 1 5 2 5 1 1 3 4 3 1 ...
..- attr(*, "names")= chr [1:2500] "2" "3" "5" "6" ...
$ lev : chr [1:4] "group1" "group2" "group3" ...
$ weight.func :function (x)
$ k : num 3401
$ weighted.apriori: logi TRUE
$ call : language loclda(x = x, grouping = y, k = floor(param$k), importance = ..1)
$ xNames : chr [1:20] "indexA1" "indexF1" "index4" "index5" ...
$ problemType : chr "Classification"
$ tuneValue :'data.frame': 1 obs. of 1 variable:
..$ k: num 3401
$ obsLevels : atomic [1:4] group1 group2 group3 ...
..- attr(*, "ordered")= logi FALSE
$ param :List of 1
..$ importance: logi TRUE
- attr(*, "class")= chr "loclda"
imp_out <- filterVarImp(x=my_loclda_model$finalModel$learn, y=my_loclda_model$finalModel$grouping)
Error in data[, fc] : (subscript) logical subscript too long
traceback()
5: lapply(data[, fc], as.numeric)
4: stopifnot(is.list(x), is.list(val))
3: modifyList(data, lapply(data[, fc], as.numeric))
2: asNumeric(x)
1: filterVarImp(x = model_name$finalModel$learn, y = model_name$finalModel$grouping)
我不明白。我应该将哪些 x
和 y
参数传递给 filterVarImp
?
谢谢!
注意:使用其他方法(例如 svmRadial、svmPoly、LogitBoost、regLogistic 等)时也会出现同样的问题。
指定 x=as.data.frame(my_loclda_model$finalModel$learn)
而不是矩阵似乎可以解决问题,尽管手册中提到矩阵和数据框都可以使用。
我正在尝试从插入符号 'loclda' 模型中计算变量重要性。
我的训练数据集包含 20 个数字预测变量和 4 个离散结果 类(2500 个样本)。
我构建了模型并使用以下命令计算了变量重要性:
# fit the model
control <- trainControl(
method="repeatedcv",
number=10,
repeats=3,
savePredictions='final',
summaryFunction=multiClassSummary,
returnData=F,
trim=T,
allowParallel=F
)
my_loclda_model <- train(MyClass ~ ., data=trainData, method='loclda', trControl=control, importance=T)
# compute variable importance
varImp(my_loclda_model)
Error : is.list(x) is not TRUE
traceback()
7: stop(msg, call. = FALSE, domain = NA)
6: stopifnot(is.list(x), is.list(val))
5: modifyList(data, lapply(data[, fc], as.numeric))
4: asNumeric(x)
3: filterVarImp(x_dat, y_dat, nonpara = nonpara, ...)
2: varImp.train(model_name)
1: varImp(model_name)
从 caret 用户手册中我们可以读到以下内容:
For models that do not have corresponding varImp methods, see filterVarImp
假设 loclda
模型没有 varImp
方法,我尝试直接从最终模型使用 filterVarImp
函数:
str(my_loclda_model$finalModel)
List of 12
$ learn : num [1:2500, 1:20] -0.404 -0.336 -0.655 -0.618 -0.151 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:2500] "X2" "X3" "X5" "X6" ...
.. ..$ : chr [1:20] "indexA1" "indexF1" "index4" "index5" ...
$ grouping : Factor w/ 4 levels "group1","group2","group3",..: 1 5 2 5 1 1 3 4 3 1 ...
..- attr(*, "names")= chr [1:2500] "2" "3" "5" "6" ...
$ lev : chr [1:4] "group1" "group2" "group3" ...
$ weight.func :function (x)
$ k : num 3401
$ weighted.apriori: logi TRUE
$ call : language loclda(x = x, grouping = y, k = floor(param$k), importance = ..1)
$ xNames : chr [1:20] "indexA1" "indexF1" "index4" "index5" ...
$ problemType : chr "Classification"
$ tuneValue :'data.frame': 1 obs. of 1 variable:
..$ k: num 3401
$ obsLevels : atomic [1:4] group1 group2 group3 ...
..- attr(*, "ordered")= logi FALSE
$ param :List of 1
..$ importance: logi TRUE
- attr(*, "class")= chr "loclda"
imp_out <- filterVarImp(x=my_loclda_model$finalModel$learn, y=my_loclda_model$finalModel$grouping)
Error in data[, fc] : (subscript) logical subscript too long
traceback()
5: lapply(data[, fc], as.numeric)
4: stopifnot(is.list(x), is.list(val))
3: modifyList(data, lapply(data[, fc], as.numeric))
2: asNumeric(x)
1: filterVarImp(x = model_name$finalModel$learn, y = model_name$finalModel$grouping)
我不明白。我应该将哪些 x
和 y
参数传递给 filterVarImp
?
谢谢!
注意:使用其他方法(例如 svmRadial、svmPoly、LogitBoost、regLogistic 等)时也会出现同样的问题。
指定 x=as.data.frame(my_loclda_model$finalModel$learn)
而不是矩阵似乎可以解决问题,尽管手册中提到矩阵和数据框都可以使用。