将 R 随机森林结果写入文件

Writing R random forest results to a file

我正在为我的数据集多次编写 运行 随机森林 class 化的 R 脚本。我希望使用至少 10 运行s 的平均值来获得更可靠的结果。所以我有这个函数和 for 循环,它是 运行ning Random Forest classifier 我希望的次数(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 对象,因为它是一个列表。如果我尝试用 rf.final_ntree501$confusion 和 cat 分别保存混淆矩阵。它会保存信息,但会弄乱矩阵的公式并将所有信息放在一行中,不包括 class 名称。

有没有人知道如何正确处理这个问题?

干杯, 拉米

使用 capture.output() 而不是 cat() 将结果按照在控制台中显示的方式写入文件。

# 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")