I am trying to save the classification success rates of the CAPdiscrim
function from the BiodiversityR
package. The vignette for CAPdiscrim
(https://www.rdocumentation.org/packages/BiodiversityR/versions/2.7-2/topics/CAPdiscrim) gives an example on how to obtain the classification success rates:
library(BiodiversityR)
library(vegan)
library(MASS)
data(dune)
data(dune.env)
for (mseq in 1:14) {
CAPdiscrim.result <- CAPdiscrim(dune~Management, data=dune.env,
dist="bray", axes=2, m=mseq)
}
This automatically prints the classification success percentages in the console eg.
Overall classification success: 40 percent
BF (n=3) correct: 0 percent
HF (n=5) correct: 40 percent
NM (n=6) correct: 33.3333333333333 percent
SF (n=6) correct: 66.6666666666667 percent
However when calling the CAPdiscrim.result
object outside the loop, it produces the actual CAPdiscrim
results eg (str(CAPdiscrim.result
)).
List of 14
$ PCoA : num [1:20, 1:2] -0.3547 -0.2946 -0.0728 -0.0693 -0.3071 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:20] "1" "2" "3" "4" ...
.. ..$ : NULL
$ m : int 14
$ tot : num 4.3
$ varm : num 107
$ group : Factor w/ 4 levels "BF","HF","NM",..: 4 1 4 4 2 2 2 2 2 1 ...
$ CV : Factor w/ 4 levels "BF","HF","NM",..: 1 2 4 4 3 2 2 1 1 2 ...
$ percent : num 40
$ x : num [1:20, 1:3] 7.64 0.18 9.43 8.88 -1.93 ...
etc. etc.
I feel I have tried everything to save the exact output that is printed in the console at the time of running the for loop
. I have tried creating empty list
s, empty data.frame
s, binding the results. I just CANNOT figure out how to store it! Any help would be greatly appreciated.
Those strings are being written out to the console via cat()
during the function call. If you want to grab those values, use capture.output
. For example
myout<-lapply(1:14, function(mseq) {
msg <- capture.output(
CAPdiscrim.result <- CAPdiscrim(dune~Management, data=dune.env,
dist="bray", axes=2, m=mseq),
type="output")
list(msg=msg, result=CAPdiscrim.result)
})
This will capture the results and the strings in the list. You can get at the strings with
myout[[1]]$msg
for example.