假设我的数据集是一个充满分类变量的 100 x 3
矩阵。我想对响应变量进行二元分类。让我们用以下代码组成一个数据集:
set.seed(2013)
y <- as.factor(round(runif(n=100,min=0,max=1),0))
var1 <- rep(c("red","blue","yellow","green"),each=25)
var2 <- rep(c("shortest","short","tall","tallest"),25)
df <- data.frame(y,var1,var2)
数据如下所示:
> head(df)
y var1 var2
1 0 red shortest
2 1 red short
3 1 red tall
4 1 red tallest
5 0 red shortest
6 1 red short
我尝试使用两种不同的方法对这些数据进行随机森林和 adaboost。第一种方法是按原样使用数据:
> library(randomForest)
> randomForest(y~var1+var2,data=df,ntrees=500)
Call:
randomForest(formula = y ~ var1 + var2, data = df, ntrees = 500)
Type of random forest: classification
Number of trees: 500
No. of variables tried at each split: 1
OOB estimate of error rate: 44%
Confusion matrix:
0 1 class.error
0 29 22 0.4313725
1 22 27 0.4489796
----------------------------------------------------
> library(ada)
> ada(y~var1+var2,data=df)
Call:
ada(y ~ var1 + var2, data = df)
Loss: exponential Method: discrete Iteration: 50
Final Confusion Matrix for Data:
Final Prediction
True value 0 1
0 34 17
1 16 33
Train Error: 0.33
Out-Of-Bag Error: 0.33 iteration= 11
Additional Estimates of number of iterations:
train.err1 train.kap1
10 16
第二种方法是将数据集转换为宽格式并将每个类别视为一个变量。我这样做的原因是因为我的实际数据集在 var1 和 var2 中有 500 多个因子,因此,树分区总是将这 500 个类别分为 2 个部分。这样做会丢失很多信息。转换数据:
id <- 1:100
library(reshape2)
tmp1 <- dcast(melt(cbind(id,df),id.vars=c("id","y")),id+y~var1,fun.aggregate=length)
tmp2 <- dcast(melt(cbind(id,df),id.vars=c("id","y")),id+y~var2,fun.aggregate=length)
df2 <- merge(tmp1,tmp2,by=c("id","y"))
新数据如下所示:
> head(df2)
id y blue green red yellow short shortest tall tallest
1 1 0 0 0 2 0 0 2 0 0
2 10 1 0 0 2 0 2 0 0 0
3 100 0 0 2 0 0 0 0 0 2
4 11 0 0 0 2 0 0 0 2 0
5 12 0 0 0 2 0 0 0 0 2
6 13 1 0 0 2 0 0 2 0 0
我将随机森林和 adaboost 应用于这个新数据集:
> library(randomForest)
> randomForest(y~blue+green+red+yellow+short+shortest+tall+tallest,data=df2,ntrees=500)
Call:
randomForest(formula = y ~ blue + green + red + yellow + short + shortest + tall + tallest, data = df2, ntrees = 500)
Type of random forest: classification
Number of trees: 500
No. of variables tried at each split: 2
OOB estimate of error rate: 39%
Confusion matrix:
0 1 class.error
0 32 19 0.3725490
1 20 29 0.4081633
----------------------------------------------------
> library(ada)
> ada(y~blue+green+red+yellow+short+shortest+tall+tallest,data=df2)
Call:
ada(y ~ blue + green + red + yellow + short + shortest + tall +
tallest, data = df2)
Loss: exponential Method: discrete Iteration: 50
Final Confusion Matrix for Data:
Final Prediction
True value 0 1
0 36 15
1 20 29
Train Error: 0.35
Out-Of-Bag Error: 0.33 iteration= 26
Additional Estimates of number of iterations:
train.err1 train.kap1
5 10
两种方法的结果是不同的。当我们在每个变量中引入更多级别(即 var1
和 var2
)时,差异更加明显。我的问题是,既然我们使用完全相同的数据,为什么结果不同?我们应该如何解释这两种方法的结果?哪个更可靠?
最佳答案
虽然这两个模型看起来相同,但它们本质上是不同的 - 在第二个模型中,您隐式地包含了给定观察可能具有多种颜色和多个高度的可能性。两种模型公式之间的正确选择将取决于您现实世界观察的特征。如果这些字符是排他性的(即每个观察值都是单一颜色和高度),则模型的第一个公式将是正确使用的公式。但是,如果观察结果可能是蓝色和绿色,或任何其他颜色组合,则可以使用第二种配方。从原始数据的预感来看,第一个似乎是最合适的(即,观察如何具有多个高度?)。
此外,为什么将 df2 中的逻辑变量列编码为 0 和 2,而不是 0/1?我想知道这是否会对拟合产生任何影响,具体取决于数据如何编码为因子或数字。
关于基于 R 树的方法,如 randomForest、adaboost : interpret result of same data with different format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15797987/