R/frontier.R

Defines functions frontier

Documented in frontier

#' Find efficient frontier
#'
#' A predicate that is TRUE if a point is on the efficient frontier.
#'
#' @param ... coordinates to scan
#' @return logical vector, TRUE if point is on efficient frontier
#' 
#' @references \url{https://stackoverflow.com/a/36209989/986793}
#' @author \href{https://stackoverflow.com/users/986793/neal-fultz}{Neal Fultz}
#' 
#' @examples 
#' 
#' df <- data.frame(x=rnorm(100), y=rnorm(100))
#' plot(df)
#' points(subset(df, frontier(x,y)), col='red', pch=15)
#' points(subset(df, frontier(-x,y)), col='green', pch=15)
#' points(subset(df, frontier(x,-y)), col='blue', pch=15)
#' points(subset(df, frontier(-x,-y)), col='orange', pch=15)
#'  
#' @export




frontier <- function(...) {
  X <- list(...)

  i <- order(..., decreasing = TRUE)
  ret <- logical(length(i))
  
  for(z in X[-1]){
    i <- i[z[i] == cummax(z[i])]
  }
  
  
  ret[i] <- TRUE
  ret
}

Try the stackoverflow package in your browser

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

stackoverflow documentation built on Jan. 10, 2020, 9:07 a.m.