【正文】
73 library(ipred) =bagging(algaetree$a1~.,data=algaetree[,1:12],coob=T,control=ntrol(xval=10)) attributes() baggingtrain=predict(,algaetree[,1:12]) baggingpre=predict(, algaetest[,1:12]) baggingpre cat(Bagging 訓(xùn)練集上的 NMSE 為 :,mean((algaetree$a1(baggingtrain))^2)/mean((mean(algaetree$a1) algaetree$a1)^2),\n) Bagging 訓(xùn)練集上的 NMSE 為 : cat(Bagging 測試集上的 NMSE 為 :,mean((algaetest$a1(baggingpre))^2)/mean((mean(algaetest$a1)algaetest$a1)^2), \n) Bagging 測試集上的 NMSE 為 : $mtrees 大量的樹(按照缺省值為 25個(gè)) 74 隨機(jī)森林( Random Forest) 75 library(randomForest) 調(diào)用 randomForest 包 因?yàn)?randomForest 不能自動(dòng)處理缺失值,得插補(bǔ) algaetestt=algaetest 對訓(xùn)練集中含有缺失項(xiàng)的用方法四進(jìn)行插補(bǔ) for(r in which(!(algaetest))) algaetestt[r,which((algaetest[r,]))] apply((algaetest[c((names(sort([r,])[2:11]))), which((algaetest[r,]))]), 2,) randomForest(a1 ~ .,data=[,1:12],ntree=500,importance=TRUE,proximity=TRUE) attributes() 76 Here are the definitions of the variable importance measures. For each tree, the prediction accuracy on the outofbag portion of the data is recorded. Then the same is done after permuting each predictor variable. The difference between the two accuracies are then averaged over all trees, and normalized by the standard error. For regression, the MSE is puted on the outofbag data for each tree, and then the same puted after permuting a variable. The differences are averaged and normalized by the standard error. If the standard error is equal to 0 for a variable, the division is not done (but the measure is almost always equal to 0 in that case). The second measure is the total decrease in node impurities from splitting on the variable, averaged over all trees. For classification, the node impurity is measured by the Gini index. For regression, it is measured by residual sum of squares. 77 $importance 查看解釋變量對模型的貢獻(xiàn)性的大小 $importanceSD 可以看出,解釋變量 PO Cl、 oPO NH4 是比較重要的變量。對于象神經(jīng)網(wǎng)絡(luò)這樣極為耗時(shí)的學(xué)習(xí)方法, Bagging 可通過并行訓(xùn)練節(jié)省大量時(shí)間開銷。訓(xùn)練之后可得到一個(gè)預(yù)測函數(shù)序列: 1,..., t h h ,最終的預(yù)測函數(shù) H 對分類問題采用投票方式,對回歸問題采用簡單平均方法對新示例進(jìn)行判別。 Bagging 技術(shù)的主要思想是給定一弱學(xué)習(xí)算法和一訓(xùn)練集 1 1 ( , ),..., ( , ) n n x y x y 。 |PO 4 = 4 3 . 8 2C l = 7 . 8 0 6o PO 4 = 5 1 . 1 2m n O 2 = 1 0 . 0 5N O 3 = 3 . 1 8 8m n O 2 8m x PH 7 . 8 7m x PH = 7 . 0 4 5 PO 4 = 1 5 . 1 83 . 8 4 66 . 7 1 77 . 8 6 71 3 . 8 1 3 1 . 4 43 8 . 7 12 6 . 3 9 4 6 . 1 5 3 8 . 1 8 5 9 . 1 465 printcp() 顯示回歸樹 每一步得出的 subtrees 的詳細(xì)信息 66 prune(,cp=) 以 cp= 為尺度對樹 進(jìn)行修剪 plot(, uniform=T,branch=1, margin=, cex=) 畫出修剪后的回歸樹 text(,cex=) 在樹中顯示分枝的信息。因?yàn)榛貧w樹能自己處理缺失值,所以我們可以直接利用原數(shù)據(jù)集 algae(只需要剔出含缺失項(xiàng)過多的第 62 條和第 199 條水樣)。 60 library(kknn) a1x=(a1) colnames(a1x)=c(a1) xunlianzzcbind(xunlian,a1x) a1c=(algaetest2$a1) colnames(a1c)=c(a1) ceshizz=cbind(ceshi,a1c) knn1lmkknn(a1~.,(xunlianzz),(ceshizz),k=1,distance =1,kernel = rectangular) attributes(knn1lm) cat(knn1 測試集上的預(yù)測誤差為 :,1mean(knn1lm$==algaetest2$a1), \n) knn1 測試集上的預(yù)測誤差為 : knn10lmkknn(a1~.,(xunlianzz),(ceshizz),k=10,distance =1,kernel = rectangular) cat(knn10 測試集上的預(yù)測誤差為 :,1mean(knn10lm$==algaetest2$a1), \n) knn10 測試集上的預(yù)測誤差為 : 1 61 可以看出, K— 近鄰算法在此處也許并不適應(yīng),因?yàn)?K— 近鄰算法主要適用于因變量為多維標(biāo)度變量(比如,季節(jié)可分別春、夏、秋、冬四季),當(dāng)因變量為分組類型的話,K— 近鄰回歸的效果往往會(huì)很好,但此處,a1 為數(shù)值變量,如果能將 a1 分別按某一尺度分為大、中、小三種類型再用 K— 近鄰回歸,往往效果會(huì)更好。 59 K 近鄰回歸( kknn 包) 主要用于分類。所以首先,我們可以先從一個(gè)穩(wěn)定的學(xué)習(xí)算法 — K 最近鄰方法來看看對該數(shù)據(jù)集的擬合效果和預(yù)測精度,再從不穩(wěn)定的算法如決策樹、隨機(jī)森林以及針對不穩(wěn)定的算法判定樹的兩種“提升” — Boosting 和 Bagging 算法( Breiman 指出,穩(wěn)定性是 Boosting 和Bagging 能否提高預(yù)測準(zhǔn)確率的關(guān)鍵因素:它們對不穩(wěn)定的學(xué)習(xí)算法能提高預(yù)測的準(zhǔn)確度,而對穩(wěn)定的學(xué)習(xí)算法效果不明顯,有時(shí)甚至使預(yù)測精確度降低。 57 統(tǒng)計(jì)應(yīng)用領(lǐng)域的一些新方法 58 學(xué)習(xí)算法的 不穩(wěn)定性 是指如果 訓(xùn)練集有較小的變化,學(xué)習(xí)算法產(chǎn)生的預(yù)測函數(shù)將發(fā)生較大變化 。 ($a1predict11$fit) 訓(xùn)練集上的殘差檢驗(yàn) W = , pvalue = (algaetestzz$a1predict1$fit) 測試集上的殘差檢