R/equating.R

#' @title Delta Plot Equating
#'
#' @description Performs Delta Equating
#'
#' @details
#' Does equating things. Delta?
#'
#' @param data is something
#' @param items is something else
#' @param new is something else
#' @param reference is something else
#' @param remove is something
#' @param output.directory think this is the output directory
#' @return Equating things
#' @examples delta.plot.equating(data = eq.dat, items = eq.item.attribute.dat, new = new.form, reference = reference.form, remove = Delta, output.directory = output.dir)
#' @family Equating
delta.plot.equating <- function (data = eq.dat, items = eq.item.attribute.dat, new = new.form, reference = reference.form, remove = Delta, output.directory = output.dir)
{
  ##Require package for delta plotting and data management
  require(deltaPlotR)
  require(reshape2)
  #Check if variables defined
  if ((is.na(new) | new == '') | (is.na(reference) | reference == '')){

    warning(' Please verify that you have defined the new argument and the reference argument')

    stop()

  } else {
    if ((inherits(data, "data.frame")==TRUE) & (inherits(items, "data.frame")==TRUE)){

      item.x<- unique(items$ItemId)
      new.x <- ifelse(item.x %in% items$ItemId[ items$Form == new], 1, 0)
      reference.x <- ifelse(item.x %in% items$ItemId[ items$Form == reference], 1, 0)
      out.dat <- data.frame(item.x,new.x,reference.x, stringsAsFactors = FALSE)
      names(out.dat)<- c("Item",new,reference)

      common <- intersect(items$ItemId[items$Form == reference], items$ItemId[items$Form == new])

      if (remove == "Yes"){


        data <- subset(data, ItemName %in% common, select = c("ClientCandidateId","Form","ItemName","ItemScore"))

        data <- dcast(data, ClientCandidateId + Form ~ ItemName, value.var = "ItemScore")

        out.data <- deltaPlot(data = data[,-1], type = "response", group = "Form",
                              focal.name = new, thr = "norm", alpha = 0.01, extreme = "constraint", save.output = TRUE,
                              output = c("Delta Plot Summary", output.directory))
        diagPlot(out.data, xlab = reference, ylab = new, save.plot = TRUE, save.options = c("Delta Plot", output.directory, "pdf"))

        if (length(out.data$DIFitems) > 0){

          common.out <- common[out.data$DIFitems]

          out.dat$delta_flag <- ifelse(out.dat$Item %in% common.out, 1, 0)

          cat("Items removed by delta plot:", common.out , "\n")

          write.csv(out.dat, file = paste(output.directory,"/Items flagged by delta plot.csv", sep = ""), row.names = FALSE)

        } else {

          warning("No items were removed by delta plot flagging")

        }

      } else {

        warning("No items were removed by delta plot flagging")
      }
    } else {
      warning('Please verify that object data is a valid dataframe')

      stop()
    }
  }

  on.exit(dev.off())
}
m070ch/ips.tools documentation built on May 18, 2019, 8:09 p.m.