Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用随机森林在R中指定坏因子水平_R - Fatal编程技术网

使用随机森林在R中指定坏因子水平

使用随机森林在R中指定坏因子水平,r,R,我正在R中运行一个带有样本外验证数据的randomForest模型: predictions <- predict(rf, newdata = model_final, type = "prob") 忽略特定于我的数据和模型的所有内容,是否有任何方法强制预测指定哪些列是具有新因子水平的列?或者是否有另一种快速、程序化的方法来识别有问题的列?假设训练集和测试集具有相同的列顺序,只需使用单个映射来识别因子级别不相同的地方: 示例数据 training <- data.frame(a=a

我正在R中运行一个带有样本外验证数据的
randomForest
模型:

predictions <- predict(rf, newdata = model_final, type = "prob")

忽略特定于我的数据和模型的所有内容,是否有任何方法强制
预测
指定哪些列是具有新因子水平的列?或者是否有另一种快速、程序化的方法来识别有问题的列?

假设训练集和测试集具有相同的列顺序,只需使用单个映射来识别
因子
级别不相同的地方:

示例数据

training <- data.frame(a=as.factor(letters), b=letters, stringsAsFactors=F)
test     <- data.frame(a=as.factor(rep(letters[1:20],3)), b=rep(letters[1:20],3), stringsAsFactors=F)
如果上述结果为假,则训练集和测试集之间的因子水平存在差异。由于它在数字、逻辑或字符列的情况下使用
idential
,因此函数
levels
idential
返回TRUE的两种情况下都返回NULL

假设我理解了您在问题中的意思,只需查找从上述函数返回的假列

training <- data.frame(a=as.factor(letters), b=letters, stringsAsFactors=F)
test     <- data.frame(a=as.factor(rep(letters[1:20],3)), b=rep(letters[1:20],3), stringsAsFactors=F)
> mapply(function(x,y) identical(levels(x), levels(y)), training, test )
    a     b 
FALSE  TRUE