R/imror.R

Defines functions imror

Documented in imror

#'  IMROR
#'
#' Version of \code{\link[sdap]{imro}} for responded items. The imputation ratio measures the contribution of changed items to a final total. The numerator takes into account only initial respondents which are not structurally missing.
#' @author Beat Hulliger - Juan Berdugo
#' @param data (mandatory): A dataframe containing the data to be processed.
#' @param r1ij (mandatory): A matrix containing the response indicators for a given dataframe.
#' @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 r1ij 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), Indicator IMROR (imror).
#' @export


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

{

  #obsi <- 1:nrow(gij)
  #varj <- var.cas

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

  #Check existence of data

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


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

  #Check existence of r1ij

  if (missing(r1ij)) {
    cat("Missing r1ij!\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")
    break
  }

  #store the size of r1ij, bij and gij

  sizedata <- as.double(dim(data))
  sizer1ij <- as.double(dim(r1ij))
  sizebij <- as.double(dim(bij))
  sizegij <- as.double(dim(gij))

  #check if the sizes of bij and gij match

  sizeindicators<- (sizebij+sizer1ij+sizegij)/3

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

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


  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 <- sum(weight * (1-bij[obsi,varj]) * yij)
  if(denominator==0)
  {
    imror.value <- 0
    return(imror.value)
    break
  }

  # Calculate IMROR.

  imror.value <- sum(weight * r1ij[obsi,varj] *(1-bij[obsi,varj]) *gij[obsi,varj]* yij)
  imror.value <- imror.value / denominator
  imror.value <- list(variables = varj, observations = obsi, imror=imror.value )
  return(imror.value)



}

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.