R/imro.R

Defines functions imro

Documented in imro

#'  Imputation Ratio IMRO
#'
#' The imputation ratio measures the contribution of changed items to a final total. The numerator takes into account all values including the initial non-respondents which are not structurally missing.
#' @author Beat Hulliger - Juan Berdugo
#' @param data (mandatory): A dataframe containing the data to be processed.
#' @param bij (optional): A matrix containing the structurally missingness indicators. bij can be calculated using the function \code{\link[sdap]{smind}}. If the argument bij is missing, the indicator irr is calculated without considering a misingness indicators matrix.
#' @param gij (mandatory): A matrix containing the imputation indicators for a given dataframe. gij can be calculated using the function \code{\link[sdap]{impind}}.
#' @param obsi (optional): A vector with the observations in rij to to be processed. If the argument obs is missing, all observations are processed.
#' @param varj (optional): A vector with the variables (column numbers) to be considered for the calculation. If the argument varj is missing, all variables are considered for the indicator.
#' @param weight (optional): A vector of weights to be considered when calculating the indicator. If no weight vector is given as an argument, the indicator is calculated without considering different weights.
#' @return A list with the following elements: variables (variables), observations (observations), Number of imputations detected (imputations), Indicator IMRO (imro).
#' @export


imro <- function(data,bij,gij,obsi=1:nrow(data),varj=1:ncol(data),weight)

{


  n <- length(obsi)
  p <- length(varj)

  if (missing(weight)) weight <- rep(1,n)

  #Check existence of data

  if (missing(data)) {
    cat("Missing dataset!\n")
    break
  }

  #Check existence of gij

  if (missing(gij)) {
    cat("Missing gij!\n")
    break
  }

  #Check existence of bij

  if (missing(bij)) {
    cat("Missing bij!\n")
    }

  #store the size of bij and gij

  sizebij <- as.double(dim(bij[obsi,varj]))
  sizegij <- as.double(dim(gij[obsi,varj]))
  sizedata <- as.double(dim(data[obsi,varj]))

  sizeindicators<- (sizebij+sizegij)/2

  #check if the sizes of bij, gij and data match



  if (identical(sizeindicators,sizedata)==FALSE)
  {
    print("The sizes of bij, gij and data do not match. Please recalculate bij and/or gij.")
    break
  }else
  {
    print("Datasets sizes ok")
  }



  #prepare part of the dataframe for matrix operations (yij)

  yij <- as.matrix(data[obsi,varj])
  class(yij) <- "numeric"
  yij[is.na(yij)] <- 0



  # Calculate denominator of the function. If it is zero, return zero and break.

  denominator <- weighted.mean(apply(((1-bij[obsi,varj])*yij),1,sum),w=weight)


  if(denominator==0)
  {
    imro.value <- 0
    return(imro.value)
    break
  }

  # Calculate IMRO.

  imro.value = weighted.mean(apply(((1-bij[obsi,varj])*gij[obsi,varj]*yij),1,sum),w=weight)
  imro.value = imro.value / denominator

  imrolist<- list(variables = varj, observations = obsi, imputations=sum(gij[obsi,varj]), imro=imro.value )


  return(imrolist)

}

Try the sdap package in your browser

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

sdap documentation built on May 2, 2019, 6:52 p.m.