R/ci_bod_constr_bad.R

Defines functions .bad_extra_constr ci_bod_constr_bad

Documented in ci_bod_constr_bad

ci_bod_constr_bad <- function(x, indic_col, ngood=1, nbad=1, low_w=0, pref=NULL) 
{
  x_num = x[, indic_col]
  n_indic <- dim(as.matrix(x_num))[2]
  n_unit <- dim(as.matrix(x_num))[1]
  for (i in seq(1, n_indic)) {
    if (!is.numeric(x_num[, i])) {
      stop(paste("Data set not numeric at column:", i))
    }
  }
  for (i in seq(1, n_unit)) {
    for (j in seq(1, n_indic)) {
      if (is.na(x_num[i, j])) {
        message(paste("Pay attention: NA values at column:", 
                      i, ", row", j, ". Composite indicator has been computed, but results may be misleading, Please refer to OECD handbook, pg. 26."))
      }
    }
  }
  if(!is.null(pref)){
      if (!is.character(pref)) {
       stop(paste("The preference vector (pref) is not of character type"))
      }
  }
  if(n_indic!=(ngood+nbad)){
    stop(paste("The number of simple indicators is not equal to the sum of the
               number of good and bad outputs, please insert the right number
               of simple indicators in x or choose the right number of columns
               in indic_col or indicate the right number of good and bad outputs
               in ngood and nbad."))
    }
  if(ngood==0){
    stop(paste("The number of good outputs (ngood) has to be greater than 0."))
  }
  if(nbad==0){
    stop(paste("The number of bad outputs (nbad) has to be greater than 0."))
  }
  
  
  x_num = as.matrix(x_num)
  
  g <- ngood
  b <- nbad
  
  Avg <- apply(x_num, 2, mean)
  
  score <- rep(0, times = n_unit)
  w <- matrix(0, nrow = n_unit, ncol = n_indic)
  
  f.sign <- c(rep(-1, g), rep(1, b))
  
  # Constraints that do not change across units and draws (built once)
  extra <- .bad_extra_constr(Avg, colnames(x_num), low_w, pref)
  
  # All the units are in the reference set: this block is fixed as well
  Constr2 <- t(f.sign * t(x_num))
  f.con_fixed <- rbind(Constr2, extra)
  f.dir <- c("==", rep(">=", times = nrow(f.con_fixed)))
  f.rhs <- c(1, rep(0, times = nrow(f.con_fixed)))
  
  for (i in 1:n_unit) {
    f.obj <- f.sign * x_num[i, ]
    jj <- lp("min", f.obj, rbind(x_num[i, ], f.con_fixed), f.dir, f.rhs)
    score[i] <- jj$objval
    w[i, ] <- jj$solution
  }
  eff <- (1/(1+score))
  
  colnames(w) <- colnames(x_num)
  
  # Write the composite scores and the indicator target values in one result matrix
  
  target <- matrix(nrow=n_unit,ncol=(b+g)) 
  for(j in 1:g)
  {
  target[,j] <- x_num[,j] + (Avg[j]*score)
  }
  for(j in (g+1):(g+b))
  {
    target[,j] <- x_num[,j] - (Avg[j]*score)
  }
  colnames(target) <- colnames(x_num)
  
  r <- list(ci_bod_constr_bad_est = eff, ci_bod_constr_bad_weights = w, 
            ci_bod_constr_bad_target = target, ci_method = "bod_constr_bad")
  r$call <- match.call()
  class(r) <- "CI"
  r
}


# Internal (not exported): constant constraint block shared by ci_bod_constr_bad,
# ci_rbod_constr_bad and ci_rbod_constr_bad_Q. It does not depend on the unit
# under evaluation nor on the bootstrap draw, so it is built only once.
# Rows (all of type ">= 0"): non-negativity of the weights, VWR lower bounds,
# ordinal VWR of type 1 (most preferred >= all others) and, when pref has more
# than two elements, ordinal VWR of type 2 (intermediate ones >= least preferred).
.bad_extra_constr <- function(Avg, x_names, low_w, pref)
{
  n_indic <- length(Avg)
  Pweight <- diag(1, n_indic)
  I <- diag(x = 1, n_indic)
  VWRg <- t((I - low_w)*as.vector(Avg))
  extra <- rbind(Pweight, VWRg)
  
  p <- length(pref)
  if (p > 0)
  {
    if (p < 2) {
      stop("The preference vector (pref) must contain at least two simple indicators.")
    }
    if (!all(pref %in% x_names)) {
      stop("All the elements of pref must be column names of the simple indicators.")
    }
    
    ### OrdVWR_type1
    I1 <- diag(x = 1, n_indic)
    colnames(I1) <- x_names
    I1[,pref[1]] <- 0
    M_imp <- matrix(0, nrow=n_indic, ncol=n_indic)
    colnames(M_imp) <- x_names
    M_imp[,pref[1]] <- 1
    ordVWR_type1 <- (-I1*Avg) + (M_imp*Avg[pref[1]])
    ordVWR_type1 <- ordVWR_type1[x_names != pref[1], , drop = FALSE]
    extra <- rbind(extra, ordVWR_type1)
    
    ### OrdVWR_type2
    if (p > 2)
    {
      I2 <- diag(x = 1, n_indic)
      colnames(I2) <- x_names
      I2[,pref[p]] <- 0
      M_imp2 <- matrix(0, nrow=n_indic, ncol=n_indic)
      colnames(M_imp2) <- x_names
      M_imp2[,pref[p]] <- -Avg[pref[p]]
      ordVWR_type2 <- (I2*Avg) + M_imp2
      keep <- x_names %in% pref & x_names != pref[1] & x_names != pref[p]
      extra <- rbind(extra, ordVWR_type2[keep, , drop = FALSE])
    }
  }
  unname(extra)
}

Try the Compind package in your browser

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

Compind documentation built on Nov. 19, 2026, 5:11 p.m.