R/l_getProjection.R

Defines functions l_getProjection.l_compound l_getProjection.l_tour_compound l_getProjection.l_tour l_getProjection.l_serialaxes l_getProjection.l_plot l_getProjection.l_hist l_getProjection

Documented in l_getProjection

#' @title Query the matrix of projection vectors
#' @param target A object returned by \code{l_tour}
#' @param data Original data set
#' @return a matrix of projection vectors
#' @export
#' @examples
#' if(interactive()) {
#'   dat <- iris[,-5]
#'   p <- l_tour(dat, color = iris$Species,
#'               as.l_tour = FALSE)
#'   # scroll the bar
#'   proj <- l_getProjection(p, dat)
#'   projected_object <- as.matrix(dat) %*% proj
#'   # it will not be exactly the same
#'   plot(projected_object[,1], projected_object[,2],
#'        col = hex12tohex6(p['color']))
#' }
l_getProjection <- function(target, data) {
  UseMethod("l_getProjection", target)
}

#' @export
l_getProjection.l_hist <- function(target, data) {

  inv_data <- MASS::ginv(as.matrix(data))
  proj <- inv_data %*% matrix(c(target['x']), ncol = 1)

  # divide the norm
  apply(proj, MARGIN = 2, function(x) {x/sqrt(sum(x^2))})
}

#' @export
l_getProjection.l_plot <- function(target, data) {

  inv_data <- MASS::ginv(as.matrix(data))
  proj <- inv_data %*% matrix(c(target['x'], target['y']), ncol = 2)

  # divide the norm
  apply(proj, MARGIN = 2, function(x) {x/sqrt(sum(x^2))})
}

#' @export
l_getProjection.l_serialaxes <- function(target, data) {

  inv_data <- MASS::ginv(as.matrix(data))
  proj <- inv_data %*% as.matrix(char2num.data.frame(target['data']))

  # divide the norm
  apply(proj, MARGIN = 2, function(x) {x/sqrt(sum(x^2))})
}

#' @export
l_getProjection.l_tour <- function(target, data) {target['projection']}

#' @export
l_getProjection.l_tour_compound <- function(target, data) {target['projection']}

#' @export
l_getProjection.l_compound <- function(target, data) {

  lapply(target,
         function(t) {
           l_getProjection(t, data)
         })
}

Try the loon.tourr package in your browser

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

loon.tourr documentation built on Oct. 27, 2021, 5:09 p.m.