R/linear_transform.R

##' applies a linear max-min curve to grades
##' @description Curves according to a linear transformation, from a set minimum to a set maximum.
##' @param g Grades_list object
##' @param min.act The minium grade in the raw (actual) dataset
##' @param min.adj The minimum grade in adjusted dataset
##' @param max.act The maximum grade in the raw (actual) dataset
##' @param max.adj The maximum grade in the adjusted dataset
##' @return Grades list with adjusted grades column
##' @export
##'
#linear_transform <- function(grades.raw, min.target=NULL, max.target=NULL) {
linear_transform <- function(g,
                             min.act = NULL, # sets to minimum non-zero value in the actual grade set
                             max.act = NULL, # sets to maximum value (<= max.adj) in the actual grade set
                             min.adj = 70,
                             max.adj = 100) {
  # Should probably check for adjusted grades, or something

  # id.col, raw.grades.col, adj.grades.col
  #
  id.col <- attr(g, "id.col")
  raw.col <- attr(g, "raw.grades.col")
  adj.col <- attr(g, "the.adj.col")
  if(is.null(adj.col)) {
    adj.col <- rlang::enquo(adj.grades.col)
  }

  # browser()
  # assign default values for minimum and maximum actual
  if(is.null(min.act)) {
    min.act <- g %>%
      filter(UQ(raw.col) > 0) %>%
      select(UQ(raw.col)) %>%
      min(na.rm=TRUE)
  }

  # Make sure min.act <= min.adj so you can't curve grades down
  if(min.act > min.adj) {
    min.act <- min.adj
  }

  # Set max.act as the maximum of the dataset
  if(is.null(max.act)) {
    max.act <- g %>%
      dplyr::select(UQ(raw.col)) %>%
      max(na.rm=TRUE)
  }


  # Perform the curve
  # via [Dickson college math professor website]
  # f(x) = y0 + (y1-y0)/(x1-x0) * (x-x0)
  # x1 and x0 are the raw reference scores; y1 and y0 are the adjusted referecne score
  g <- dplyr::mutate(g, grades.adj = (min.adj + (max.adj - min.adj)/(max.act - min.act) * (UQ(raw.col) - min.act) )) #(UQ(raw.col) - min.act))

  # Set the adjusted grades column
  attr(g, "adj.grades.col") <- quo(grades.adj)

  g
}
adsteen/gradr documentation built on May 10, 2019, 7:26 a.m.