【正文】
為 : 68 Boosting((調(diào)用 mboost 包) 69 70 library(mboost) 調(diào)用 mboost 包 mboostlm=blackboost(a1~.,control=boost_control(mstop=50),data=algaetree[,1:12]) attributes(mboostlm) t=predict(mboostlm) cat(mboost 訓(xùn)練集上的 NMSE 為 :, mean((algaetree$a1(t))^2)/mean((mean(algaetree$a1)algaetree$a1)^2),\n) mboost 訓(xùn)練集上的 NMSE 為 : t1=predict(mboostlm,algaetest[,1:12]) cat(mboost 測試集上的 NMSE 為 :,mean((algaetest$a1(t1))^2)/mean((mean(algaetest$a1)algaetest$a1)^2), \n) mboost 測試集上的 NMSE 為 : 71 Bagging(bootstrap aggregation) 72 Bagging 是 Breiman 提出的與 Boosting 相似的技術(shù)。 Bagging 技術(shù)的主要思想是給定一弱學(xué)習(xí)算法和一訓(xùn)練集 1 1 ( , ),..., ( , ) n n x y x y 。讓該學(xué)習(xí)算法訓(xùn)練多輪,每輪的訓(xùn)練集由從初始的訓(xùn)練集中隨機取出的 n 個訓(xùn)練例組成,初始訓(xùn)練例在某輪訓(xùn)練集中可以出現(xiàn)多次或根本不出現(xiàn)。訓(xùn)練之后可得到一個預(yù)測函數(shù)序列: 1,..., t h h ,最終的預(yù)測函數(shù) H 對分類問題采用投票方式,對回歸問題采用簡單平均方法對新示例進行判別。 Bagging 與 Boosting 的區(qū)別在于 Bagging 的訓(xùn)練集的選擇是隨機的,各輪訓(xùn)練集之間相互獨立,而 Boosting 的訓(xùn)練集的選擇不是獨立的,各輪訓(xùn)練集的選擇與前面各輪的學(xué)習(xí)結(jié)果有關(guān); Bagging 的各個預(yù)測函數(shù)沒有權(quán)重,而 Boosting是有權(quán)重的; Bagging 的各個預(yù)測函數(shù)可以并行生成,而 Boosting 的各個預(yù)測函數(shù)只能順序生成。對于象神經(jīng)網(wǎng)絡(luò)這樣極為耗時的學(xué)習(xí)方法, Bagging 可通過并行訓(xùn)練節(jié)省大量時間開銷。以下我將通過 R 語言中的 ipred 包運用Bagging 算法對該數(shù)據(jù)集進行分析研究。 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個) 74 隨機森林( Random Forest) 75 library(randomForest) 調(diào)用 randomForest 包 因為 randomForest 不能自動處理缺失值,得插補 algaetestt=algaetest 對訓(xùn)練集中含有缺失項的用方法四進行插補 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 查看解釋變量對模型的貢獻性的大小 $importanceSD 可以看出,解釋變量 PO Cl、 oPO NH4 是比較重要的變量。 2 4 6 8 10234567In d e xrandomforest.a1$importanceSD689101178 2 4 6 8 10234567I n d e xrandomforest.a1$importanceSDClNH4o P O 4P O 4C h l aplot($importanceSD) identify(1:11,$importanceSD,labels=names($importanceSD)) 79 cat(randomforest 訓(xùn)練集上的 NMSE 為 :, mean((a1(predict()))^2)/mean((mean(a1)a1)^2),\n) randomforest 訓(xùn)練集上的 NMSE 為 : randomforestpre=predict(, algaetestt[,1:12]) cat(randomforest 測試集上的 NMSE 為 :,mean((algaetestt$a1(randomforestpre))^2)/mean((mean(algaetestt$a1)algaetestt$a1)^2), \n) randomforest 測試集上的 NMSE 為 : 80 81 謝謝