compare_predictors: Fit linear models for a set of predictors to compare their...

compare_predictorsR Documentation

Fit linear models for a set of predictors to compare their effects alone and together

Description

This function fits a set of models to the data, where each model includes only one predictor. It then fits a full model with all predictors. The function returns a data frame with the coefficients for each model, as well as the full model. Thus, if given p predictors, it will return p+1 model results and 2 betas for each predictor.

Usage

compare_predictors(
  data,
  outcome,
  predictors,
  additional_models = NULL,
  conf.level = 0.95,
  family = gaussian,
  controls = NULL,
  keep_controls = T
)

Arguments

data

The data frame to use

outcome

The name of the outcome variable. Must be numeric.

predictors

A character vector of predictors to use.

additional_models

A list of additional models to fit. Each element of the list should be a character vector of predictors to use. The names of the list will be used as the model names in the output.

conf.level

The confidence level to use. Default is .95.

family

The family to use. Default is "gaussian" (OLS), see ?glm for more information.

controls

A character vector of control variables to use. These will be added to all models.

keep_controls

Whether to keep the values for control variables in the output. Default is TRUE.

Value

A data frame with the coefficients for each model

Examples

compare_predictors(iris, names(iris)[1], names(iris)[-1])
compare_predictors(mpg, names(mpg)[3], names(mpg)[-3])
#with additional models
compare_predictors(iris, names(iris)[1], names(iris)[-1], additional_models = list(petal = c("Petal.Length", "Petal.Width")))
#with controls
compare_predictors(iris, names(iris)[1], names(iris)[-c(1, 5)], controls = c("Species"))

Deleetdk/kirkegaard documentation built on June 8, 2025, 4:09 a.m.