cross_fit: Cross map a model across multiple formulas, subsets, and...

View source: R/cross_fit.R

cross_fitR Documentation

Cross map a model across multiple formulas, subsets, and weights

Description

Applies a modeling function to every combination of a set of formulas and a set of data subsets.

Usage

cross_fit(
  data,
  formulas,
  cols = NULL,
  weights = NULL,
  clusters = NULL,
  families = NULL,
  fn = lm,
  fn_args = list(),
  tidy = tidy_glance,
  tidy_args = list(),
  errors = c("stop", "warn")
)

Arguments

data

A data frame

formulas

A list of formulas to apply to each subset of the data. If named, these names will be used in the model column of the output. Otherwise, the formulas will be converted to strings in the model column.

cols

Columns to subset the data. Can be any expression supported by <tidy-select>. If NULL, the data is not subset into columns. Defaults to NULL.

weights

A list of columns passed to weights in fn. If one of the elements is NULL or NA, that model will not be weighted. Defaults to NULL.

clusters

A list of columns passed to clusters if supported by fn. If one of the elements is NULL or NA, that model will not be clustered. Defaults to NULL.

families

A list of glm model families passed to family if supported by fn. Defaults to gaussian("identity"), the equivalent of lm(). See family for examples.

fn

The modeling function. Either an unquoted function name or a purrr-style lambda function with two arguments. To use multiple modeling functions, see cross_fit_glm(). Defaults to lm.

fn_args

A list of additional arguments to fn.

tidy

A logical or function to use to tidy model output into data.frame columns. If TRUE, uses the default tidying function: tidy_glance(). If FALSE, NA, or NULL, the untidied model output will be returned in a list column named fit. An alternative function can be specified with an unquoted function name or a purrr-style lambda function with one argument (see usage with broom::tidy(conf.int = TRUE) in examples). Defaults to tidy_glance.

tidy_args

A list of additional arguments to the tidy function

errors

If "stop", the default, the function will stop and return an error if any subset produces an error. If "warn", the function will produce a warning for subsets that produce an error and return results for all subsets that do not.

Value

A tibble with a column for the model formula, columns for subsets, columns for the model family and type (if applicable), columns for the weights and clusters (if applicable), and columns of tidy model output or a list column of models (if tidy = FALSE)

See Also

cross_fit_glm() to map a model across multiple model types.

cross_fit_robust() to map robust linear models.

xmap() to apply any function to combinations of inputs.

Examples

cross_fit(mtcars, mpg ~ wt, cyl)
cross_fit(mtcars, list(mpg ~ wt, mpg ~ hp), cyl)
cross_fit(mtcars, list(wt = mpg ~ wt, hp = mpg ~ hp), cyl)

cross_fit(mtcars, list(mpg ~ wt, mpg ~ hp), c(cyl, vs))
cross_fit(mtcars, list(mpg ~ wt, mpg ~ hp), dplyr::starts_with("c"))

cross_fit(mtcars, list(hp = mpg ~ hp), cyl, weights = wt)
cross_fit(mtcars, list(hp = mpg ~ hp), cyl, weights = c(wt, NA))

cross_fit(
  mtcars, list(vs ~ cyl, vs ~ hp), am,
  fn = glm, fn_args = list(family = binomial(link = logit))
)
cross_fit(
  mtcars, list(vs ~ cyl, vs ~ hp), am,
  fn = ~ glm(.x, .y, family = binomial(link = logit))
)

cross_fit(mtcars, list(mpg ~ wt, mpg ~ hp), cyl, tidy = FALSE)
cross_fit(mtcars, list(mpg ~ wt, mpg ~ hp), cyl, tidy_args = c(conf.int = TRUE))

cross_fit(mtcars, list(mpg ~ wt, mpg ~ hp), cyl, tidy = broom::tidy)
cross_fit(
  mtcars, list(mpg ~ wt, mpg ~ hp), cyl,
  tidy = ~ broom::tidy(., conf.int = TRUE)
)

crossmap documentation built on Jan. 13, 2023, 1:13 a.m.