R/irr.R

Defines functions irr

Documented in irr

#'  Item Response Rate
#'
#' Item response rate calculates the proportion of responses for a given matrix of response indicators rij.
#' @author Beat Hulliger, Juan Berdugo
#' @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 misingness indicators matrix.
#' @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 Proportion of responses for a given matrix of response indicators rij.
#' @export

irr <- function(rij,bij,obsi=1:nrow(rij),varj=1:ncol(rij),weight)
  # Beat Hulliger, Juan Berdugo
  # 3.7.2015, 6.8.2015, 19.8.2015
  # Indicator IRR
  # rij: the response matrix
  # bij: the structural missingness matrix
  # obsi: a vector of the row numbers
  # varj: a vector of the column numbers
  # weight: an optional 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)
  # check whether the structural missingness indicator is given as an argument
  if (missing(bij))
  {
    cat("Missing bij!\n")
    if (p>1) irr.i <- apply(rij[obsi,varj],1,mean)
    else irr.i <- rij[obsi,varj]
    irr.value <- weighted.mean(irr.i,w=weight)
    return(irr.value)
  }
  else
  {
    max_rij_bij <- pmax(rij,bij)
    if (p>1) irr.i <- apply(max_rij_bij[obsi,varj],1,mean)
    else irr.i <- max_rij_bij[obsi,varj]
    irr.value <- weighted.mean(irr.i,w=weight)
    return(irr.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.