R/medianSamp.R

Defines functions medianSamp

Documented in medianSamp

#' Aggregation function for a ordinal variable
#'
#' The function medianSamp chooses the level as the median or randomly between
#' two levels.
#'
#' @param x ordered factor vector
#' @param weights numeric vector providing weights for the observations in x
#' @family imputation methods
#' @export


medianSamp <- function(x,weights = NULL){
  if(length(x)==1){
    return(x)
  }
  if(is.null(weights)){
    m <- median(as.numeric(x))
  }else{
    m <- weightedMedian(as.numeric(x), weights = weights)
  }
  mi <- floor(m)
  
  return(levels(x)[mi+sample(c(0,1), size = 1, prob = c(1-(m-mi),m-mi))])
}

Try the VIM package in your browser

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

VIM documentation built on Aug. 25, 2022, 5:07 p.m.