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

cross_fit(
  data,
  formulas,
  cols = NULL,
  weights = NULL,
  fn = stats::lm,
  fn_args = list(),
  tidy = broom::tidy,
  tidy_args = list()
)

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 another column name that does not appear in the data, that model will not be weighted. Defaults to NULL.

fn

The modeling function. Either an unquoted function name or a purrr-style lambda function with two arguments (see usage with glm in examples). 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: broom::tidy(). If FALSE, NA, or NULL, the untidied model output will be returned in a list column 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 broom::tidy().

tidy_args

A list of additional arguments to the tidy function

Value

A tibble with subsetting columns, a column for the model formula applied, and columns of tidy model output or a list column of models (if tidy = FALSE)

See also

xmap() to apply any function to combinations of inputs

Examples

cross_fit(mtcars, mpg ~ wt, cyl)
#> # A tibble: 6 x 7 #> model cyl term estimate std.error statistic p.value #> <chr> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 mpg ~ wt 4 (Intercept) 39.6 4.35 9.10 0.00000777 #> 2 mpg ~ wt 4 wt -5.65 1.85 -3.05 0.0137 #> 3 mpg ~ wt 6 (Intercept) 28.4 4.18 6.79 0.00105 #> 4 mpg ~ wt 6 wt -2.78 1.33 -2.08 0.0918 #> 5 mpg ~ wt 8 (Intercept) 23.9 3.01 7.94 0.00000405 #> 6 mpg ~ wt 8 wt -2.19 0.739 -2.97 0.0118
cross_fit(mtcars, list(mpg ~ wt, mpg ~ hp), cyl)
#> # A tibble: 12 x 7 #> model cyl term estimate std.error statistic p.value #> <chr> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 mpg ~ wt 4 (Intercept) 39.6 4.35 9.10 0.00000777 #> 2 mpg ~ wt 4 wt -5.65 1.85 -3.05 0.0137 #> 3 mpg ~ wt 6 (Intercept) 28.4 4.18 6.79 0.00105 #> 4 mpg ~ wt 6 wt -2.78 1.33 -2.08 0.0918 #> 5 mpg ~ wt 8 (Intercept) 23.9 3.01 7.94 0.00000405 #> 6 mpg ~ wt 8 wt -2.19 0.739 -2.97 0.0118 #> 7 mpg ~ hp 4 (Intercept) 36.0 5.20 6.92 0.0000693 #> 8 mpg ~ hp 4 hp -0.113 0.0612 -1.84 0.0984 #> 9 mpg ~ hp 6 (Intercept) 20.7 3.30 6.26 0.00153 #> 10 mpg ~ hp 6 hp -0.00761 0.0266 -0.286 0.786 #> 11 mpg ~ hp 8 (Intercept) 18.1 2.99 6.05 0.0000574 #> 12 mpg ~ hp 8 hp -0.0142 0.0139 -1.02 0.326
cross_fit(mtcars, list(wt = mpg ~ wt, hp = mpg ~ hp), cyl)
#> # A tibble: 12 x 7 #> model cyl term estimate std.error statistic p.value #> <chr> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 wt 4 (Intercept) 39.6 4.35 9.10 0.00000777 #> 2 wt 4 wt -5.65 1.85 -3.05 0.0137 #> 3 wt 6 (Intercept) 28.4 4.18 6.79 0.00105 #> 4 wt 6 wt -2.78 1.33 -2.08 0.0918 #> 5 wt 8 (Intercept) 23.9 3.01 7.94 0.00000405 #> 6 wt 8 wt -2.19 0.739 -2.97 0.0118 #> 7 hp 4 (Intercept) 36.0 5.20 6.92 0.0000693 #> 8 hp 4 hp -0.113 0.0612 -1.84 0.0984 #> 9 hp 6 (Intercept) 20.7 3.30 6.26 0.00153 #> 10 hp 6 hp -0.00761 0.0266 -0.286 0.786 #> 11 hp 8 (Intercept) 18.1 2.99 6.05 0.0000574 #> 12 hp 8 hp -0.0142 0.0139 -1.02 0.326
cross_fit(mtcars, list(mpg ~ wt, mpg ~ hp), c(cyl, vs))
#> Warning: essentially perfect fit: summary may be unreliable
#> # A tibble: 20 x 8 #> model cyl vs term estimate std.error statistic p.value #> <chr> <dbl> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 mpg ~ wt 4 0 (Intercept) 26 NaN NaN NaN #> 2 mpg ~ wt 4 0 wt NA NA NA NA #> 3 mpg ~ wt 4 1 (Intercept) 39.9 4.61e+ 0 8.66e+ 0 2.47e- 5 #> 4 mpg ~ wt 4 1 wt -5.72 1.95e+ 0 -2.94e+ 0 1.87e- 2 #> 5 mpg ~ wt 6 0 (Intercept) 22.2 1.61e+ 1 1.38e+ 0 3.99e- 1 #> 6 mpg ~ wt 6 0 wt -0.594 5.83e+ 0 -1.02e- 1 9.35e- 1 #> 7 mpg ~ wt 6 1 (Intercept) 63.6 1.19e+ 1 5.36e+ 0 3.30e- 2 #> 8 mpg ~ wt 6 1 wt -13.1 3.50e+ 0 -3.75e+ 0 6.42e- 2 #> 9 mpg ~ wt 8 0 (Intercept) 23.9 3.01e+ 0 7.94e+ 0 4.05e- 6 #> 10 mpg ~ wt 8 0 wt -2.19 7.39e- 1 -2.97e+ 0 1.18e- 2 #> 11 mpg ~ hp 4 0 (Intercept) 26 NaN NaN NaN #> 12 mpg ~ hp 4 0 hp NA NA NA NA #> 13 mpg ~ hp 4 1 (Intercept) 36.0 5.52e+ 0 6.52e+ 0 1.85e- 4 #> 14 mpg ~ hp 4 1 hp -0.113 6.55e- 2 -1.73e+ 0 1.21e- 1 #> 15 mpg ~ hp 6 0 (Intercept) 23.2 1.02e-14 2.28e+15 2.79e-16 #> 16 mpg ~ hp 6 0 hp -0.02 7.53e-17 -2.66e+14 2.40e-15 #> 17 mpg ~ hp 6 1 (Intercept) 24.2 1.41e+ 1 1.72e+ 0 2.28e- 1 #> 18 mpg ~ hp 6 1 hp -0.0440 1.22e- 1 -3.61e- 1 7.52e- 1 #> 19 mpg ~ hp 8 0 (Intercept) 18.1 2.99e+ 0 6.05e+ 0 5.74e- 5 #> 20 mpg ~ hp 8 0 hp -0.0142 1.39e- 2 -1.02e+ 0 3.26e- 1
cross_fit(mtcars, list(mpg ~ wt, mpg ~ hp), dplyr::starts_with("c"))
#> # A tibble: 36 x 8 #> model cyl carb term estimate std.error statistic p.value #> <chr> <dbl> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 mpg ~ wt 4 1 (Intercept) 62.0 17.2 3.60 0.0367 #> 2 mpg ~ wt 4 1 wt -16.0 7.95 -2.01 0.138 #> 3 mpg ~ wt 4 2 (Intercept) 36.8 2.83 13.0 0.000201 #> 4 mpg ~ wt 4 2 wt -4.56 1.13 -4.02 0.0159 #> 5 mpg ~ wt 6 1 (Intercept) 64.7 NaN NaN NaN #> 6 mpg ~ wt 6 1 wt -13.5 NaN NaN NaN #> 7 mpg ~ wt 6 4 (Intercept) 30.2 3.61 8.37 0.0140 #> 8 mpg ~ wt 6 4 wt -3.38 1.16 -2.92 0.100 #> 9 mpg ~ wt 6 6 (Intercept) 19.7 NaN NaN NaN #> 10 mpg ~ wt 6 6 wt NA NA NA NA #> # … with 26 more rows
cross_fit(mtcars, list(hp = mpg ~ hp), cyl, weights = wt)
#> # A tibble: 6 x 8 #> model cyl weights term estimate std.error statistic p.value #> <chr> <dbl> <chr> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 hp 4 wt (Intercept) 36.2 5.14 7.03 0.0000610 #> 2 hp 4 wt hp -0.123 0.0601 -2.04 0.0717 #> 3 hp 6 wt (Intercept) 20.4 3.49 5.85 0.00207 #> 4 hp 6 wt hp -0.00657 0.0283 -0.232 0.826 #> 5 hp 8 wt (Intercept) 18.0 3.36 5.36 0.000172 #> 6 hp 8 wt hp -0.0151 0.0156 -0.968 0.352
cross_fit(mtcars, list(hp = mpg ~ hp), cyl, weights = list(wt, NULL))
#> # A tibble: 12 x 8 #> model cyl weights term estimate std.error statistic p.value #> <chr> <dbl> <chr> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 hp 4 NULL (Intercept) 36.0 5.20 6.92 0.0000693 #> 2 hp 4 NULL hp -0.113 0.0612 -1.84 0.0984 #> 3 hp 4 wt (Intercept) 36.2 5.14 7.03 0.0000610 #> 4 hp 4 wt hp -0.123 0.0601 -2.04 0.0717 #> 5 hp 6 NULL (Intercept) 20.7 3.30 6.26 0.00153 #> 6 hp 6 NULL hp -0.00761 0.0266 -0.286 0.786 #> 7 hp 6 wt (Intercept) 20.4 3.49 5.85 0.00207 #> 8 hp 6 wt hp -0.00657 0.0283 -0.232 0.826 #> 9 hp 8 NULL (Intercept) 18.1 2.99 6.05 0.0000574 #> 10 hp 8 NULL hp -0.0142 0.0139 -1.02 0.326 #> 11 hp 8 wt (Intercept) 18.0 3.36 5.36 0.000172 #> 12 hp 8 wt hp -0.0151 0.0156 -0.968 0.352
cross_fit( mtcars, list(vs ~ cyl, vs ~ hp), am, fn = glm, fn_args = list(family = binomial(link = logit)) )
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
#> Warning: glm.fit: algorithm did not converge
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
#> # A tibble: 8 x 7 #> model am term estimate std.error statistic p.value #> <chr> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 vs ~ cyl 0 (Intercept) 172. 251712. 0.000682 0.999 #> 2 vs ~ cyl 0 cyl -24.6 35695. -0.000689 0.999 #> 3 vs ~ cyl 1 (Intercept) 44.3 10435. 0.00424 0.997 #> 4 vs ~ cyl 1 cyl -10.6 2609. -0.00406 0.997 #> 5 vs ~ hp 0 (Intercept) 231. 276189. 0.000835 0.999 #> 6 vs ~ hp 0 hp -1.69 1999. -0.000844 0.999 #> 7 vs ~ hp 1 (Intercept) 7.07 4.76 1.49 0.137 #> 8 vs ~ hp 1 hp -0.0663 0.0467 -1.42 0.156
cross_fit( mtcars, list(vs ~ cyl, vs ~ hp), am, fn = ~ glm(.x, .y, family = binomial(link = logit)) )
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
#> Warning: glm.fit: algorithm did not converge
#> Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
#> # A tibble: 8 x 7 #> model am term estimate std.error statistic p.value #> <chr> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 vs ~ cyl 0 (Intercept) 172. 251712. 0.000682 0.999 #> 2 vs ~ cyl 0 cyl -24.6 35695. -0.000689 0.999 #> 3 vs ~ cyl 1 (Intercept) 44.3 10435. 0.00424 0.997 #> 4 vs ~ cyl 1 cyl -10.6 2609. -0.00406 0.997 #> 5 vs ~ hp 0 (Intercept) 231. 276189. 0.000835 0.999 #> 6 vs ~ hp 0 hp -1.69 1999. -0.000844 0.999 #> 7 vs ~ hp 1 (Intercept) 7.07 4.76 1.49 0.137 #> 8 vs ~ hp 1 hp -0.0663 0.0467 -1.42 0.156
cross_fit(mtcars, list(mpg ~ wt, mpg ~ hp), cyl, tidy = FALSE)
#> # A tibble: 6 x 3 #> model cyl fit #> <chr> <dbl> <list> #> 1 mpg ~ wt 4 <lm> #> 2 mpg ~ wt 6 <lm> #> 3 mpg ~ wt 8 <lm> #> 4 mpg ~ hp 4 <lm> #> 5 mpg ~ hp 6 <lm> #> 6 mpg ~ hp 8 <lm>
cross_fit(mtcars, list(mpg ~ wt, mpg ~ hp), cyl, tidy_args = c(conf.int = TRUE))
#> # A tibble: 12 x 9 #> model cyl term estimate std.error statistic p.value conf.low conf.high #> <chr> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 mpg ~… 4 (Inter… 39.6 4.35 9.10 7.77e-6 29.7 49.4 #> 2 mpg ~… 4 wt -5.65 1.85 -3.05 1.37e-2 -9.83 -1.46 #> 3 mpg ~… 6 (Inter… 28.4 4.18 6.79 1.05e-3 17.7 39.2 #> 4 mpg ~… 6 wt -2.78 1.33 -2.08 9.18e-2 -6.21 0.651 #> 5 mpg ~… 8 (Inter… 23.9 3.01 7.94 4.05e-6 17.3 30.4 #> 6 mpg ~… 8 wt -2.19 0.739 -2.97 1.18e-2 -3.80 -0.582 #> 7 mpg ~… 4 (Inter… 36.0 5.20 6.92 6.93e-5 24.2 47.7 #> 8 mpg ~… 4 hp -0.113 0.0612 -1.84 9.84e-2 -0.251 0.0256 #> 9 mpg ~… 6 (Inter… 20.7 3.30 6.26 1.53e-3 12.2 29.2 #> 10 mpg ~… 6 hp -0.00761 0.0266 -0.286 7.86e-1 -0.0759 0.0607 #> 11 mpg ~… 8 (Inter… 18.1 2.99 6.05 5.74e-5 11.6 24.6 #> 12 mpg ~… 8 hp -0.0142 0.0139 -1.02 3.26e-1 -0.0445 0.0160
cross_fit( mtcars, list(mpg ~ wt, mpg ~ hp), cyl, tidy = ~ broom::tidy(., conf.int = TRUE) )
#> # A tibble: 12 x 9 #> model cyl term estimate std.error statistic p.value conf.low conf.high #> <chr> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 mpg ~… 4 (Inter… 39.6 4.35 9.10 7.77e-6 29.7 49.4 #> 2 mpg ~… 4 wt -5.65 1.85 -3.05 1.37e-2 -9.83 -1.46 #> 3 mpg ~… 6 (Inter… 28.4 4.18 6.79 1.05e-3 17.7 39.2 #> 4 mpg ~… 6 wt -2.78 1.33 -2.08 9.18e-2 -6.21 0.651 #> 5 mpg ~… 8 (Inter… 23.9 3.01 7.94 4.05e-6 17.3 30.4 #> 6 mpg ~… 8 wt -2.19 0.739 -2.97 1.18e-2 -3.80 -0.582 #> 7 mpg ~… 4 (Inter… 36.0 5.20 6.92 6.93e-5 24.2 47.7 #> 8 mpg ~… 4 hp -0.113 0.0612 -1.84 9.84e-2 -0.251 0.0256 #> 9 mpg ~… 6 (Inter… 20.7 3.30 6.26 1.53e-3 12.2 29.2 #> 10 mpg ~… 6 hp -0.00761 0.0266 -0.286 7.86e-1 -0.0759 0.0607 #> 11 mpg ~… 8 (Inter… 18.1 2.99 6.05 5.74e-5 11.6 24.6 #> 12 mpg ~… 8 hp -0.0142 0.0139 -1.02 3.26e-1 -0.0445 0.0160