R/impind.R

Defines functions impind

Documented in impind

#'  Imputation Indicator
#'
#' The imputation indicator function tests a dataframe vs a list of imputation indicators. The flags about the imputation are assumed already present on the dataframe. They may have different values while the output flag is binary.
#' @author Beat Hulliger, Juan Berdugo
#' @param data (mandatory): A dataframe containing the data to be processed.
#' @param imputedvars (mandatory): A vector with the column numbers of the variables that are imputed. For variable groups use the function \code{\link[sdap]{rind}}.
#' @param imputationflags (mandatory): A vector with the column numbers of the imputation flags corresponding to the imputed variables. To calculate the vector use the function \code{\link[sdap]{flaggroup}}.
#' @param gij (optional): A matrix "gij" containing imputation indicators for each variable and observation.
#' @param imp.ind (optional): A vector with the values considered as imputation indicators. Defaults: 1, 2, 3
#' @return A matrix "gij" containing imputation indicators for each variable and observation.
#' @export

impind <- function(data, imputedvars, imputationflags, gij, imp.ind = c(1,2,3))
{
  sizedata<- dim(data)
  n <- sizedata[1]
  p <- sizedata[2]


  # Check that the number of imputed varibles matches the number of imputation variables. They should be equal in order
  # to proceed.
  if (length(imputedvars) != length(imputationflags))
  {
    cat(paste("The number of imputed variablse and the number of imputation variables do not match." ))
    break
  }

  #Check if the dataset has enough columns
  if (p <= (length(imputedvars)+length(imputationflags)))
  {
    cat(paste("Dataset should contain at least", (length(imputedvars)+length(imputationflags)) ,"variables.", "Only ", p, "Variable(s) could be found.\n" ))
    break
  }

    #Create the gij matrix. If it already exists, the creation process is omitted.

  if (missing(gij))
    {
    gij <- !is.na(data)
    gij[gij==TRUE] <- FALSE
    cat("Argument gij not found. A new gij matrix has been created.\n")
    }

  #Check if previous gij matrix has the same size


  sizegij<- dim(gij)

  if (!identical(sizedata,sizegij))
  {
    gij <- matrix(0,nrow(data),ncol(data))
    storage.mode(gij) <- "integer"
    cat("Previously existing gij was deleted since the dataset does not match the size of gij.\n")
  }

  #Calculate gij. Looping over the variables and matching them to the imputation indicators.


  for (j in 1:length(imputationflags))
  {
    gij_variables<-(match(data[,imputationflags[j]],imp.ind, nomatch=0))
    gij_variables <- gij_variables>0
    gij[,imputedvars[j]]<- gij_variables
  }


  return(gij)
}

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.