1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#' 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))])
}
|