R/urr.R

Defines functions urr

Documented in urr

#'  Unit Response Rate
#'
#' Unit response rate calculates the proportion of data which can actually be used for analysis.
#' @author Beat Hulliger
#' @param rij (mandatory): A matrix containing the response indicators for a given dataframe. rij can be calculated using the function \code{\link[sdap]{rind}}.
#' @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 missingness indicators matrix.
#' @param obsi (optional): A vector with the observations in rij 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 Proportion of data which can actually be used for analysis.
#' @export


urr <- function(rij,bij,obsi=1:nrow(rij),varj=1:ncol(rij),weight)

{
  n <- length(obsi)
  if (n<=1) cat(paste("Warning: Number of obs is ",n,"\n"))
  p <- length(varj)
  if (missing(weight)) weight <- rep(1,n)
  if (missing(rij)) {
    cat("Missing rij!\n")
    return()
  }

  if (missing(bij))
  {
    cat("Missing bij!\n")
    if (p>1) ri <- 1-apply(1-rij[obsi,varj],1,prod) else
      ri <- rij[obsi,varj] # 1-row products (1-r_ij)
    urr.value <- weighted.mean(ri,weight)
    return(urr.value)
  } else
  {
    rijtemp <- pmax(as.matrix(rij[obsi,varj], mode = "integer"),as.matrix(bij[obsi,varj], mode = "integer"))
    if (p>1) ri <- 1-apply(1-rijtemp,1,prod) else
      ri <- rijtemp # 1-row products (1-r_ij)
    rm(rijtemp)
    urr.value <- weighted.mean(ri,weight)
    return(urr.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.