【正文】
為 : 68 Boosting((調用 mboost 包) 69 70 library(mboost) 調用 mboost 包 mboostlm=blackboost(a1~.,control=boost_control(mstop=50),data=algaetree[,1:12]) attributes(mboostlm) t=predict(mboostlm) cat(mboost 訓練集上的 NMSE 為 :, mean((algaetree$a1(t))^2)/mean((mean(algaetree$a1)algaetree$a1)^2),\n) mboost 訓練集上的 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 相似的技術。 Bagging 技術的主要思想是給定一弱學習算法和一訓練集 1 1 ( , ),..., ( , ) n n x y x y 。讓該學習算法訓練多輪,每輪的訓練集由從初始的訓練集中隨機取出的 n 個訓練例組成,初始訓練例在某輪訓練集中可以出現(xiàn)多次或根本不出現(xiàn)。訓練之后可得到一個預測函數(shù)序列: 1,..., t h h ,最終的預測函數(shù) H 對分類問題采用投票方式,對回歸問題采用簡單平均方法對新示例進行判別。 Bagging 與 Boosting 的區(qū)別在于 Bagging 的訓練集的選擇是隨機的,各輪訓練集之間相互獨立,而 Boosting 的訓練集的選擇不是獨立的,各輪訓練集的選擇與前面各輪的學習結果有關; Bagging 的各個預測函數(shù)沒有權重,而 Boosting是有權重的; Bagging 的各個預測函數(shù)可以并行生成,而 Boosting 的各個預測函數(shù)只能順序生成。對于象神經(jīng)網(wǎng)絡這樣極為耗時的學習方法, Bagging 可通過并行訓練節(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 訓練集上的 NMSE 為 :,mean((algaetree$a1(baggingtrain))^2)/mean((mean(algaetree$a1) algaetree$a1)^2),\n) Bagging 訓練集上的 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) 調用 randomForest 包 因為 randomForest 不能自動處理缺失值,得插補 algaetestt=algaetest 對訓練集中含有缺失項的用方法四進行插補 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 訓練集上的 NMSE 為 :, mean((a1(predict()))^2)/mean((mean(a1)a1)^2),\n) randomforest 訓練集上的 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 謝謝