2016-05-31 8 views
0

私は自分のデータセットに対してランダムフォレストの分類を複数回実行するRスクリプトを作成しています。もっと頑強な結果を得るために、少なくとも10回の平均値を使用したいと思います。だから、私はこの機能をforループで実行しています。これは、ランダムフォレスト分類子を何度も実行しています(n = iterations)。ランダムなフォレストの結果をファイルに書き込む

iterateRandomForest <- function (samples,iterations,output_text,outname,pVSURF,b) { 
    for (i in (1: iterations)) { 

    cat("\n Loop starts", "\n", file=output_text,append=TRUE)  
    time <- toString(Sys.time()) 
    cat(time,"\n", file=output_text,append=TRUE) 
    cat("Iteration number ",i," for variable set: ", outname, "\n", sep="",file=output_text,append=TRUE) 

    load(pVSURF) 
    sel.vars <- x$varselect.pred + 1 
    colnames(samples[,sel.vars]) 

    ptm <- proc.time()                # Start timer to calculate processing length 
    (rf.final_ntree501 = randomForest(samples[,"species_na"], x=samples[,sel.vars], 
         ntree=b, importance=TRUE, norm.votes=TRUE, proximity=TRUE)) # Run randomForest 

    ### PROBLEM HERE 
    cat(rf.final_ntree501,file=output_text,append=TRUE) 
    ### PROBLEM ENDS 

    cat("Processing time: ",proc.time() - ptm, "\n", file=output_text,append=TRUE)  # Stop timer 
    cat("Loop ends\n", file=output_text,append=TRUE) 
    } 
} 

通常あなただけの、次のような結果を印刷するために作成したランダムフォレストオブジェクト(rf.final_ntree501)の名前を書くことができます

Call: 
    randomForest(x = samples[, sel.vars], y = samples[, "species_na"],  ntree = b, importance = TRUE, proximity = TRUE, norm.votes = TRUE) 
      Type of random forest: classification 
       Number of trees: 501 
No. of variables tried at each split: 4 

    OOB estimate of error rate: 45.43% 
Confusion matrix: 
       Acacia mearnsii Cupressus lusitanica Eucalyptus sp. Euphorbia sp. Ficus sp. Grevillea robusta Maesa lanceolata other Persea americana class.error 
Acacia mearnsii     34     1    3    0   0     7    0 28    0 0.5342466 
Cupressus lusitanica    4     3    8    0   0    13    0 16    0 0.9318182 
Eucalyptus sp.      5     0    35    0   0    15    0  8    0 0.4444444 
Euphorbia sp.      0     0    1   16   0     2    0 15    0 0.5294118 
Ficus sp.       0     0    0    1   1     5    0 17    0 0.9583333 
Grevillea robusta     5     2    3    0   1    91    0 29    1 0.3106061 
Maesa lanceolata     4     0    0    0   0     2    0 14    0 1.0000000 
other        16     0    3    4   1    27    1 189    1 0.2190083 
Persea americana     5     1    0    0   0     6    0 33    1 0.9782609 

だから私は望みますこの情報をループ内のファイルに書き込んでください(「問題の一部」を参照)。私はそれがリストであるので、私はRFオブジェクトを直接書くことができないことを知っています。私がcatとrf.final_ntree501 $混乱で別々に混乱マトリックスを保存しようとすると。それは情報を保存しますが、行列の定式化を混乱させ、すべての情報をクラス名を除いた1行に置きます。

これを正しく処理する方法はありますか?代わりにcat()

乾杯、 ラミ

答えて

1

使用capture.output()ファイルには、コンソールに表示される方法を、結果を記述します。

# generate random data 
samples <- matrix(runif(675), ncol = 9) 
resp <- as.factor(sample(LETTERS[1:9], 75, replace = TRUE)) 

# random forest 
rf <- randomForest(x = samples, y = resp, ntree = 501, 
    importance = TRUE, norm.votes = TRUE, proximity = TRUE) 

# save desired information into a file 
capture.output(rf, file = output_text, append = TRUE) 

混乱行列を別に保存すると、write.table()を使用できます。結果は、選択されたセパレータ(この例ではタブ)を使用して機械可読形式にフォーマットされます。

write.table(rf$confusion, file = "filename.txt", sep = "\t") 
+0

これが問題を解決しました。ありがとうございます:) – RaimoGIS

+0

答えを受け入れることを検討してください:) – nya

+1

ああ、申し訳ありません:)。 – RaimoGIS

関連する問題