決策樹與隨機(jī)森林

PART I 決策樹 (Decision Tree)

決策樹基本知識(shí)


image.png
image.png

決策樹何時(shí)停止生長(zhǎng):
(I) all leaf nodes are pure with entropy of zero;
(II) a prespecified minimum change in purity cannot be made with any splitting methods;
(III) the number of observations in the leaf node reaches the pre-specified minimum one.

  1. 加載并查看數(shù)據(jù)集
data(airquality)
str(airquality)

# 'data.frame': 153 obs. of  6 variables:
#   $ Ozone  : int  41 36 12 18 NA 28 23 19 8 NA ...
# $ Solar.R: int  190 118 149 313 NA NA 299 99 19 194 ...
# $ Wind   : num  7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
# $ Temp   : int  67 72 74 62 56 66 65 59 61 69 ...
# $ Month  : int  5 5 5 5 5 5 5 5 5 5 ...
# $ Day    : int  1 2 3 4 5 6 7 8 9 10 ...
  1. 插補(bǔ)Ozone變量中的缺失值
set.seed(888)
airquality[is.na(airquality$Ozone),1] <- sample(airquality[!is.na(airquality$Ozone),1],37) #使用非缺失值進(jìn)行插補(bǔ)
summary(airquality$Ozone)

# Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
# 1.00   16.00   30.00   41.31   63.00  168.00 
  1. 擬合模型
library(party)
airct <- ctree(Ozone ~ ., data = airquality,controls = ctree_control(maxsurrogate = 3))
airct

# Conditional inference tree with 4 terminal nodes
# 
# Response:  Ozone 
# Inputs:  Solar.R, Wind, Temp, Month, Day 
# Number of observations:  153 
# 
# 1) Temp <= 76; criterion = 1, statistic = 47.479
# 2)*  weights = 61 
# 1) Temp > 76
# 3) Wind <= 6.3; criterion = 1, statistic = 24.235
# 4)*  weights = 21 
# 3) Wind > 6.3
# 5) Temp <= 84; criterion = 0.964, statistic = 7.182
# 6)*  weights = 45 
# 5) Temp > 84
# 7)*  weights = 26 
  1. 結(jié)果可視化
plot(airct)
image.png

查看具體每個(gè)節(jié)點(diǎn)的情況

plot(airct, inner_panel = node_boxplot, edge_panel = function(...) invisible(),tnex = 1)
image.png
inner <- nodes(airct, c(1,3,5,7))
layout(matrix(1:length(inner), ncol = length(inner)/2))
out <- sapply(inner, function(i) {
  splitstat <- i$psplit$splitstatistic
  x <- airquality[[i$psplit$variableName]][splitstat >0]
  plot(x, splitstat[splitstat > 0],main =
              paste("Node",i$nodeID), xlab = i$psplit$variableName,
            ylab = "Statistic",ylim = c(0, 10), cex.axis = 1.2, cex.lab =
              1.2,cex.main = 1.2)
  abline(v = i$psplit$splitpoint, lty = 4)
})
image.png

連續(xù)型變量有多種分割方法,用一個(gè)統(tǒng)計(jì)量statistic可以描述每種分割方法的好差程度,statistic值越大說(shuō)明分割越好

  1. 決策樹的預(yù)測(cè)
ind <- sample(2, nrow(airquality), replace=TRUE, prob
              = c(0.7,0.3))
newData <- airquality[ind==2,]
newpred <- predict(airct, newdata= newData)
plot(newpred,newData$Ozone,xlab="Ozone value predicted by decision tree",
     ylab="Observed ozone value")
image.png
PART II 隨機(jī)森林 (Random Forest)
image.png
  1. 擬合隨機(jī)森林模型
aircf<-cforest(Ozone ~ ., data = airquality)
aircf

# Random Forest using Conditional Inference Trees
# 
# Number of trees:  500 
# 
# Response:  Ozone 
# Inputs:  Solar.R, Wind, Temp, Month, Day 
# Number of observations:  153 
  1. 評(píng)估預(yù)測(cè)效果
predforest <- predict(aircf, newdata= newData)
plot(predforest,newData$Ozone,ylab="Observed ozone value",
     xlab="Predicted ozone value based on random forest")
image.png
PART III Model based recursive partitioning
airmod <- mob(Ozone ~Temp+Day|Solar.R+Wind+Month, data = airquality)
# Variables after symbol “|” are partitioning variables
plot(airmod)
image.png
airmod
# 1) Wind <= 6.3; criterion = 0.999, statistic = 25.078
# 2)*  weights = 23 
# Terminal node model
# Gaussian GLM with coefficients:
#   (Intercept)         Temp          Day  
# -96.4060       2.0736      -0.1832  
# 
# 1) Wind > 6.3
# 3)*  weights = 123 
# Terminal node model
# Gaussian GLM with coefficients:
#   (Intercept)         Temp          Day  
# -90.0534       1.5811       0.2236  
# 

coef(airmod)
# (Intercept)     Temp        Day
# 2   -96.40597 2.073623 -0.1831996
# 3   -90.05342 1.581143  0.2235920

sctest(airmod,node = 1)
# Solar.R         Wind        Month
# statistic 6.0347325 25.077906381 22.955024196
# p.value   0.9661175  0.001153747  0.003099691

參考資料

  1. 章仲恒教授丁香園課程:決策樹與隨機(jī)森林
  2. Annals of Translational Medicine: Big-data Clinical Trial Column
  3. Zhongheng Zhang Decision tree modeling using R
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子病游,更是在濱河造成了極大的恐慌,老刑警劉巖虏等,帶你破解...
    沈念sama閱讀 222,183評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件乍桂,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡怜俐,警方通過查閱死者的電腦和手機(jī)身堡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)拍鲤,“玉大人贴谎,你說(shuō)我怎么就攤上這事〖疚龋” “怎么了擅这?”我有些...
    開封第一講書人閱讀 168,766評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)景鼠。 經(jīng)常有香客問我仲翎,道長(zhǎng),這世上最難降的妖魔是什么铛漓? 我笑而不...
    開封第一講書人閱讀 59,854評(píng)論 1 299
  • 正文 為了忘掉前任溯香,我火速辦了婚禮,結(jié)果婚禮上票渠,老公的妹妹穿的比我還像新娘逐哈。我一直安慰自己,他們只是感情好问顷,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評(píng)論 6 398
  • 文/花漫 我一把揭開白布昂秃。 她就那樣靜靜地躺著,像睡著了一般杜窄。 火紅的嫁衣襯著肌膚如雪肠骆。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,457評(píng)論 1 311
  • 那天塞耕,我揣著相機(jī)與錄音蚀腿,去河邊找鬼。 笑死扫外,一個(gè)胖子當(dāng)著我的面吹牛莉钙,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播筛谚,決...
    沈念sama閱讀 40,999評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼磁玉,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了驾讲?” 一聲冷哼從身側(cè)響起蚊伞,我...
    開封第一講書人閱讀 39,914評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎吮铭,沒想到半個(gè)月后时迫,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,465評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡谓晌,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評(píng)論 3 342
  • 正文 我和宋清朗相戀三年掠拳,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片纸肉。...
    茶點(diǎn)故事閱讀 40,675評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡溺欧,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出毁靶,到底是詐尸還是另有隱情胧奔,我是刑警寧澤,帶...
    沈念sama閱讀 36,354評(píng)論 5 351
  • 正文 年R本政府宣布预吆,位于F島的核電站龙填,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏拐叉。R本人自食惡果不足惜岩遗,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評(píng)論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望凤瘦。 院中可真熱鬧宿礁,春花似錦、人聲如沸蔬芥。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至返吻,卻和暖如春姑子,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背测僵。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工街佑, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人捍靠。 一個(gè)月前我還...
    沈念sama閱讀 49,091評(píng)論 3 378
  • 正文 我出身青樓沐旨,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親榨婆。 傳聞我的和親對(duì)象是個(gè)殘疾皇子磁携,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評(píng)論 2 360

推薦閱讀更多精彩內(nèi)容