File: MDSplot.R

package info (click to toggle)
r-cran-randomforest 4.7-1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 496 kB
  • sloc: ansic: 1,897; fortran: 366; makefile: 2
file content (21 lines) | stat: -rw-r--r-- 865 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
MDSplot <- function(rf, fac, k=2, palette=NULL, pch=20, ...) {
    if (!inherits(rf, "randomForest")) 
        stop(deparse(substitute(rf)), " must be a randomForest object")
    if(is.null(rf$proximity)) 
        stop(deparse(substitute(rf)), " does not contain a proximity matrix")
    op <- par(pty="s")
    on.exit(par(op))
    rf.mds <- stats::cmdscale(1 - rf$proximity, eig=TRUE, k=k)
    colnames(rf.mds$points) <- paste("Dim", 1:k)
    nlevs <- nlevels(fac)
    if (is.null(palette)) {
        palette <- if (requireNamespace("RColorBrewer", quietly = TRUE) && nlevs < 12)
            RColorBrewer::brewer.pal(nlevs, "Set1") else rainbow(nlevs)
    }
    if (k <= 2) {
        plot(rf.mds$points, col=palette[as.numeric(fac)], pch=pch, ...)
    } else {
        pairs(rf.mds$points, col=palette[as.numeric(fac)], pch=pch, ...)
    }
    invisible(rf.mds)
}