R/utils.r

Defines functions random_logfile prob_to_score score_to_prob response_name_from_formula response_var_from_data feature_matrix_from_data

feature_matrix_from_data <- function(df, formula) {
  x <- model.matrix(formula, df)
  # intercept name
  colnames(x)[1] <- "[intercept]"

  # fix cases when colnames are "fixed" by model.matrix
  colnames(x) <- gsub("^`|`$", "", colnames(x))

  # return matrix
  x
}

response_var_from_data <- function(df, formula) {
  y_name <- response_name_from_formula(formula)
  y <- df[[y_name]]
  stopifnot(all(y %in% c(-1L, 1L)))
  y
}

response_name_from_formula <- function(formula) {
  all.vars(formula)[1]
}

score_to_prob <- function(score) {
  1/(1 + exp(-score))
}

prob_to_score <- function(prob) {
  -log(1/prob - 1)
}

random_logfile <- function() {
  tempfile(pattern = "riskslimr-", fileext = ".log")
}
zamorarr/riskslimr documentation built on Sept. 2, 2021, 7:38 p.m.