R/supervised.pca.R

Defines functions supervised.pca

Documented in supervised.pca

supervised.pca <- function(target, dataset, indices, center = TRUE, scale = TRUE, colours = NULL, graph = TRUE) {
  
  mod.all <- prcomp(dataset, center = center, scale = scale)
  mod.sel <- prcomp(dataset[, indices], center = center, scale = scale)
  rat <- sum(mod.sel$sdev^2) / sum(mod.all$sdev^2)
  
  if ( graph ) {
    if ( is.null(colours) )  target <- as.numeric( as.factor(target) )
    plot( mod.all$x[, 1:2], col = target, main = "Scores using all variables" )
    dev.new()
    plot( mod.sel$x[, 1:2], col = target, main = "Scores using the selected variables" )
  }
  
  list(mod.all = mod.all, mode.sel = mod.sel, var.percent = rat)
}

Try the MXM package in your browser

Any scripts or data that you put into this service are public.

MXM documentation built on Aug. 25, 2022, 9:05 a.m.