R/expand_model.R

Defines functions expand_model

Documented in expand_model

#' Expand model
#'
#' @description Expand model given \code{y} and \code{x} variables.
#' @param y The Y variable.
#' @param x The X variables.
#' @param k Number of additive components.
#' @return A \code{char} vector containing the expanded models.
#' @examples
#' library(voice)
#'
#' expand_model('y', LETTERS[1:4], 1)
#' expand_model('y', LETTERS[1:4], 2)
#' expand_model('y', LETTERS[1:4], 3)
#' expand_model('y', LETTERS[1:4], 4)
#'
#' # multiple models using apply functions
#' nx <- 10 # number of X variables to be used
#' models <- lapply(1:nx, expand_model, y = 'y', x = LETTERS[1:nx])
#' names(models) <- 1:nx
#' models
#' sum(sapply(models, length)) # total of models
#' @export
expand_model <- function(y, x, k){

  y <- paste0(y, ' ~ ')
  comb <- utils::combn(x, k)

  if(k == 1){
    models <- unlist(lapply(y, paste0, comb))
  }

  if(k > 1){
    nc <- ncol(comb)
    pred <- vector('list', nc)
    for(j in 1:nc){
      pred[[j]] <- paste0(comb[,j], collapse = ' + ')
    }
    models <- unlist(lapply(y, paste0, pred))
  }
  return(models)
}

Try the voice package in your browser

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

voice documentation built on April 19, 2023, 5:09 p.m.