回歸樹模型在之前的
博客文章中已有介紹,。而
裝袋算法與隨機(jī)森林相對而言會生成多個樹模型,再進(jìn)行組合預(yù)測,,其效果遠(yuǎn)大于單個樹模型,。裝袋算法(bagging)采取自助法的思路,從樣本中隨機(jī)抽樣,,形成多個訓(xùn)練樣本,,生成多個樹模型。然后以多數(shù)投票的方式來預(yù)測結(jié)果,。隨機(jī)森林則(randomForest)更進(jìn)一步,,不僅對樣本進(jìn)行抽樣,還對變量進(jìn)行抽樣,。下面來橫向?qū)Ρ纫幌赂魉惴ā?BR>
首先讀入必要的程序包
library(DMwR)
library(rpart)
library(ipred)
library(randomForest)
前二種算法可以計算缺失數(shù)據(jù),,但隨機(jī)森林不行,所以還需將數(shù)據(jù)進(jìn)行清洗整理
data(algae)
algae <- algae[-manyNAs(algae), ]
clean.algae <- knnImputation(algae,k=10)
回歸樹模型計算
model.tree=rpart(a1 ~ ., data = clean.algae[, 1:12])
pre.tree <- predict(model.tree, clean.algae)
plot(pre.tree~clean.algae$a1)
nmse1 <- mean((pre.tree- clean.algae[,'a1'])^2)/
mean((mean(clean.algae[,'a1'])- clean.algae[,'a1'])^2)
裝袋算法計算
model.bagging <- bagging(
a1 ~ ., data = clean.algae[, 1:12], nbagg=1000)
pre.bagging=predict(model.bagging,clean.algae)
plot(pre.bagging~clean.algae$a1)
nmse2 <- mean((pre.bagging- clean.algae[,'a1'])^2)/
mean((mean(clean.algae[,'a1'])- clean.algae[,'a1'])^2)
隨機(jī)森林計算
model.forest <-randomForest(a1 ~ ., data = clean.algae)
#若有缺失數(shù)據(jù)需加入: na.action=na.omit
pre.forest=predict(model.forest, clean.algae)
plot(pre.forest~ clean.algae$a1)
(nmse3 <- mean((pre.forest- clean.algae[,'a1'])^2)/
mean((mean( clean.algae[,'a1'])- clean.algae[,'a1'])^2)
print(c(nmse1,nmse2,nmse3))
用預(yù)測值與真值之間的相對離差平方和來作為測量誤差的指標(biāo),,其結(jié)果分別為:
0.3541180 0.3103366 0.1002235 可以看出隨機(jī)森林是最有效的,。
再來看看處理分類數(shù)據(jù)的表現(xiàn),利用iris數(shù)據(jù)來判斷花的種類
library(randomForest)
model.forest <-randomForest(Species ~ ., data = iris)
pre.forest=predict(model.forest, iris)
table(pre.forest,iris$Species)
pre.forest setosa versicolor virginica
setosa 50 0 0
versicolor 0 50 0
virginica 0 0 50
library(rpart)
model.tree=rpart(Species ~ ., data = iris,method='class')
pre.tree=predict(model.tree, data = iris,type='class')
table(pre.tree,iris$Species)
pre.tree setosa versicolor virginica
setosa 50 0 0
versicolor 0 49 5
virginica 0 1 45
隨機(jī)森林算法預(yù)測全對,,而分類樹模型則有六處錯誤,。