R/lmuni.R

Defines functions lmuni

Documented in lmuni

#' Linear regression univariable models: \code{finalfit} model wrapper
#'
#' Using \code{finalfit} conventions, produces multiple univariable linear
#'   regression models for a set of explanatory variables against a continuous dependent.
#'
#' Uses \code{\link[stats]{lm}} with \code{finalfit} modelling conventions. Output can be
#'   passed to \code{\link{fit2df}}.
#'
#' @param .data Dataframe.
#' @param dependent Character vector of length 1, name of depdendent variable (must be continuous vector).
#' @param explanatory Character vector of any length: name(s) of explanatory variables.
#' @param weights Character vector of length 1: name of variabe for weighting. 
#' 'Prior weights' to be used in the fitting process.
#' @param ... Other arguments to pass to \code{\link[stats]{lm}}.
#' @return A list of multivariable \code{\link[stats]{lm}} fitted model outputs.
#'   Output is of class \code{lmlist}.
#'
#' @seealso \code{\link{fit2df}}
#' @family finalfit model wrappers
#' @export
#'
#' @examples
#' library(finalfit)
#' library(dplyr)
#'
#' explanatory = c("age.factor", "sex.factor", "obstruct.factor", "perfor.factor")
#' dependent = "nodes"
#'
#' colon_s %>%
#'   lmuni(dependent, explanatory) %>%
#'   fit2df()
#'
lmuni <- function(.data, dependent, explanatory, weights = "", ...){
  result <- list()
  for (i in 1:length(explanatory)){
    result[[i]] <- ff_eval(
    	lm(ff_formula(dependent, explanatory[i]), weights = !!sym(weights), data = .data, ...)
    )
    result[[i]]$call$formula <- formula(result[[i]])
  }
  class(result) = "lmlist"
  return(result)
}

Try the finalfit package in your browser

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

finalfit documentation built on Nov. 17, 2023, 1:09 a.m.