From 17fd891da93abb8d457d7daf4a0823774eac01dc Mon Sep 17 00:00:00 2001 From: huizezhang-sherry Date: Thu, 5 Sep 2024 00:12:39 +0200 Subject: [PATCH] fix checks --- DESCRIPTION | 2 +- NAMESPACE | 2 + R/interesting-indices.r | 1 + R/search-jellyfish.R | 31 +++++++++-- R/tour-guided.r | 1 + R/util.r | 2 + man/anomaly_index.Rd | 12 ++++ man/guided_anomaly_tour.Rd | 67 +++++++++++++++++++++++ man/guided_tour.Rd | 2 + man/{search_jellyfish.Rd => jellyfish.Rd} | 18 ++++-- man/mahal_dist.Rd | 18 ++++++ 11 files changed, 147 insertions(+), 9 deletions(-) create mode 100644 man/anomaly_index.Rd create mode 100644 man/guided_anomaly_tour.Rd rename man/{search_jellyfish.Rd => jellyfish.Rd} (50%) create mode 100644 man/mahal_dist.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 30e3be8b..6f5af5c7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -41,6 +41,6 @@ License: MIT + file LICENSE LazyData: true URL: https://github.com/ggobi/tourr BugReports: https://github.com/ggobi/tourr/issues -RoxygenNote: 7.3.1 +RoxygenNote: 7.3.2 Encoding: UTF-8 VignetteBuilder: knitr diff --git a/NAMESPACE b/NAMESPACE index 133de2a4..75d9b2c1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -142,12 +142,14 @@ importFrom(graphics,rect) importFrom(graphics,segments) importFrom(graphics,stars) importFrom(graphics,text) +importFrom(stats,loess) importFrom(stats,mahalanobis) importFrom(stats,na.omit) importFrom(stats,qchisq) importFrom(stats,quantile) importFrom(stats,residuals) importFrom(stats,rnorm) +importFrom(stats,runif) importFrom(stats,var) importFrom(utils,globalVariables) importFrom(utils,tail) diff --git a/R/interesting-indices.r b/R/interesting-indices.r index e9b81479..65518d75 100644 --- a/R/interesting-indices.r +++ b/R/interesting-indices.r @@ -84,6 +84,7 @@ splines2d <- function() { } #' @rdname spline-loess +#' @importFrom stats loess #' @export loess2d <- function() { function(mat) { diff --git a/R/search-jellyfish.R b/R/search-jellyfish.R index 36e14664..44a45403 100644 --- a/R/search-jellyfish.R +++ b/R/search-jellyfish.R @@ -1,4 +1,4 @@ -#' An jellyfish optimisers for the projection pursuit guided tour +#' A jellyfish optimiser for projection pursuit guided tour #' #' @param current starting projection, a list of basis of class "multi-bases" #' @param index index function @@ -6,10 +6,15 @@ #' @param max.tries the maximum number of iteration before giving up #' @param ... other arguments being passed into the \code{search_jellyfish()} #' @keywords optimize +#' @importFrom stats runif +#' @rdname jellyfish #' @export #' @examples -#' res <- animate_xy(flea[, 1:6], guided_tour(holes(), search_f = search_jellyfish)) -#' res +#' library(dplyr) +#' res <- animate_xy(flea[, 1:6], guided_tour(lda_pp(cl = flea$species), +#' search_f = search_jellyfish)) +#' bases <- res |> filter(loop == 1) |> pull(basis) |> check_dup(0.1) +#' animate_xy(data = flea[,1:6], tour_path = planned_tour(bases), col = flea$species) search_jellyfish <- function(current, index, tries, max.tries = 50, ...) { rcd_env <- parent.frame(n = 4) if (is.null(rcd_env[["record"]])) rcd_env <- parent.frame(n = 1) @@ -44,7 +49,7 @@ search_jellyfish <- function(current, index, tries, max.tries = 50, ...) { target = lapply(current, function(i) {update_typeB(i, current)}) } - target <- purrr::map2(current, target, correct_orientation) + target <- mapply(correct_orientation, current, target) target_idx <- sapply(target, index) # if the target is worse than current, use current @@ -81,4 +86,22 @@ search_jellyfish <- function(current, index, tries, max.tries = 50, ...) { } } +#' @param bases a list of bases extracted from the data collection object, see examples +#' @param min_dist the minimum distance between two bases +#' @rdname jellyfish +check_dup <- function(bases, min_dist) { + res <- list() + res[[1]] <- bases[[1]] + i <- 1; j <- 2 + while(j <= length(bases)){ + if (proj_dist(bases[[i]], bases[[j]]) >= min_dist) { + res <- c(res, list(bases[[j]])) + i <- i + 1; j <- j + 1 + } else{ + j <- j + 1 + } + } + return(res) +} +globalVariables(c("loop")) diff --git a/R/tour-guided.r b/R/tour-guided.r index 25fbe3cb..a3a4e1d7 100644 --- a/R/tour-guided.r +++ b/R/tour-guided.r @@ -21,6 +21,7 @@ #' a better projection before giving up #' @param max.i the maximum index value, stop search if a larger value is found #' @param n_sample number of samples to generate if \code{search_f} is \code{\link{search_polish}} +#' @param n_jellies only used for \code{search_jellyfish}, the number of jellyfish to use #' @param ... arguments sent to the search_f #' @seealso \code{\link{cmass}}, \code{\link{holes}} and \code{\link{lda_pp}} #' for examples of index functions. The function should take a numeric diff --git a/R/util.r b/R/util.r index e2750d10..468de741 100644 --- a/R/util.r +++ b/R/util.r @@ -162,3 +162,5 @@ mapShapes <- function(x, shapeset) { shapes <- shapeset shapes[as.numeric(x)] } + + diff --git a/man/anomaly_index.Rd b/man/anomaly_index.Rd new file mode 100644 index 00000000..299c5b01 --- /dev/null +++ b/man/anomaly_index.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/anomaly-pursuit.r +\name{anomaly_index} +\alias{anomaly_index} +\title{Anomaly index.} +\usage{ +anomaly_index() +} +\description{ +Calculates an index that looks for the best projection of +observations that are outside a pre-determined p-D ellipse. +} diff --git a/man/guided_anomaly_tour.Rd b/man/guided_anomaly_tour.Rd new file mode 100644 index 00000000..a9e81f27 --- /dev/null +++ b/man/guided_anomaly_tour.Rd @@ -0,0 +1,67 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/tour-guided-anomaly.r +\name{guided_anomaly_tour} +\alias{guided_anomaly_tour} +\title{A guided anomaly tour path.} +\usage{ +guided_anomaly_tour( + index_f, + d = 2, + alpha = 0.5, + cooling = 0.99, + max.tries = 25, + max.i = Inf, + ellipse, + ellc = NULL, + ellmu = NULL, + search_f = search_geodesic, + ... +) +} +\arguments{ +\item{index_f}{the section pursuit index function to optimise. The function +needs to take two arguments, the projected data, indexes of anomalies.} + +\item{d}{target dimensionality} + +\item{alpha}{the initial size of the search window, in radians} + +\item{cooling}{the amount the size of the search window should be adjusted +by after each step} + +\item{max.tries}{the maximum number of unsuccessful attempts to find +a better projection before giving up} + +\item{max.i}{the maximum index value, stop search if a larger value is found} + +\item{ellipse}{pxp variance-covariance matrix defining ellipse, default NULL. +Useful for comparing data with some hypothesized null.} + +\item{ellc}{This can be considered the equivalent of a critical value, used to +scale the ellipse larger or smaller to capture more or fewer anomalies. Default 3.} + +\item{ellmu}{This is the centre of the ellipse corresponding to the mean of the +normal population. Default vector of 0's} + +\item{search_f}{the search strategy to use} + +\item{...}{arguments sent to the search_f} +} +\description{ +The guided anomaly tour is a variation of the guided tour that is +using an ellipse to determine anomalies on which to select target planes. +} +\details{ +Usually, you will not call this function directly, but will pass it to +a method that works with tour paths like \code{\link{animate_slice}}, +\code{\link{save_history}} or \code{\link{render}}. +} +\examples{ +animate_xy(flea[, 1:6], guided_anomaly_tour(anomaly_index(), + ellipse=cov(flea[,1:6])), ellipse=cov(flea[,1:6]), axes="off") +} +\seealso{ +\code{\link{slice_index}} for an example of an index functions. +\code{\link{search_geodesic}}, \code{\link{search_better}}, + \code{\link{search_better_random}} for different search strategies +} diff --git a/man/guided_tour.Rd b/man/guided_tour.Rd index 83375437..6ea88cb2 100644 --- a/man/guided_tour.Rd +++ b/man/guided_tour.Rd @@ -33,6 +33,8 @@ a better projection before giving up} \item{search_f}{the search strategy to use: \code{\link{search_geodesic}}, \code{\link{search_better}}, \code{\link{search_better_random}}, \code{\link{search_polish}}. Default is \code{\link{search_geodesic}}.} +\item{n_jellies}{only used for \code{search_jellyfish}, the number of jellyfish to use} + \item{n_sample}{number of samples to generate if \code{search_f} is \code{\link{search_polish}}} \item{alpha}{the initial size of the search window, in radians} diff --git a/man/search_jellyfish.Rd b/man/jellyfish.Rd similarity index 50% rename from man/search_jellyfish.Rd rename to man/jellyfish.Rd index c4fec8d1..0307cfa0 100644 --- a/man/search_jellyfish.Rd +++ b/man/jellyfish.Rd @@ -2,9 +2,12 @@ % Please edit documentation in R/search-jellyfish.R \name{search_jellyfish} \alias{search_jellyfish} -\title{An jellyfish optimisers for the projection pursuit guided tour} +\alias{check_dup} +\title{A jellyfish optimiser for projection pursuit guided tour} \usage{ search_jellyfish(current, index, tries, max.tries = 50, ...) + +check_dup(bases, min_dist) } \arguments{ \item{current}{starting projection, a list of basis of class "multi-bases"} @@ -16,12 +19,19 @@ search_jellyfish(current, index, tries, max.tries = 50, ...) \item{max.tries}{the maximum number of iteration before giving up} \item{...}{other arguments being passed into the \code{search_jellyfish()}} + +\item{bases}{a list of bases extracted from the data collection object, see examples} + +\item{min_dist}{the minimum distance between two bases} } \description{ -An jellyfish optimisers for the projection pursuit guided tour +A jellyfish optimiser for projection pursuit guided tour } \examples{ -res <- animate_xy(flea[, 1:6], guided_tour(holes(), search_f = search_jellyfish)) -res +library(dplyr) +res <- animate_xy(flea[, 1:6], guided_tour(lda_pp(cl = flea$species), +search_f = search_jellyfish)) +bases <- res |> filter(loop == 1) |> pull(basis) |> check_dup(0.1) +animate_xy(data = flea[,1:6], tour_path = planned_tour(bases), col = flea$species) } \keyword{optimize} diff --git a/man/mahal_dist.Rd b/man/mahal_dist.Rd new file mode 100644 index 00000000..7a67a42b --- /dev/null +++ b/man/mahal_dist.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/linear-algebra.r +\name{mahal_dist} +\alias{mahal_dist} +\title{Calculate the Mahalanobis distance between points and center.} +\usage{ +mahal_dist(x, vc) +} +\arguments{ +\item{x}{matrix of data} + +\item{vc}{pre-determined variance-covariance matrix} +} +\description{ +Computes the Mahalanobis distance using a provided variance-covariance +matrix of observations from 0. +} +\keyword{algebra}