View source: R/model_formula.R
| model_formula | R Documentation |
Generates model formulas from a dataframe, a response name, and a vector of predictors that can be the output of a multicollinearity management function such as collinear_select() and the likes. Intended to help fit exploratory models from the result of a multicollinearity analysis.
The types of formulas it can generate are:
additive: y ~ x + z
polynomial: y ~ poly(x, ...) + poly(z, ...)
GAM: y ~ s(x) + s(z)
random effect: y ~ x + (1 \ z)
model_formula(
df = NULL,
response = NULL,
predictors = NULL,
term_f = NULL,
term_args = NULL,
random_effects = NULL,
quiet = FALSE,
...
)
df |
(required; dataframe, tibble, or sf) A dataframe with responses
(optional) and predictors. Must have at least 10 rows for pairwise
correlation analysis, and |
response |
(optional, character string) Name of a response variable in |
predictors |
(optional; character vector or NULL) Names of the
predictors in |
term_f |
(optional; string). Name of function to apply to each term in the formula, such as "s" for |
term_args |
(optional; string). Arguments of the function applied to each term. For example, for "poly" it can be "degree = 2, raw = TRUE". Default: NULL |
random_effects |
(optional, string or character vector). Names of variables to be used as random effects. Each element is added to the final formula as |
quiet |
(optional; logical) If FALSE, messages are printed. Default: FALSE. |
... |
(optional) Internal args (e.g. |
list if predictors is a list or length of response is higher than one, and character vector otherwise.
Other modelling_tools:
case_weights(),
score_auc(),
score_cramer(),
score_r2()
data(
vi_smol,
vi_predictors_numeric
)
#reduce collinearity
x <- collinear_select(
df = vi_smol,
predictors = vi_predictors_numeric
)
#additive formula
y <- model_formula(
df = vi_smol,
response = "vi_numeric",
predictors = x
)
y
#using a formula in a model
m <- stats::lm(
formula = y,
data = vi_smol
)
summary(m)
#classification formula (character response)
y <- model_formula(
df = vi_smol,
response = "vi_categorical",
predictors = x
)
y
#polynomial formula (3rd degree)
y <- model_formula(
df = vi_smol,
response = "vi_numeric",
predictors = x,
term_f = "poly",
term_args = "degree = 3, raw = TRUE"
)
y
#gam formula
y <- model_formula(
df = vi_smol,
response = "vi_numeric",
predictors = x,
term_f = "s"
)
y
#random effect
y <- model_formula(
df = vi_smol,
response = "vi_numeric",
predictors = x,
random_effects = "country_name" #from vi_smol$country_name
)
y
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.