R/convert_model_dots.R

Defines functions convert_model_dots

Documented in convert_model_dots

#'@title Converts dot operator to terms
#'
#'@description Converts the dot operator `.` in a formula to the linear terms in the model. Includes interactions (e.g. .*.)
#'@param RunMatrix The Run Matrix
#'@param model Base model
#'@return New model with dot operator replaced
#'@keywords internal
convert_model_dots = function(RunMatrix, model) {
  if (any(unlist(strsplit(as.character(model[2]), "\\s\\+\\s|\\s\\*\\s|\\:")) == ".")) {
    dotreplace = paste0("(", paste0(colnames(RunMatrix), collapse = " + "), ")")
    additionterms = unlist(strsplit(as.character(model[2]), "\\s\\+\\s"))
    multiplyterms = unlist(lapply(lapply(strsplit(additionterms, split = "\\s\\*\\s"), gsub, pattern = "^\\.$", replacement = dotreplace), paste0, collapse = " * "))
    interactionterms = unlist(lapply(lapply(strsplit(multiplyterms, split = "\\:"), gsub, pattern = "^\\.$", replacement = dotreplace), paste0, collapse = ":"))
    model = as.formula(paste0("~", paste(interactionterms, collapse = " + "), sep = ""))
  }
  return(model)
}

Try the skpr package in your browser

Any scripts or data that you put into this service are public.

skpr documentation built on May 12, 2021, 1:07 a.m.