R/fairreviewers.R

init <- function(ratings) {
  env <- new.env()
  env$rating_matrix <- data.matrix(ratings)

  # Bind computations to separate isolated environments
  env$arithmetic_strictness_env <- arithmetic_strictness_env(env$rating_matrix)
  env$expected_strictness_env   <- expected_strictness_env(env$rating_matrix)

  # Load back results from computations: These vectors
  # contain the strictness factors
  env$arithmetic_strictness_vector <- env$arithmetic_strictness_env$arithmetic_strictness_vector
  env$expected_strictness_vector   <- env$expected_strictness_env$expected_strictness_vector
  env$document_means               <- env$arithmetic_strictness_env$doc_means

  # Correct rows by arithmetic strictness
  arithmetic_correction_of_doc_means <- apply(env$rating_matrix, 1, function(row) {
    mean(env$arithmetic_strictness_vector * row, na.rm = TRUE)
  })

  # Correct by multiplying points with expected_strictness factors
  expected_strictness_correction <- apply(env$rating_matrix, 1, function(row) {
    mean(env$expected_strictness_vector * row, na.rm = TRUE)
  })

  env$strictness <- cbind("Arithmetic Strictness" = env$arithmetic_strictness_vector,
                          "Expected Strictness" = env$expected_strictness_vector)

  env$result     <- cbind(env$rating_matrix,
                          "Mean" = round(env$document_means, 2),
                          "Rating after AS correction" = round(arithmetic_correction_of_doc_means, 2),
                          "Rating after ES correction" = round(expected_strictness_correction, 2))

  # Return the set of results packaged
  # in an environment
  env
}
neumanrq/fairreviewers documentation built on May 24, 2019, 5:06 a.m.