library(tidyverse)
models <- tibble::tribble(
~model_name, ~ formula,
"length-width", Sepal.Length ~ Petal.Width + Petal.Length,
"interaction", Sepal.Length ~ Petal.Width * Petal.Length
)
iris |>
nest_by(Species) %>%
left_join(models, by = character()) %>%
rowwise(Species, model_name) %>%
mutate(model = list(lm(formula, data = data))) %>%
summarise(broom::glance(model))
# A tibble: 6 × 14
# Groups: Species, model_name [6]
Species model_n…¹ r.squ…² adj.r…³ sigma stati…⁴ p.value df logLik AIC
<fct> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 setosa length-w… 0.112 0.0739 0.339 2.96 6.18e- 2 2 -15.3 38.7
2 setosa interact… 0.133 0.0760 0.339 2.34 8.54e- 2 3 -14.7 39.5
3 versicolor length-w… 0.574 0.556 0.344 31.7 1.92e- 9 2 -16.0 40.0
4 versicolor interact… 0.577 0.549 0.347 20.9 1.11e- 8 3 -15.9 41.8
5 virginica length-w… 0.747 0.736 0.327 69.3 9.50e-15 2 -13.5 34.9
6 virginica interact… 0.757 0.741 0.323 47.8 3.54e-14 3 -12.4 34.9
# … with 4 more variables: BIC <dbl>, deviance <dbl>, df.residual <int>,
# nobs <int>, and abbreviated variable names ¹model_name, ²r.squared,
# ³adj.r.squared, ⁴statistic
# ℹ Use `colnames()` to see all variable names