Title: | Helpers for Model Coefficients Tibbles |
Version: | 1.21.0 |
Description: | Provides suite of functions to work with regression model 'broom::tidy()' tibbles. The suite includes functions to group regression model terms by variable, insert reference and header rows for categorical variables, add variable labels, and more. |
License: | GPL (≥ 3) |
URL: | https://larmarange.github.io/broom.helpers/, https://github.com/larmarange/broom.helpers |
BugReports: | https://github.com/larmarange/broom.helpers/issues |
Depends: | R (≥ 4.1) |
Imports: | broom (≥ 0.8), cards, cli, dplyr (≥ 1.1.0), labelled, lifecycle, purrr, rlang (≥ 1.0.1), stats, stringr, tibble, tidyr, tidyselect |
Suggests: | betareg, biglm, brms (≥ 2.13.0), broom.mixed, cmprsk, covr, datasets, effects, emmeans, fixest (≥ 0.10.0), forcats, gam, gee, geepack, ggplot2, ggeffects (≥ 1.3.2), ggstats (≥ 0.2.1), glmmTMB, glmtoolbox, glue, gt, gtsummary (≥ 2.0.0), knitr, lavaan, lfe, lme4 (≥ 1.1.28), logitr (≥ 0.8.0), marginaleffects (≥ 0.21.0), margins, MASS, mgcv, mice, mmrm (≥ 0.3.6), multgee, nnet, ordinal, parameters, parsnip, patchwork, plm, pscl, rmarkdown, rstanarm, scales, spelling, survey, survival, testthat (≥ 3.0.0), tidycmprsk, VGAM, svyVGAM |
VignetteBuilder: | knitr |
RdMacros: | lifecycle |
Encoding: | UTF-8 |
Language: | en-US |
LazyData: | true |
RoxygenNote: | 7.3.2 |
Config/testthat/edition: | 3 |
NeedsCompilation: | no |
Packaged: | 2025-04-24 08:04:30 UTC; josep |
Author: | Joseph Larmarange |
Maintainer: | Joseph Larmarange <joseph@larmarange.net> |
Repository: | CRAN |
Date/Publication: | 2025-04-24 08:50:02 UTC |
Remove backticks around variable names
Description
Remove backticks around variable names
Usage
.clean_backticks(x, variable_names = x)
Arguments
x |
( |
variable_names |
( |
See Also
Other other_helpers:
.escape_regex()
Escapes any characters that would have special meaning in a regular expression
Description
This functions has been adapted from Hmisc::escapeRegex()
Usage
.escape_regex(string)
Arguments
string |
( |
See Also
Other other_helpers:
.clean_backticks()
Convert formula selector to a named list
Description
This function will soon be removed from broom.helpers
. Please consider
cards::process_formula_selectors()
as an alternative.
Usage
.formula_list_to_named_list(
x,
data = NULL,
var_info = NULL,
arg_name = NULL,
select_single = FALSE,
type_check = NULL,
type_check_msg = NULL,
null_allowed = TRUE
)
Arguments
x |
list of selecting formulas |
data |
A data frame to select columns from. Default is NULL |
var_info |
A data frame of variable names and attributes. May also pass a character vector of variable names. Default is NULL |
arg_name |
Optional string indicating the source argument name. This helps in the error messaging. Default is NULL. |
select_single |
Logical indicating whether the result must be a single
variable. Default is |
type_check |
A predicate function that checks the elements passed on
the RHS of the formulas in |
type_check_msg |
When the |
null_allowed |
Are |
Details
Functions takes a list of formulas, a named list, or a combination of named
elements with formula elements and returns a named list.
For example, list(age = 1, starts_with("stage") ~ 2)
.
Shortcuts
A shortcut for specifying an option be applied to all columns/variables
is omitting the LHS of the formula.
For example, list(~ 1)
is equivalent to passing list(everything() ~ 1)
.
Additionally, a single formula may be passed instead of placing a single
formula in a list; e.g. everything() ~ 1
is equivalent to
passing list(everything() ~ 1)
Generate a custom selector function
Description
Usage
.generic_selector(variable_column, select_column, select_expr, fun_name)
.is_selector_scoped(variable_column, select_column)
Arguments
variable_column |
string indicating column variable names are stored |
select_column |
character vector of columns used in the |
select_expr |
unquoted predicate command to subset a data frame to select variables |
fun_name |
quoted name of function where |
Details
.is_selector_scoped()
checks if a selector has been properly registered
in env_variable_type$df_var_info
.
Value
custom selector functions
Variable selector
Description
This function will soon be removed from broom.helpers
. Please consider
cards::process_selectors()
as an alternative.
Usage
.select_to_varnames(
select,
data = NULL,
var_info = NULL,
arg_name = NULL,
select_single = FALSE
)
Arguments
select |
A single object selecting variables, e.g. |
data |
A data frame to select columns from. Default is NULL |
var_info |
A data frame of variable names and attributes. May also pass a character vector of variable names. Default is NULL |
arg_name |
Optional string indicating the source argument name. This helps in the error messaging. Default is NULL. |
select_single |
Logical indicating whether the result must be a single
variable. Default is |
Details
Function takes select()
-like inputs and converts the selector to
a character vector of variable names. Functions accepts tidyselect syntax,
and additional selector functions defined within the package
Value
A character vector of variable names
Check a package installation status or minimum required version
Description
The function .assert_package()
checks whether a package is installed and
returns an error or FALSE
if not available. If a package search is provided,
the function will check whether a minimum version of a package is required.
The function .get_package_dependencies()
returns a tibble with all
dependencies of a specific package. Finally, .get_min_version_required()
will return, if any, the minimum version of pkg
required by pkg_search
,
NULL
if no minimum version required.
Usage
.assert_package(pkg, fn = NULL, pkg_search = "broom.helpers", boolean = FALSE)
.get_package_dependencies(pkg_search = "broom.helpers")
.get_all_packages_dependencies(
pkg_search = NULL,
remove_duplicates = FALSE,
lib.loc = NULL
)
.get_min_version_required(pkg, pkg_search = "broom.helpers")
Arguments
pkg |
( |
fn |
( |
pkg_search |
( |
boolean |
( |
remove_duplicates |
( |
lib.loc |
( |
Details
get_all_packages_dependencies()
could be used to get the list of
dependencies of all installed packages.
Value
logical or error for .assert_package()
, NULL
or character with
the minimum version required for .get_min_version_required()
, a tibble for
.get_package_dependencies()
.
Examples
.assert_package("broom", boolean = TRUE)
.get_package_dependencies()
.get_min_version_required("brms")
Compute a matrix of terms contributions
Description
Used for model_get_n()
. For each row and term, equal 1 if this row should
be taken into account in the estimate of the number of observations,
0 otherwise.
Usage
model_compute_terms_contributions(model)
## Default S3 method:
model_compute_terms_contributions(model)
Arguments
model |
(a model object, e.g. |
Details
This function does not cover lavaan
models (NULL
is returned).
See Also
Other model_helpers:
model_get_assign()
,
model_get_coefficients_type()
,
model_get_contrasts()
,
model_get_model()
,
model_get_model_frame()
,
model_get_model_matrix()
,
model_get_n()
,
model_get_nlevels()
,
model_get_offset()
,
model_get_pairwise_contrasts()
,
model_get_response()
,
model_get_response_variable()
,
model_get_terms()
,
model_get_weights()
,
model_get_xlevels()
,
model_identify_variables()
,
model_list_contrasts()
,
model_list_higher_order_variables()
,
model_list_terms_levels()
,
model_list_variables()
Examples
mod <- lm(Sepal.Length ~ Sepal.Width, iris)
mod |> model_compute_terms_contributions()
mod <- lm(hp ~ mpg + factor(cyl) + disp:hp, mtcars)
mod |> model_compute_terms_contributions()
mod <- glm(
response ~ stage * grade + trt,
gtsummary::trial,
family = binomial,
contrasts = list(
stage = contr.sum,
grade = contr.treatment(3, 2),
trt = "contr.SAS"
)
)
mod |> model_compute_terms_contributions()
mod <- glm(
response ~ stage * trt,
gtsummary::trial,
family = binomial,
contrasts = list(stage = contr.poly)
)
mod |> model_compute_terms_contributions()
mod <- glm(
Survived ~ Class * Age + Sex,
data = Titanic |> as.data.frame(),
weights = Freq, family = binomial
)
mod |> model_compute_terms_contributions()
d <- dplyr::as_tibble(Titanic) |>
dplyr::group_by(Class, Sex, Age) |>
dplyr::summarise(
n_survived = sum(n * (Survived == "Yes")),
n_dead = sum(n * (Survived == "No"))
)
mod <- glm(cbind(n_survived, n_dead) ~ Class * Age + Sex, data = d, family = binomial)
mod |> model_compute_terms_contributions()
Get the assign attribute of model matrix of a model
Description
Return the assign attribute attached to the object returned by
stats::model.matrix()
.
Usage
model_get_assign(model)
## Default S3 method:
model_get_assign(model)
## S3 method for class 'vglm'
model_get_assign(model)
## S3 method for class 'svy_vglm'
model_get_assign(model)
## S3 method for class 'model_fit'
model_get_assign(model)
Arguments
model |
(a model object, e.g. |
See Also
Other model_helpers:
model_compute_terms_contributions()
,
model_get_coefficients_type()
,
model_get_contrasts()
,
model_get_model()
,
model_get_model_frame()
,
model_get_model_matrix()
,
model_get_n()
,
model_get_nlevels()
,
model_get_offset()
,
model_get_pairwise_contrasts()
,
model_get_response()
,
model_get_response_variable()
,
model_get_terms()
,
model_get_weights()
,
model_get_xlevels()
,
model_identify_variables()
,
model_list_contrasts()
,
model_list_higher_order_variables()
,
model_list_terms_levels()
,
model_list_variables()
Examples
lm(hp ~ mpg + factor(cyl), mtcars) |>
model_get_assign()
Get coefficient type
Description
Indicate the type of coefficient among "generic", "logistic", "poisson", "relative_risk" or "prop_hazard".
Usage
model_get_coefficients_type(model)
## Default S3 method:
model_get_coefficients_type(model)
## S3 method for class 'glm'
model_get_coefficients_type(model)
## S3 method for class 'negbin'
model_get_coefficients_type(model)
## S3 method for class 'geeglm'
model_get_coefficients_type(model)
## S3 method for class 'fixest'
model_get_coefficients_type(model)
## S3 method for class 'biglm'
model_get_coefficients_type(model)
## S3 method for class 'glmerMod'
model_get_coefficients_type(model)
## S3 method for class 'clogit'
model_get_coefficients_type(model)
## S3 method for class 'polr'
model_get_coefficients_type(model)
## S3 method for class 'multinom'
model_get_coefficients_type(model)
## S3 method for class 'svyolr'
model_get_coefficients_type(model)
## S3 method for class 'clm'
model_get_coefficients_type(model)
## S3 method for class 'clmm'
model_get_coefficients_type(model)
## S3 method for class 'coxph'
model_get_coefficients_type(model)
## S3 method for class 'crr'
model_get_coefficients_type(model)
## S3 method for class 'tidycrr'
model_get_coefficients_type(model)
## S3 method for class 'cch'
model_get_coefficients_type(model)
## S3 method for class 'model_fit'
model_get_coefficients_type(model)
## S3 method for class 'LORgee'
model_get_coefficients_type(model)
## S3 method for class 'vglm'
model_get_coefficients_type(model)
## S3 method for class 'vgam'
model_get_coefficients_type(model)
## S3 method for class 'svy_vglm'
model_get_coefficients_type(model)
Arguments
model |
(a model object, e.g. |
See Also
Other model_helpers:
model_compute_terms_contributions()
,
model_get_assign()
,
model_get_contrasts()
,
model_get_model()
,
model_get_model_frame()
,
model_get_model_matrix()
,
model_get_n()
,
model_get_nlevels()
,
model_get_offset()
,
model_get_pairwise_contrasts()
,
model_get_response()
,
model_get_response_variable()
,
model_get_terms()
,
model_get_weights()
,
model_get_xlevels()
,
model_identify_variables()
,
model_list_contrasts()
,
model_list_higher_order_variables()
,
model_list_terms_levels()
,
model_list_variables()
Examples
lm(hp ~ mpg + factor(cyl), mtcars) |>
model_get_coefficients_type()
df <- Titanic |>
dplyr::as_tibble() |>
dplyr::mutate(Survived = factor(Survived, c("No", "Yes")))
glm(Survived ~ Class + Age * Sex, data = df, weights = df$n, family = binomial) |>
model_get_coefficients_type()
Get contrasts used in the model
Description
Get contrasts used in the model
Usage
model_get_contrasts(model)
## S3 method for class 'model_fit'
model_get_contrasts(model)
## S3 method for class 'zeroinfl'
model_get_contrasts(model)
## S3 method for class 'hurdle'
model_get_contrasts(model)
## S3 method for class 'betareg'
model_get_contrasts(model)
## S3 method for class 'svy_vglm'
model_get_contrasts(model)
Arguments
model |
(a model object, e.g. |
See Also
Other model_helpers:
model_compute_terms_contributions()
,
model_get_assign()
,
model_get_coefficients_type()
,
model_get_model()
,
model_get_model_frame()
,
model_get_model_matrix()
,
model_get_n()
,
model_get_nlevels()
,
model_get_offset()
,
model_get_pairwise_contrasts()
,
model_get_response()
,
model_get_response_variable()
,
model_get_terms()
,
model_get_weights()
,
model_get_xlevels()
,
model_identify_variables()
,
model_list_contrasts()
,
model_list_higher_order_variables()
,
model_list_terms_levels()
,
model_list_variables()
Examples
glm(
am ~ mpg + factor(cyl),
data = mtcars,
family = binomial,
contrasts = list(`factor(cyl)` = contr.sum)
) |>
model_get_contrasts()
Get the model from model objects
Description
Most model objects are proper R model objects. There are, however, some model objects that store the proper object internally (e.g. mice models). This function extracts that model object in those cases.
Usage
model_get_model(model)
## Default S3 method:
model_get_model(model)
## S3 method for class 'mira'
model_get_model(model)
Arguments
model |
(a model object, e.g. |
See Also
Other model_helpers:
model_compute_terms_contributions()
,
model_get_assign()
,
model_get_coefficients_type()
,
model_get_contrasts()
,
model_get_model_frame()
,
model_get_model_matrix()
,
model_get_n()
,
model_get_nlevels()
,
model_get_offset()
,
model_get_pairwise_contrasts()
,
model_get_response()
,
model_get_response_variable()
,
model_get_terms()
,
model_get_weights()
,
model_get_xlevels()
,
model_identify_variables()
,
model_list_contrasts()
,
model_list_higher_order_variables()
,
model_list_terms_levels()
,
model_list_variables()
Examples
lm(hp ~ mpg + factor(cyl), mtcars) |>
model_get_model()
Get the model frame of a model
Description
The structure of the object returned by stats::model.frame()
could slightly differ for certain types of models.
model_get_model_frame()
will always return an object
with the same data structure or NULL
if it is not possible
to compute model frame from model
.
Usage
model_get_model_frame(model)
## Default S3 method:
model_get_model_frame(model)
## S3 method for class 'coxph'
model_get_model_frame(model)
## S3 method for class 'svycoxph'
model_get_model_frame(model)
## S3 method for class 'survreg'
model_get_model_frame(model)
## S3 method for class 'biglm'
model_get_model_frame(model)
## S3 method for class 'model_fit'
model_get_model_frame(model)
## S3 method for class 'fixest'
model_get_model_frame(model)
## S3 method for class 'svy_vglm'
model_get_model_frame(model)
Arguments
model |
(a model object, e.g. |
See Also
Other model_helpers:
model_compute_terms_contributions()
,
model_get_assign()
,
model_get_coefficients_type()
,
model_get_contrasts()
,
model_get_model()
,
model_get_model_matrix()
,
model_get_n()
,
model_get_nlevels()
,
model_get_offset()
,
model_get_pairwise_contrasts()
,
model_get_response()
,
model_get_response_variable()
,
model_get_terms()
,
model_get_weights()
,
model_get_xlevels()
,
model_identify_variables()
,
model_list_contrasts()
,
model_list_higher_order_variables()
,
model_list_terms_levels()
,
model_list_variables()
Examples
lm(hp ~ mpg + factor(cyl), mtcars) |>
model_get_model_frame() |>
head()
Get the model matrix of a model
Description
The structure of the object returned by stats::model.matrix()
could slightly differ for certain types of models.
model_get_model_matrix()
will always return an object
with the same structure as stats::model.matrix.default()
.
Usage
model_get_model_matrix(model, ...)
## Default S3 method:
model_get_model_matrix(model, ...)
## S3 method for class 'multinom'
model_get_model_matrix(model, ...)
## S3 method for class 'clm'
model_get_model_matrix(model, ...)
## S3 method for class 'brmsfit'
model_get_model_matrix(model, ...)
## S3 method for class 'glmmTMB'
model_get_model_matrix(model, ...)
## S3 method for class 'plm'
model_get_model_matrix(model, ...)
## S3 method for class 'biglm'
model_get_model_matrix(model, ...)
## S3 method for class 'model_fit'
model_get_model_matrix(model, ...)
## S3 method for class 'fixest'
model_get_model_matrix(model, ...)
## S3 method for class 'LORgee'
model_get_model_matrix(model, ...)
## S3 method for class 'betareg'
model_get_model_matrix(model, ...)
## S3 method for class 'cch'
model_get_model_matrix(model, ...)
## S3 method for class 'vglm'
model_get_model_matrix(model, ...)
## S3 method for class 'vgam'
model_get_model_matrix(model, ...)
## S3 method for class 'svy_vglm'
model_get_model_matrix(model, ...)
Arguments
model |
(a model object, e.g. |
... |
Additional arguments passed to |
Details
For models fitted with glmmTMB::glmmTMB()
, it will return a model matrix
taking into account all components ("cond", "zi" and "disp"). For a more
restricted model matrix, please refer to glmmTMB::model.matrix.glmmTMB()
.
For plm::plm()
models, constant columns are not removed.
For fixest
models, will recreate a model matrix with both main variables
and instrumental variables. For more options, see
fixest::model.matrix.fixest.
See Also
Other model_helpers:
model_compute_terms_contributions()
,
model_get_assign()
,
model_get_coefficients_type()
,
model_get_contrasts()
,
model_get_model()
,
model_get_model_frame()
,
model_get_n()
,
model_get_nlevels()
,
model_get_offset()
,
model_get_pairwise_contrasts()
,
model_get_response()
,
model_get_response_variable()
,
model_get_terms()
,
model_get_weights()
,
model_get_xlevels()
,
model_identify_variables()
,
model_list_contrasts()
,
model_list_higher_order_variables()
,
model_list_terms_levels()
,
model_list_variables()
Examples
lm(hp ~ mpg + factor(cyl), mtcars) |>
model_get_model_matrix() |>
head()
Get the number of observations
Description
For binomial and multinomial logistic models, will also return the number of events.
Usage
model_get_n(model)
## Default S3 method:
model_get_n(model)
## S3 method for class 'glm'
model_get_n(model)
## S3 method for class 'glmerMod'
model_get_n(model)
## S3 method for class 'multinom'
model_get_n(model)
## S3 method for class 'LORgee'
model_get_n(model)
## S3 method for class 'coxph'
model_get_n(model)
## S3 method for class 'survreg'
model_get_n(model)
## S3 method for class 'model_fit'
model_get_n(model)
## S3 method for class 'tidycrr'
model_get_n(model)
Arguments
model |
(a model object, e.g. |
Details
For Poisson models, will return the number of events and exposure time
(defined with stats::offset()
).
For Cox models (survival::coxph()
), will return the number of events,
exposure time and the number of individuals.
For competing risk regression models (tidycmprsk::crr()
), n_event
takes
into account only the event of interest defined by failcode.
See tidy_add_n()
for more details.
The total number of observations (N_obs
), of individuals (N_ind
), of
events (N_event
) and of exposure time (Exposure
) are stored as attributes
of the returned tibble.
This function does not cover lavaan
models (NULL
is returned).
See Also
Other model_helpers:
model_compute_terms_contributions()
,
model_get_assign()
,
model_get_coefficients_type()
,
model_get_contrasts()
,
model_get_model()
,
model_get_model_frame()
,
model_get_model_matrix()
,
model_get_nlevels()
,
model_get_offset()
,
model_get_pairwise_contrasts()
,
model_get_response()
,
model_get_response_variable()
,
model_get_terms()
,
model_get_weights()
,
model_get_xlevels()
,
model_identify_variables()
,
model_list_contrasts()
,
model_list_higher_order_variables()
,
model_list_terms_levels()
,
model_list_variables()
Examples
lm(hp ~ mpg + factor(cyl) + disp:hp, mtcars) |>
model_get_n()
mod <- glm(
response ~ stage * grade + trt,
gtsummary::trial,
family = binomial,
contrasts = list(stage = contr.sum, grade = contr.treatment(3, 2), trt = "contr.SAS")
)
mod |> model_get_n()
## Not run:
mod <- glm(
Survived ~ Class * Age + Sex,
data = Titanic |> as.data.frame(),
weights = Freq, family = binomial
)
mod |> model_get_n()
d <- dplyr::as_tibble(Titanic) |>
dplyr::group_by(Class, Sex, Age) |>
dplyr::summarise(
n_survived = sum(n * (Survived == "Yes")),
n_dead = sum(n * (Survived == "No"))
)
mod <- glm(cbind(n_survived, n_dead) ~ Class * Age + Sex, data = d, family = binomial)
mod |> model_get_n()
mod <- glm(response ~ age + grade * trt, gtsummary::trial, family = poisson)
mod |> model_get_n()
mod <- glm(
response ~ trt * grade + offset(ttdeath),
gtsummary::trial,
family = poisson
)
mod |> model_get_n()
dont
df <- survival::lung |> dplyr::mutate(sex = factor(sex))
mod <- survival::coxph(survival::Surv(time, status) ~ ph.ecog + age + sex, data = df)
mod |> model_get_n()
mod <- lme4::lmer(Reaction ~ Days + (Days | Subject), lme4::sleepstudy)
mod |> model_get_n()
mod <- lme4::glmer(response ~ trt * grade + (1 | stage),
family = binomial, data = gtsummary::trial
)
mod |> model_get_n()
mod <- lme4::glmer(cbind(incidence, size - incidence) ~ period + (1 | herd),
family = binomial, data = lme4::cbpp
)
mod |> model_get_n()
## End(Not run)
Get the number of levels for each factor used in xlevels
Description
Get the number of levels for each factor used in xlevels
Usage
model_get_nlevels(model)
## Default S3 method:
model_get_nlevels(model)
## S3 method for class 'svy_vglm'
model_get_nlevels(model)
Arguments
model |
(a model object, e.g. |
Value
a tibble with two columns: "variable"
and "var_nlevels"
See Also
Other model_helpers:
model_compute_terms_contributions()
,
model_get_assign()
,
model_get_coefficients_type()
,
model_get_contrasts()
,
model_get_model()
,
model_get_model_frame()
,
model_get_model_matrix()
,
model_get_n()
,
model_get_offset()
,
model_get_pairwise_contrasts()
,
model_get_response()
,
model_get_response_variable()
,
model_get_terms()
,
model_get_weights()
,
model_get_xlevels()
,
model_identify_variables()
,
model_list_contrasts()
,
model_list_higher_order_variables()
,
model_list_terms_levels()
,
model_list_variables()
Examples
lm(hp ~ mpg + factor(cyl), mtcars) |>
model_get_nlevels()
Get model offset
Description
This function does not cover lavaan
models (NULL
is returned).
Usage
model_get_offset(model)
## Default S3 method:
model_get_offset(model)
Arguments
model |
(a model object, e.g. |
See Also
Other model_helpers:
model_compute_terms_contributions()
,
model_get_assign()
,
model_get_coefficients_type()
,
model_get_contrasts()
,
model_get_model()
,
model_get_model_frame()
,
model_get_model_matrix()
,
model_get_n()
,
model_get_nlevels()
,
model_get_pairwise_contrasts()
,
model_get_response()
,
model_get_response_variable()
,
model_get_terms()
,
model_get_weights()
,
model_get_xlevels()
,
model_identify_variables()
,
model_list_contrasts()
,
model_list_higher_order_variables()
,
model_list_terms_levels()
,
model_list_variables()
Examples
mod <- glm(
response ~ trt + offset(log(ttdeath)),
gtsummary::trial,
family = poisson
)
mod |> model_get_offset()
Get pairwise comparison of the levels of a categorical variable
Description
It is computed with emmeans::emmeans()
.
Usage
model_get_pairwise_contrasts(
model,
variables,
pairwise_reverse = TRUE,
contrasts_adjust = NULL,
conf.level = 0.95,
emmeans_args = list()
)
Arguments
model |
(a model object, e.g. |
variables |
( |
pairwise_reverse |
( |
contrasts_adjust |
optional adjustment method when computing contrasts,
see |
conf.level |
( |
emmeans_args |
( |
Details
For pscl::zeroinfl()
and pscl::hurdle()
models, pairwise contrasts are
computed separately for each component, using mode = "count"
and
mode = "zero"
(see documentation of emmeans
) and a component column
is added to the results.
See Also
Other model_helpers:
model_compute_terms_contributions()
,
model_get_assign()
,
model_get_coefficients_type()
,
model_get_contrasts()
,
model_get_model()
,
model_get_model_frame()
,
model_get_model_matrix()
,
model_get_n()
,
model_get_nlevels()
,
model_get_offset()
,
model_get_response()
,
model_get_response_variable()
,
model_get_terms()
,
model_get_weights()
,
model_get_xlevels()
,
model_identify_variables()
,
model_list_contrasts()
,
model_list_higher_order_variables()
,
model_list_terms_levels()
,
model_list_variables()
Examples
mod <- lm(Sepal.Length ~ Species, data = iris)
mod |> model_get_pairwise_contrasts(variables = "Species")
mod |>
model_get_pairwise_contrasts(
variables = "Species",
contrasts_adjust = "none"
)
Get model response
Description
This function does not cover lavaan
models (NULL
is returned).
Usage
model_get_response(model)
## Default S3 method:
model_get_response(model)
## S3 method for class 'glm'
model_get_response(model)
## S3 method for class 'glmerMod'
model_get_response(model)
## S3 method for class 'model_fit'
model_get_response(model)
Arguments
model |
(a model object, e.g. |
See Also
Other model_helpers:
model_compute_terms_contributions()
,
model_get_assign()
,
model_get_coefficients_type()
,
model_get_contrasts()
,
model_get_model()
,
model_get_model_frame()
,
model_get_model_matrix()
,
model_get_n()
,
model_get_nlevels()
,
model_get_offset()
,
model_get_pairwise_contrasts()
,
model_get_response_variable()
,
model_get_terms()
,
model_get_weights()
,
model_get_xlevels()
,
model_identify_variables()
,
model_list_contrasts()
,
model_list_higher_order_variables()
,
model_list_terms_levels()
,
model_list_variables()
Examples
lm(hp ~ mpg + factor(cyl) + disp:hp, mtcars) |>
model_get_response()
mod <- glm(
response ~ stage * grade + trt,
gtsummary::trial,
family = binomial,
contrasts = list(stage = contr.sum, grade = contr.treatment(3, 2), trt = "contr.SAS")
)
mod |> model_get_response()
mod <- glm(
Survived ~ Class * Age + Sex,
data = Titanic |> as.data.frame(),
weights = Freq,
family = binomial
)
mod |> model_get_response()
d <- dplyr::as_tibble(Titanic) |>
dplyr::group_by(Class, Sex, Age) |>
dplyr::summarise(
n_survived = sum(n * (Survived == "Yes")),
n_dead = sum(n * (Survived == "No"))
)
mod <- glm(cbind(n_survived, n_dead) ~ Class * Age + Sex, data = d, family = binomial, y = FALSE)
mod |> model_get_response()
Get the name of the response variable
Description
Get the name of the response variable
Usage
model_get_response_variable(model)
## Default S3 method:
model_get_response_variable(model)
Arguments
model |
(a model object, e.g. |
See Also
Other model_helpers:
model_compute_terms_contributions()
,
model_get_assign()
,
model_get_coefficients_type()
,
model_get_contrasts()
,
model_get_model()
,
model_get_model_frame()
,
model_get_model_matrix()
,
model_get_n()
,
model_get_nlevels()
,
model_get_offset()
,
model_get_pairwise_contrasts()
,
model_get_response()
,
model_get_terms()
,
model_get_weights()
,
model_get_xlevels()
,
model_identify_variables()
,
model_list_contrasts()
,
model_list_higher_order_variables()
,
model_list_terms_levels()
,
model_list_variables()
Examples
lm(hp ~ mpg + factor(cyl) + disp:hp, mtcars) |>
model_get_response_variable()
mod <- glm(
response ~ stage * grade + trt,
gtsummary::trial,
family = binomial
)
mod |> model_get_response_variable()
mod <- glm(
Survived ~ Class * Age + Sex,
data = Titanic |> as.data.frame(),
weights = Freq,
family = binomial
)
mod |> model_get_response_variable()
Get the terms of a model
Description
Return the result of stats::terms()
applied to the model
or NULL
if it is not possible to get terms from model
.
Usage
model_get_terms(model)
## Default S3 method:
model_get_terms(model)
## S3 method for class 'brmsfit'
model_get_terms(model)
## S3 method for class 'glmmTMB'
model_get_terms(model)
## S3 method for class 'model_fit'
model_get_terms(model)
## S3 method for class 'betareg'
model_get_terms(model)
## S3 method for class 'betareg'
model_get_terms(model)
## S3 method for class 'cch'
model_get_terms(model)
## S3 method for class 'fixest'
model_get_terms(model)
## S3 method for class 'svy_vglm'
model_get_terms(model)
Arguments
model |
(a model object, e.g. |
Details
For models fitted with glmmTMB::glmmTMB()
, it will return a terms object
taking into account all components ("cond" and "zi"). For a more
restricted terms object, please refer to glmmTMB::terms.glmmTMB()
.
For fixest
models, return a term object combining main variables and
instrumental variables.
See Also
Other model_helpers:
model_compute_terms_contributions()
,
model_get_assign()
,
model_get_coefficients_type()
,
model_get_contrasts()
,
model_get_model()
,
model_get_model_frame()
,
model_get_model_matrix()
,
model_get_n()
,
model_get_nlevels()
,
model_get_offset()
,
model_get_pairwise_contrasts()
,
model_get_response()
,
model_get_response_variable()
,
model_get_weights()
,
model_get_xlevels()
,
model_identify_variables()
,
model_list_contrasts()
,
model_list_higher_order_variables()
,
model_list_terms_levels()
,
model_list_variables()
Examples
lm(hp ~ mpg + factor(cyl), mtcars) |>
model_get_terms()
Get sampling weights used by a model
Description
This function does not cover lavaan
models (NULL
is returned).
Usage
model_get_weights(model)
## Default S3 method:
model_get_weights(model)
## S3 method for class 'svyglm'
model_get_weights(model)
## S3 method for class 'svrepglm'
model_get_weights(model)
## S3 method for class 'model_fit'
model_get_weights(model)
## S3 method for class 'svy_vglm'
model_get_weights(model)
Arguments
model |
(a model object, e.g. |
Note
For class svrepglm
objects (GLM on a survey object with replicate weights),
it will return the original sampling weights of the data, not the replicate
weights.
See Also
Other model_helpers:
model_compute_terms_contributions()
,
model_get_assign()
,
model_get_coefficients_type()
,
model_get_contrasts()
,
model_get_model()
,
model_get_model_frame()
,
model_get_model_matrix()
,
model_get_n()
,
model_get_nlevels()
,
model_get_offset()
,
model_get_pairwise_contrasts()
,
model_get_response()
,
model_get_response_variable()
,
model_get_terms()
,
model_get_xlevels()
,
model_identify_variables()
,
model_list_contrasts()
,
model_list_higher_order_variables()
,
model_list_terms_levels()
,
model_list_variables()
Examples
mod <- lm(Sepal.Length ~ Sepal.Width, iris)
mod |> model_get_weights()
mod <- lm(hp ~ mpg + factor(cyl) + disp:hp, mtcars, weights = mtcars$gear)
mod |> model_get_weights()
mod <- glm(
response ~ stage * grade + trt,
gtsummary::trial,
family = binomial
)
mod |> model_get_weights()
mod <- glm(
Survived ~ Class * Age + Sex,
data = Titanic |> as.data.frame(),
weights = Freq,
family = binomial
)
mod |> model_get_weights()
d <- dplyr::as_tibble(Titanic) |>
dplyr::group_by(Class, Sex, Age) |>
dplyr::summarise(
n_survived = sum(n * (Survived == "Yes")),
n_dead = sum(n * (Survived == "No"))
)
mod <- glm(cbind(n_survived, n_dead) ~ Class * Age + Sex, data = d, family = binomial)
mod |> model_get_weights()
Get xlevels used in the model
Description
Get xlevels used in the model
Usage
model_get_xlevels(model)
## Default S3 method:
model_get_xlevels(model)
## S3 method for class 'lmerMod'
model_get_xlevels(model)
## S3 method for class 'glmerMod'
model_get_xlevels(model)
## S3 method for class 'felm'
model_get_xlevels(model)
## S3 method for class 'brmsfit'
model_get_xlevels(model)
## S3 method for class 'glmmTMB'
model_get_xlevels(model)
## S3 method for class 'plm'
model_get_xlevels(model)
## S3 method for class 'model_fit'
model_get_xlevels(model)
## S3 method for class 'svy_vglm'
model_get_xlevels(model)
Arguments
model |
(a model object, e.g. |
See Also
Other model_helpers:
model_compute_terms_contributions()
,
model_get_assign()
,
model_get_coefficients_type()
,
model_get_contrasts()
,
model_get_model()
,
model_get_model_frame()
,
model_get_model_matrix()
,
model_get_n()
,
model_get_nlevels()
,
model_get_offset()
,
model_get_pairwise_contrasts()
,
model_get_response()
,
model_get_response_variable()
,
model_get_terms()
,
model_get_weights()
,
model_identify_variables()
,
model_list_contrasts()
,
model_list_higher_order_variables()
,
model_list_terms_levels()
,
model_list_variables()
Examples
lm(hp ~ mpg + factor(cyl), mtcars) |>
model_get_xlevels()
Identify for each coefficient of a model the corresponding variable
Description
It will also identify interaction terms and intercept(s).
Usage
model_identify_variables(model)
## Default S3 method:
model_identify_variables(model)
## S3 method for class 'lavaan'
model_identify_variables(model)
## S3 method for class 'aov'
model_identify_variables(model)
## S3 method for class 'clm'
model_identify_variables(model)
## S3 method for class 'clmm'
model_identify_variables(model)
## S3 method for class 'gam'
model_identify_variables(model)
## S3 method for class 'model_fit'
model_identify_variables(model)
## S3 method for class 'logitr'
model_identify_variables(model)
## S3 method for class 'svy_vglm'
model_identify_variables(model)
Arguments
model |
(a model object, e.g. |
Value
A tibble with four columns:
-
term
: coefficients of the model -
variable
: the corresponding variable -
var_class
: class of the variable (cf.stats::.MFclass()
) -
var_type
:"continuous"
,"dichotomous"
(categorical variable with 2 levels),"categorical"
(categorical variable with 3 or more levels),"intercept"
or"interaction"
-
var_nlevels
: number of original levels for categorical variables
See Also
Other model_helpers:
model_compute_terms_contributions()
,
model_get_assign()
,
model_get_coefficients_type()
,
model_get_contrasts()
,
model_get_model()
,
model_get_model_frame()
,
model_get_model_matrix()
,
model_get_n()
,
model_get_nlevels()
,
model_get_offset()
,
model_get_pairwise_contrasts()
,
model_get_response()
,
model_get_response_variable()
,
model_get_terms()
,
model_get_weights()
,
model_get_xlevels()
,
model_list_contrasts()
,
model_list_higher_order_variables()
,
model_list_terms_levels()
,
model_list_variables()
Examples
df <- Titanic |>
dplyr::as_tibble() |>
dplyr::mutate(Survived = factor(Survived, c("No", "Yes")))
glm(
Survived ~ Class + Age * Sex,
data = df, weights = df$n,
family = binomial
) |>
model_identify_variables()
lm(
Sepal.Length ~ poly(Sepal.Width, 2) + Species,
data = iris,
contrasts = list(Species = contr.sum)
) |>
model_identify_variables()
List contrasts used by a model
Description
List contrasts used by a model
Usage
model_list_contrasts(model)
## Default S3 method:
model_list_contrasts(model)
Arguments
model |
(a model object, e.g. |
Details
For models with no intercept, no contrasts will be applied to one of the categorical variable. In such case, one dummy term will be returned for each level of the categorical variable.
Value
A tibble with three columns:
-
variable
: variable name -
contrasts
: contrasts used -
contrasts_type
: type of contrasts ("treatment", "sum", "poly", "helmert", "sdiff, "other" or "no.contrast") -
reference
: for variables with treatment, SAS or sum contrasts, position of the reference level
See Also
Other model_helpers:
model_compute_terms_contributions()
,
model_get_assign()
,
model_get_coefficients_type()
,
model_get_contrasts()
,
model_get_model()
,
model_get_model_frame()
,
model_get_model_matrix()
,
model_get_n()
,
model_get_nlevels()
,
model_get_offset()
,
model_get_pairwise_contrasts()
,
model_get_response()
,
model_get_response_variable()
,
model_get_terms()
,
model_get_weights()
,
model_get_xlevels()
,
model_identify_variables()
,
model_list_higher_order_variables()
,
model_list_terms_levels()
,
model_list_variables()
Examples
glm(
am ~ mpg + factor(cyl),
data = mtcars,
family = binomial,
contrasts = list(`factor(cyl)` = contr.sum)
) |>
model_list_contrasts()
List higher order variables of a model
Description
List higher order variables of a model
Usage
model_list_higher_order_variables(model)
## Default S3 method:
model_list_higher_order_variables(model)
Arguments
model |
(a model object, e.g. |
See Also
Other model_helpers:
model_compute_terms_contributions()
,
model_get_assign()
,
model_get_coefficients_type()
,
model_get_contrasts()
,
model_get_model()
,
model_get_model_frame()
,
model_get_model_matrix()
,
model_get_n()
,
model_get_nlevels()
,
model_get_offset()
,
model_get_pairwise_contrasts()
,
model_get_response()
,
model_get_response_variable()
,
model_get_terms()
,
model_get_weights()
,
model_get_xlevels()
,
model_identify_variables()
,
model_list_contrasts()
,
model_list_terms_levels()
,
model_list_variables()
Examples
lm(hp ~ mpg + factor(cyl) + disp:hp, mtcars) |>
model_list_higher_order_variables()
mod <- glm(
response ~ stage * grade + trt:stage,
gtsummary::trial,
family = binomial
)
mod |> model_list_higher_order_variables()
mod <- glm(
Survived ~ Class * Age + Sex,
data = Titanic |> as.data.frame(),
weights = Freq,
family = binomial
)
mod |> model_list_higher_order_variables()
List levels of categorical terms
Description
Only for categorical variables with treatment,
SAS, sum or successive differences contrasts (cf. MASS::contr.sdif()
), and
categorical variables with no contrast.
Usage
model_list_terms_levels(
model,
label_pattern = "{level}",
variable_labels = NULL,
sdif_term_level = c("diff", "ratio")
)
## Default S3 method:
model_list_terms_levels(
model,
label_pattern = "{level}",
variable_labels = NULL,
sdif_term_level = c("diff", "ratio")
)
Arguments
model |
(a model object, e.g. |
label_pattern |
( |
variable_labels |
( |
sdif_term_level |
( |
Value
A tibble with ten columns:
-
variable
: variable -
contrasts_type
: type of contrasts ("sum" or "treatment") -
term
: term name -
level
: term level -
level_rank
: rank of the level -
reference
: logical indicating which term is the reference level -
reference_level
: level of the reference term -
var_label
: variable label obtained withmodel_list_variables()
-
var_nlevels
: number of levels in this variable -
dichotomous
: logical indicating if the variable is dichotomous -
label
: term label (by default equal to term level) The first nine columns can be used inlabel_pattern
.
See Also
Other model_helpers:
model_compute_terms_contributions()
,
model_get_assign()
,
model_get_coefficients_type()
,
model_get_contrasts()
,
model_get_model()
,
model_get_model_frame()
,
model_get_model_matrix()
,
model_get_n()
,
model_get_nlevels()
,
model_get_offset()
,
model_get_pairwise_contrasts()
,
model_get_response()
,
model_get_response_variable()
,
model_get_terms()
,
model_get_weights()
,
model_get_xlevels()
,
model_identify_variables()
,
model_list_contrasts()
,
model_list_higher_order_variables()
,
model_list_variables()
Examples
glm(
am ~ mpg + factor(cyl),
data = mtcars,
family = binomial,
contrasts = list(`factor(cyl)` = contr.sum)
) |>
model_list_terms_levels()
df <- Titanic |>
dplyr::as_tibble() |>
dplyr::mutate(Survived = factor(Survived, c("No", "Yes")))
mod <- glm(
Survived ~ Class + Age + Sex,
data = df, weights = df$n, family = binomial,
contrasts = list(Age = contr.sum, Class = "contr.helmert")
)
mod |> model_list_terms_levels()
mod |> model_list_terms_levels("{level} vs {reference_level}")
mod |> model_list_terms_levels("{variable} [{level} - {reference_level}]")
mod |> model_list_terms_levels(
"{ifelse(reference, level, paste(level, '-', reference_level))}"
)
List all the variables used in a model
Description
Including variables used only in an interaction.
Usage
model_list_variables(
model,
labels = NULL,
only_variable = FALSE,
add_var_type = FALSE,
instrumental_suffix = " (instrumental)"
)
## Default S3 method:
model_list_variables(
model,
labels = NULL,
only_variable = FALSE,
add_var_type = FALSE,
instrumental_suffix = " (instrumental)"
)
## S3 method for class 'lavaan'
model_list_variables(
model,
labels = NULL,
only_variable = FALSE,
add_var_type = FALSE,
instrumental_suffix = " (instrumental)"
)
## S3 method for class 'logitr'
model_list_variables(
model,
labels = NULL,
only_variable = FALSE,
add_var_type = FALSE,
instrumental_suffix = " (instrumental)"
)
Arguments
model |
(a model object, e.g. |
labels |
( |
only_variable |
( |
add_var_type |
( |
instrumental_suffix |
( |
Value
A tibble with three columns:
-
variable
: the corresponding variable -
var_class
: class of the variable (cf.stats::.MFclass()
) -
label_attr
: variable label defined in the original data frame with the label attribute (cf.labelled::var_label()
) -
var_label
: a variable label (by priority,labels
if defined,label_attr
if available, otherwisevariable
)
If add_var_type = TRUE
:
-
var_type
:"continuous"
,"dichotomous"
(categorical variable with 2 levels),"categorical"
(categorical variable with 3 or more levels),"intercept"
or"interaction"
-
var_nlevels
: number of original levels for categorical variables
See Also
Other model_helpers:
model_compute_terms_contributions()
,
model_get_assign()
,
model_get_coefficients_type()
,
model_get_contrasts()
,
model_get_model()
,
model_get_model_frame()
,
model_get_model_matrix()
,
model_get_n()
,
model_get_nlevels()
,
model_get_offset()
,
model_get_pairwise_contrasts()
,
model_get_response()
,
model_get_response_variable()
,
model_get_terms()
,
model_get_weights()
,
model_get_xlevels()
,
model_identify_variables()
,
model_list_contrasts()
,
model_list_higher_order_variables()
,
model_list_terms_levels()
Examples
df <- Titanic |>
dplyr::as_tibble() |>
dplyr::mutate(Survived = factor(Survived, c("No", "Yes")))
glm(
Survived ~ Class + Age:Sex,
data = df, weights = df$n,
family = binomial
) |>
model_list_variables()
lm(
Sepal.Length ~ poly(Sepal.Width, 2) + Species,
data = iris,
contrasts = list(Species = contr.sum)
) |>
model_list_variables()
glm(
response ~ poly(age, 3) + stage + grade * trt,
na.omit(gtsummary::trial),
family = binomial,
) |>
model_list_variables()
Objects exported from other packages
Description
These objects are imported from other packages. Follow the links below to see their documentation.
- dplyr
all_of
,any_of
,contains
,ends_with
,everything
,last_col
,matches
,num_range
,one_of
,starts_with
,vars
,where
Scoping a tidy tibble allowing to tidy select
Description
This function uses the information from a model tidy tibble to generate
a data frame exposing the different variables of the model,
data frame that could be used for tidy selection. In addition, columns
"var_type"
, "var_class"
and "contrasts_type"
are scoped and their
values are added as attributes to the data frame.
For example, if var_type='continuous'
for variable "age"
, then the
attribute attr(.$age, 'gtsummary.var_type') <- 'continuous'
is set.
That attribute is then used in a selector like all_continuous()
.
Note: attributes are prefixed with "gtsummary."
to be compatible with
selectors provided by {gtsummary}
.
Usage
scope_tidy(x, data = NULL)
Arguments
x |
( |
data |
( |
Value
A data frame.
Examples
mod <- lm(Sepal.Length ~ Sepal.Width * Species, data = iris)
tt <- mod |> tidy_and_attach() |> tidy_add_contrasts()
scope_tidy(tt) |> str()
scope_tidy(tt, data = model_get_model_frame(mod)) |> str()
scope_tidy(tt) |> dplyr::select(dplyr::starts_with("Se")) |> names()
scope_tidy(tt) |> dplyr::select(where(is.factor)) |> names()
scope_tidy(tt) |> dplyr::select(all_continuous()) |> names()
scope_tidy(tt) |> dplyr::select(all_contrasts()) |> names()
scope_tidy(tt) |> dplyr::select(all_interaction()) |> names()
scope_tidy(tt) |> dplyr::select(all_intercepts()) |> names()
Select helper functions
Description
Set of functions to supplement the tidyselect set of functions for selecting columns of data frames (and other items as well).
-
all_continuous()
selects continuous variables -
all_categorical()
selects categorical (including"dichotomous"
) variables -
all_dichotomous()
selects only type"dichotomous"
-
all_interaction()
selects interaction terms from a regression model -
all_intercepts()
selects intercept terms from a regression model -
all_contrasts()
selects variables in regression model based on their type of contrast -
all_ran_pars()
andall_ran_vals()
for random-effect parameters and values from a mixed model (seevignette("broom_mixed_intro", package = "broom.mixed")
)
Usage
all_continuous(continuous2 = TRUE)
all_categorical(dichotomous = TRUE)
all_dichotomous()
all_interaction()
all_ran_pars()
all_ran_vals()
all_intercepts()
all_contrasts(
contrasts_type = c("treatment", "sum", "poly", "helmert", "sdif", "other")
)
Arguments
continuous2 |
( |
dichotomous |
( |
contrasts_type |
( |
Value
A character vector of column names selected.
See Also
Examples
glm(response ~ age * trt + grade, gtsummary::trial, family = binomial) |>
tidy_plus_plus(exponentiate = TRUE, include = all_categorical())
glm(response ~ age + trt + grade + stage,
gtsummary::trial,
family = binomial,
contrasts = list(trt = contr.SAS, grade = contr.sum, stage = contr.poly)
) |>
tidy_plus_plus(
exponentiate = TRUE,
include = all_contrasts(c("treatment", "sum"))
)
Sequence generation between min and max
Description
Sequence generation between min and max
Usage
seq_range(x, length.out = 25)
Arguments
x |
( |
length.out |
( |
Details
seq_range(x, length.out)
is a shortcut for
seq(min(x, na.rm = TRUE), max(x, na.rm = TRUE), length.out = length.out)
Value
a numeric vector
Examples
seq_range(iris$Petal.Length)
Listing of Supported Models
Description
Listing of Supported Models
Usage
supported_models
Format
A data frame with one row per supported model
- model
Model
- notes
Notes
Supported models
model | notes |
betareg::betareg() | Use tidy_parameters() as tidy_fun with component argument to control with coefficients to return. broom::tidy() does not support the exponentiate argument for betareg models, use tidy_parameters() instead. |
biglm::bigglm() | |
brms::brm() | broom.mixed package required |
cmprsk::crr() | Limited support. It is recommended to use tidycmprsk::crr() instead. |
fixest::feglm() | May fail with R <= 4.0. |
fixest::femlm() | May fail with R <= 4.0. |
fixest::feNmlm() | May fail with R <= 4.0. |
fixest::feols() | May fail with R <= 4.0. |
gam::gam() | |
geepack::geeglm() | |
glmmTMB::glmmTMB() | broom.mixed package required |
glmtoolbox::glmgee() | |
lavaan::lavaan() | Limited support for categorical variables |
lfe::felm() | |
lme4::glmer.nb() | broom.mixed package required |
lme4::glmer() | broom.mixed package required |
lme4::lmer() | broom.mixed package required |
logitr::logitr() | Requires logitr >= 0.8.0 |
MASS::glm.nb() | |
MASS::polr() | |
mgcv::gam() | Use default tidier broom::tidy() for smooth terms only, or gtsummary::tidy_gam() to include parametric terms |
mice::mira | Limited support. If mod is a mira object, use tidy_fun = function(x, ...) {mice::pool(x) |> mice::tidy(...)} |
mmrm::mmrm() | |
multgee::nomLORgee() | Use tidy_multgee() as tidy_fun . |
multgee::ordLORgee() | Use tidy_multgee() as tidy_fun . |
nnet::multinom() | |
ordinal::clm() | Limited support for models with nominal predictors. |
ordinal::clmm() | Limited support for models with nominal predictors. |
parsnip::model_fit | Supported as long as the type of model and the engine is supported. |
plm::plm() | |
pscl::hurdle() | Use tidy_zeroinfl() as tidy_fun . |
pscl::zeroinfl() | Use tidy_zeroinfl() as tidy_fun . |
rstanarm::stan_glm() | broom.mixed package required |
stats::aov() | Reference rows are not relevant for such models. |
stats::glm() | |
stats::lm() | |
stats::nls() | Limited support |
survey::svycoxph() | |
survey::svyglm() | |
survey::svyolr() | |
survival::cch() | Experimental support. |
survival::clogit() | |
survival::coxph() | |
survival::survreg() | |
svyVGAM::svy_vglm() | Experimental support. It is recommended to use tidy_svy_vglm() as tidy_fun . |
tidycmprsk::crr() | |
VGAM::vgam() | Experimental support. It is recommended to use tidy_vgam() as tidy_fun . |
VGAM::vglm() | Experimental support. It is recommended to use tidy_vgam() as tidy_fun . |
Add coefficients type and label as attributes
Description
Add the type of coefficients ("generic", "logistic", "poisson",
"relative_risk" or "prop_hazard") and the corresponding coefficient labels,
as attributes to x
(respectively
named coefficients_type
and coefficients_label
).
Usage
tidy_add_coefficients_type(
x,
exponentiate = attr(x, "exponentiate"),
model = tidy_get_model(x)
)
Arguments
x |
( |
exponentiate |
( |
model |
(a model object, e.g. |
See Also
Other tidy_helpers:
tidy_add_contrasts()
,
tidy_add_estimate_to_reference_rows()
,
tidy_add_header_rows()
,
tidy_add_n()
,
tidy_add_pairwise_contrasts()
,
tidy_add_reference_rows()
,
tidy_add_term_labels()
,
tidy_add_variable_labels()
,
tidy_attach_model()
,
tidy_disambiguate_terms()
,
tidy_group_by()
,
tidy_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
Examples
ex1 <- lm(hp ~ mpg + factor(cyl), mtcars) |>
tidy_and_attach() |>
tidy_add_coefficients_type()
attr(ex1, "coefficients_type")
attr(ex1, "coefficients_label")
df <- Titanic |>
dplyr::as_tibble() |>
dplyr::mutate(Survived = factor(Survived, c("No", "Yes")))
ex2 <- glm(
Survived ~ Class + Age * Sex,
data = df,
weights = df$n,
family = binomial
) |>
tidy_and_attach(exponentiate = TRUE) |>
tidy_add_coefficients_type()
attr(ex2, "coefficients_type")
attr(ex2, "coefficients_label")
Add contrasts type for categorical variables
Description
Add a contrasts
column corresponding to contrasts used for a
categorical variable and a contrasts_type
column equal to
"treatment", "sum", "poly", "helmert", "other" or "no.contrast".
Usage
tidy_add_contrasts(x, model = tidy_get_model(x), quiet = FALSE)
Arguments
x |
( |
model |
(a model object, e.g. |
quiet |
( |
Details
If the variable
column is not yet available in x
,
tidy_identify_variables()
will be automatically applied.
See Also
Other tidy_helpers:
tidy_add_coefficients_type()
,
tidy_add_estimate_to_reference_rows()
,
tidy_add_header_rows()
,
tidy_add_n()
,
tidy_add_pairwise_contrasts()
,
tidy_add_reference_rows()
,
tidy_add_term_labels()
,
tidy_add_variable_labels()
,
tidy_attach_model()
,
tidy_disambiguate_terms()
,
tidy_group_by()
,
tidy_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
Examples
df <- Titanic |>
dplyr::as_tibble() |>
dplyr::mutate(Survived = factor(Survived, c("No", "Yes")))
glm(
Survived ~ Class + Age + Sex,
data = df, weights = df$n, family = binomial,
contrasts = list(Age = contr.sum, Class = "contr.helmert")
) |>
tidy_and_attach() |>
tidy_add_contrasts()
Add an estimate value to references rows for categorical variables
Description
For categorical variables with a treatment contrast
(stats::contr.treatment()
) or a SAS contrast (stats::contr.SAS()
),
will add an estimate equal to 0
(or 1
if exponentiate = TRUE
)
to the reference row.
Usage
tidy_add_estimate_to_reference_rows(
x,
exponentiate = attr(x, "exponentiate"),
conf.level = attr(x, "conf.level"),
model = tidy_get_model(x),
quiet = FALSE
)
Arguments
x |
( |
exponentiate |
( |
conf.level |
( |
model |
(a model object, e.g. |
quiet |
( |
Details
For categorical variables with a sum contrast (stats::contr.sum()
),
the estimate value of the reference row will be equal to the sum of
all other coefficients multiplied by -1
(eventually exponentiated if
exponentiate = TRUE
), and obtained with emmeans::emmeans()
.
The emmeans
package should therefore be installed.
For sum contrasts, the model coefficient corresponds
to the difference of each level with the grand mean.
For sum contrasts, confidence intervals and p-values will also
be computed and added to the reference rows.
For other variables, no change will be made.
If the reference_row
column is not yet available in x
,
tidy_add_reference_rows()
will be automatically applied.
See Also
Other tidy_helpers:
tidy_add_coefficients_type()
,
tidy_add_contrasts()
,
tidy_add_header_rows()
,
tidy_add_n()
,
tidy_add_pairwise_contrasts()
,
tidy_add_reference_rows()
,
tidy_add_term_labels()
,
tidy_add_variable_labels()
,
tidy_attach_model()
,
tidy_disambiguate_terms()
,
tidy_group_by()
,
tidy_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
Examples
df <- Titanic |>
dplyr::as_tibble() |>
dplyr::mutate(dplyr::across(where(is.character), factor))
glm(
Survived ~ Class + Age + Sex,
data = df, weights = df$n, family = binomial,
contrasts = list(Age = contr.sum, Class = "contr.SAS")
) |>
tidy_and_attach(exponentiate = TRUE) |>
tidy_add_reference_rows() |>
tidy_add_estimate_to_reference_rows()
glm(
response ~ stage + grade * trt,
gtsummary::trial,
family = binomial,
contrasts = list(
stage = contr.treatment(4, base = 3),
grade = contr.treatment(3, base = 2),
trt = contr.treatment(2, base = 2)
)
) |>
tidy_and_attach() |>
tidy_add_reference_rows() |>
tidy_add_estimate_to_reference_rows()
Add header rows variables with several terms
Description
For variables with several terms (usually categorical variables but
could also be the case of continuous variables with polynomial terms
or splines), tidy_add_header_rows()
will add an additional row
per variable, where label
will be equal to var_label
.
These additional rows could be identified with header_row
column.
Usage
tidy_add_header_rows(
x,
show_single_row = NULL,
model = tidy_get_model(x),
quiet = FALSE,
strict = FALSE
)
Arguments
x |
( |
show_single_row |
( |
model |
(a model object, e.g. |
quiet |
( |
strict |
( |
Details
The show_single_row
argument allows to specify a list
of dichotomous variables that should be displayed on a single row
instead of two rows.
The added header_row
column will be equal to:
-
TRUE
for an header row; -
FALSE
for a normal row of a variable with an header row; -
NA
for variables without an header row.
If the label
column is not yet available in x
,
tidy_add_term_labels()
will be automatically applied.
See Also
Other tidy_helpers:
tidy_add_coefficients_type()
,
tidy_add_contrasts()
,
tidy_add_estimate_to_reference_rows()
,
tidy_add_n()
,
tidy_add_pairwise_contrasts()
,
tidy_add_reference_rows()
,
tidy_add_term_labels()
,
tidy_add_variable_labels()
,
tidy_attach_model()
,
tidy_disambiguate_terms()
,
tidy_group_by()
,
tidy_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
Examples
df <- Titanic |>
dplyr::as_tibble() |>
dplyr::mutate(Survived = factor(Survived, c("No", "Yes")))
res <-
glm(
Survived ~ Class + Age + Sex,
data = df, weights = df$n, family = binomial,
contrasts = list(Age = contr.sum, Class = "contr.SAS")
) |>
tidy_and_attach() |>
tidy_add_variable_labels(labels = list(Class = "Custom label for Class")) |>
tidy_add_reference_rows()
res |> tidy_add_header_rows()
res |> tidy_add_header_rows(show_single_row = all_dichotomous())
glm(
response ~ stage + grade * trt,
gtsummary::trial,
family = binomial,
contrasts = list(
stage = contr.treatment(4, base = 3),
grade = contr.treatment(3, base = 2),
trt = contr.treatment(2, base = 2)
)
) |>
tidy_and_attach() |>
tidy_add_reference_rows() |>
tidy_add_header_rows()
Add the (weighted) number of observations
Description
Add the number of observations in a new column n_obs
, taking into account any
weights if they have been defined.
Usage
tidy_add_n(x, model = tidy_get_model(x))
Arguments
x |
( |
model |
(a model object, e.g. |
Details
For continuous variables, it corresponds to all valid observations contributing to the model.
For categorical variables coded with treatment or sum contrasts,
each model term could be associated to only one level of the original
categorical variable. Therefore, n_obs
will correspond to the number of
observations associated with that level. n_obs
will also be computed for
reference rows. For polynomial contrasts (defined with stats::contr.poly()
),
all levels will contribute to the computation of each model term. Therefore,
n_obs
will be equal to the total number of observations. For Helmert and custom
contrasts, only rows contributing positively (i.e. with a positive contrast)
to the computation of a term will be considered for estimating n_obs
. The
result could therefore be difficult to interpret. For a better understanding
of which observations are taken into account to compute n_obs
values, you
could look at model_compute_terms_contributions()
.
For interaction terms, only rows contributing to all the terms of the
interaction will be considered to compute n_obs
.
For binomial logistic models, tidy_add_n()
will also return the
corresponding number of events (n_event
) for each term, taking into account
any defined weights. Observed proportions could be obtained as n_obs / n_event
.
Similarly, a number of events will be computed for multinomial logistic
models (nnet::multinom()
) for each level of the outcome (y.level
),
corresponding to the number of observations equal to that outcome level.
For Poisson models, n_event
will be equal to the number of counts per term.
In addition, a third column exposure
will be computed. If no offset is
defined, exposure is assumed to be equal to 1 (eventually multiplied by
weights) per observation. If an offset is defined, exposure
will be equal
to the (weighted) sum of the exponential of the offset (as a reminder, to
model the effect of x
on the ratio y / z
, a Poisson model will be defined
as glm(y ~ x + offset(log(z)), family = poisson)
). Observed rates could be
obtained with n_event / exposure
.
For Cox models (survival::coxph()
), an individual could be coded
with several observations (several rows). n_obs
will correspond to the
weighted number of observations which could be different from the number of
individuals n_ind
. tidy_add_n()
will also compute a (weighted) number of
events (n_event
) according to the definition of the survival::Surv()
object.
Exposure time is also returned in exposure
column. It is equal to the
(weighted) sum of the time variable if only one variable time is passed to
survival::Surv()
, and to the (weighted) sum of time2 - time
if two time
variables are defined in survival::Surv()
.
For competing risk regression models (tidycmprsk::crr()
), n_event
takes
into account only the event of interest defined by failcode.
The (weighted) total number of observations (N_obs
), of individuals
(N_ind
), of events (N_event
) and of exposure time (Exposure
) are
stored as attributes of the returned tibble.
See Also
Other tidy_helpers:
tidy_add_coefficients_type()
,
tidy_add_contrasts()
,
tidy_add_estimate_to_reference_rows()
,
tidy_add_header_rows()
,
tidy_add_pairwise_contrasts()
,
tidy_add_reference_rows()
,
tidy_add_term_labels()
,
tidy_add_variable_labels()
,
tidy_attach_model()
,
tidy_disambiguate_terms()
,
tidy_group_by()
,
tidy_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
Examples
lm(Petal.Length ~ ., data = iris) |>
tidy_and_attach() |>
tidy_add_n()
lm(Petal.Length ~ ., data = iris, contrasts = list(Species = contr.sum)) |>
tidy_and_attach() |>
tidy_add_n()
lm(Petal.Length ~ ., data = iris, contrasts = list(Species = contr.poly)) |>
tidy_and_attach() |>
tidy_add_n()
lm(Petal.Length ~ poly(Sepal.Length, 2), data = iris) |>
tidy_and_attach() |>
tidy_add_n()
df <- Titanic |>
dplyr::as_tibble() |>
dplyr::mutate(Survived = factor(Survived, c("No", "Yes")))
glm(
Survived ~ Class + Age + Sex,
data = df, weights = df$n, family = binomial,
contrasts = list(Age = contr.sum, Class = "contr.helmert")
) |>
tidy_and_attach() |>
tidy_add_n()
glm(
Survived ~ Class * (Age:Sex),
data = df, weights = df$n, family = binomial,
contrasts = list(Age = contr.sum, Class = "contr.helmert")
) |>
tidy_and_attach() |>
tidy_add_n()
glm(response ~ age + grade * trt, gtsummary::trial, family = poisson) |>
tidy_and_attach() |>
tidy_add_n()
glm(
response ~ trt * grade + offset(log(ttdeath)),
gtsummary::trial,
family = poisson
) |>
tidy_and_attach() |>
tidy_add_n()
Add pairwise contrasts for categorical variables
Description
Computes pairwise contrasts with emmeans::emmeans()
and add them to the
results tibble. Works only with models supported by emmeans
, see
vignette("models", package = "emmeans")
.
Usage
tidy_add_pairwise_contrasts(
x,
variables = all_categorical(),
keep_model_terms = FALSE,
pairwise_reverse = TRUE,
contrasts_adjust = NULL,
conf.level = attr(x, "conf.level"),
emmeans_args = list(),
model = tidy_get_model(x),
quiet = FALSE
)
Arguments
x |
( |
variables |
include ( |
keep_model_terms |
( |
pairwise_reverse |
( |
contrasts_adjust |
( |
conf.level |
( |
emmeans_args |
( |
model |
(a model object, e.g. |
quiet |
( |
Note
If the contrasts
column is not yet available in x
,
tidy_add_contrasts()
will be automatically applied.
For multi-components models, such as zero-inflated Poisson or beta regression, support of pairwise contrasts is still experimental.
See Also
Other tidy_helpers:
tidy_add_coefficients_type()
,
tidy_add_contrasts()
,
tidy_add_estimate_to_reference_rows()
,
tidy_add_header_rows()
,
tidy_add_n()
,
tidy_add_reference_rows()
,
tidy_add_term_labels()
,
tidy_add_variable_labels()
,
tidy_attach_model()
,
tidy_disambiguate_terms()
,
tidy_group_by()
,
tidy_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
Examples
mod1 <- lm(Sepal.Length ~ Species, data = iris)
mod1 |>
tidy_and_attach() |>
tidy_add_pairwise_contrasts()
mod1 |>
tidy_and_attach() |>
tidy_add_pairwise_contrasts(pairwise_reverse = FALSE)
mod1 |>
tidy_and_attach() |>
tidy_add_pairwise_contrasts(keep_model_terms = TRUE)
mod1 |>
tidy_and_attach() |>
tidy_add_pairwise_contrasts(contrasts_adjust = "none")
if (.assert_package("gtsummary", boolean = TRUE)) {
mod2 <- glm(
response ~ age + trt + grade,
data = gtsummary::trial,
family = binomial
)
mod2 |>
tidy_and_attach(exponentiate = TRUE) |>
tidy_add_pairwise_contrasts()
}
Add references rows for categorical variables
Description
For categorical variables with a treatment contrast
(stats::contr.treatment()
), a SAS contrast (stats::contr.SAS()
)
a sum contrast (stats::contr.sum()
), or successive differences contrast
(MASS::contr.sdif()
) add a reference row.
Usage
tidy_add_reference_rows(
x,
no_reference_row = NULL,
model = tidy_get_model(x),
quiet = FALSE
)
Arguments
x |
( |
no_reference_row |
( |
model |
(a model object, e.g. |
quiet |
( |
Details
The added reference_row
column will be equal to:
-
TRUE
for a reference row; -
FALSE
for a normal row of a variable with a reference row; -
NA
for variables without a reference row.
If the contrasts
column is not yet available in x
,
tidy_add_contrasts()
will be automatically applied.
tidy_add_reference_rows()
will not populate the label
of the reference term. It is therefore better to apply
tidy_add_term_labels()
after tidy_add_reference_rows()
rather than before. Similarly, it is better to apply
tidy_add_reference_rows()
before tidy_add_n()
.
See Also
Other tidy_helpers:
tidy_add_coefficients_type()
,
tidy_add_contrasts()
,
tidy_add_estimate_to_reference_rows()
,
tidy_add_header_rows()
,
tidy_add_n()
,
tidy_add_pairwise_contrasts()
,
tidy_add_term_labels()
,
tidy_add_variable_labels()
,
tidy_attach_model()
,
tidy_disambiguate_terms()
,
tidy_group_by()
,
tidy_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
Examples
df <- Titanic |>
dplyr::as_tibble() |>
dplyr::mutate(Survived = factor(Survived, c("No", "Yes")))
res <-
glm(
Survived ~ Class + Age + Sex,
data = df, weights = df$n, family = binomial,
contrasts = list(Age = contr.sum, Class = "contr.SAS")
) |>
tidy_and_attach()
res |> tidy_add_reference_rows()
res |> tidy_add_reference_rows(no_reference_row = all_dichotomous())
res |> tidy_add_reference_rows(no_reference_row = "Class")
glm(
response ~ stage + grade * trt,
gtsummary::trial,
family = binomial,
contrasts = list(
stage = contr.treatment(4, base = 3),
grade = contr.treatment(3, base = 2),
trt = contr.treatment(2, base = 2)
)
) |>
tidy_and_attach() |>
tidy_add_reference_rows()
Add term labels
Description
Will add term labels in a label
column, based on:
labels provided in
labels
argument if provided;factor levels for categorical variables coded with treatment, SAS or sum contrasts (the label could be customized with
categorical_terms_pattern
argument);variable labels when there is only one term per variable;
term name otherwise.
Usage
tidy_add_term_labels(
x,
labels = NULL,
interaction_sep = " * ",
categorical_terms_pattern = "{level}",
relabel_poly = FALSE,
model = tidy_get_model(x),
quiet = FALSE,
strict = FALSE
)
Arguments
x |
( |
labels |
( |
interaction_sep |
( |
categorical_terms_pattern |
( |
relabel_poly |
Should terms generated with |
model |
(a model object, e.g. |
quiet |
( |
strict |
( |
Details
If the variable_label
column is not yet available in x
,
tidy_add_variable_labels()
will be automatically applied.
If the contrasts
column is not yet available in x
,
tidy_add_contrasts()
will be automatically applied.
It is possible to pass a custom label for any term in labels
,
including interaction terms.
See Also
Other tidy_helpers:
tidy_add_coefficients_type()
,
tidy_add_contrasts()
,
tidy_add_estimate_to_reference_rows()
,
tidy_add_header_rows()
,
tidy_add_n()
,
tidy_add_pairwise_contrasts()
,
tidy_add_reference_rows()
,
tidy_add_variable_labels()
,
tidy_attach_model()
,
tidy_disambiguate_terms()
,
tidy_group_by()
,
tidy_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
Examples
df <- Titanic |>
dplyr::as_tibble() |>
dplyr::mutate(Survived = factor(Survived, c("No", "Yes"))) |>
labelled::set_variable_labels(
Class = "Passenger's class",
Sex = "Sex"
)
mod <-
glm(Survived ~ Class * Age * Sex, data = df, weights = df$n, family = binomial)
mod |>
tidy_and_attach() |>
tidy_add_term_labels()
mod |>
tidy_and_attach() |>
tidy_add_term_labels(
interaction_sep = " x ",
categorical_terms_pattern = "{level} / {reference_level}"
)
Add variable labels
Description
Will add variable labels in a var_label
column, based on:
labels provided in
labels
argument if provided;variable labels defined in the original data frame with the
label
attribute (cf.labelled::var_label()
);variable name otherwise.
Usage
tidy_add_variable_labels(
x,
labels = NULL,
interaction_sep = " * ",
instrumental_suffix = " (instrumental)",
model = tidy_get_model(x)
)
Arguments
x |
( |
labels |
( |
interaction_sep |
( |
instrumental_suffix |
( |
model |
(a model object, e.g. |
Details
If the variable
column is not yet available in x
,
tidy_identify_variables()
will be automatically applied.
It is possible to pass a custom label for an interaction
term in labels
(see examples).
See Also
Other tidy_helpers:
tidy_add_coefficients_type()
,
tidy_add_contrasts()
,
tidy_add_estimate_to_reference_rows()
,
tidy_add_header_rows()
,
tidy_add_n()
,
tidy_add_pairwise_contrasts()
,
tidy_add_reference_rows()
,
tidy_add_term_labels()
,
tidy_attach_model()
,
tidy_disambiguate_terms()
,
tidy_group_by()
,
tidy_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
Examples
df <- Titanic |>
dplyr::as_tibble() |>
dplyr::mutate(Survived = factor(Survived, c("No", "Yes"))) |>
labelled::set_variable_labels(
Class = "Passenger's class",
Sex = "Sex"
)
glm(Survived ~ Class * Age * Sex, data = df, weights = df$n, family = binomial) |>
tidy_and_attach() |>
tidy_add_variable_labels(
labels = list(
"(Intercept)" ~ "Custom intercept",
Sex ~ "Gender",
"Class:Age" ~ "Custom label"
)
)
Marginal Predictions at the mean with effects::allEffects()
Description
Use effects::allEffects()
to estimate marginal predictions and
return a tibble tidied in a way that it could be used by broom.helpers
functions.
See vignette("functions-supported-by-effects", package = "effects")
for
a list of supported models.
Usage
tidy_all_effects(x, conf.int = TRUE, conf.level = 0.95, ...)
Arguments
x |
(a model object, e.g. |
conf.int |
( |
conf.level |
( |
... |
Additional parameters passed to |
Details
By default, effects::allEffects()
estimate marginal predictions at the mean
at the observed means for continuous variables and weighting modalities
of categorical variables according to their observed distribution in the
original dataset. Marginal predictions are therefore computed at
a sort of averaged situation / typical values for the other variables fixed
in the model.
For more information, see vignette("marginal_tidiers", "broom.helpers")
.
Note
If the model contains interactions, effects::allEffects()
will return
marginal predictions for the different levels of the interactions.
See Also
effects::allEffects()
Other marginal_tieders:
tidy_avg_comparisons()
,
tidy_avg_slopes()
,
tidy_ggpredict()
,
tidy_marginal_contrasts()
,
tidy_marginal_predictions()
,
tidy_margins()
Examples
df <- Titanic |>
dplyr::as_tibble() |>
tidyr::uncount(n) |>
dplyr::mutate(Survived = factor(Survived, c("No", "Yes")))
mod <- glm(
Survived ~ Class + Age + Sex,
data = df, family = binomial
)
tidy_all_effects(mod)
tidy_plus_plus(mod, tidy_fun = tidy_all_effects)
Attach a full model to the tibble of model terms
Description
To facilitate the use of broom helpers with pipe, it is recommended to
attach the original model as an attribute to the tibble of model terms
generated by broom::tidy()
.
Usage
tidy_attach_model(x, model, .attributes = NULL)
tidy_and_attach(
model,
tidy_fun = tidy_with_broom_or_parameters,
conf.int = TRUE,
conf.level = 0.95,
exponentiate = FALSE,
model_matrix_attr = TRUE,
...
)
tidy_get_model(x)
tidy_detach_model(x)
Arguments
x |
( |
model |
(a model object, e.g. |
.attributes |
( |
tidy_fun |
( |
conf.int |
( |
conf.level |
( |
exponentiate |
( |
model_matrix_attr |
( |
... |
Other arguments passed to |
Details
tidy_attach_model()
attach the model to a tibble already generated while
tidy_and_attach()
will apply broom::tidy()
and attach the model.
Use tidy_get_model()
to get the model attached to the tibble and
tidy_detach_model()
to remove the attribute containing the model.
See Also
Other tidy_helpers:
tidy_add_coefficients_type()
,
tidy_add_contrasts()
,
tidy_add_estimate_to_reference_rows()
,
tidy_add_header_rows()
,
tidy_add_n()
,
tidy_add_pairwise_contrasts()
,
tidy_add_reference_rows()
,
tidy_add_term_labels()
,
tidy_add_variable_labels()
,
tidy_disambiguate_terms()
,
tidy_group_by()
,
tidy_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
Examples
mod <- lm(Sepal.Length ~ Sepal.Width + Species, data = iris)
tt <- mod |>
tidy_and_attach(conf.int = TRUE)
tt
tidy_get_model(tt)
Marginal Contrasts with marginaleffects::avg_comparisons()
Description
Use marginaleffects::avg_comparisons()
to estimate marginal contrasts and
return a tibble tidied in a way that it could be used by broom.helpers
functions. See marginaleffects::avg_comparisons()
for a list of supported
models.
Usage
tidy_avg_comparisons(x, conf.int = TRUE, conf.level = 0.95, ...)
Arguments
x |
(a model object, e.g. |
conf.int |
( |
conf.level |
( |
... |
Additional parameters passed to
|
Details
By default, marginaleffects::avg_comparisons()
estimate average marginal
contrasts: a contrast is computed for each observed value in the original
dataset (counterfactual approach) before being averaged.
Marginal Contrasts at the Mean could be computed by specifying
newdata = "mean"
. The variables
argument can be used to select the
contrasts to be computed. Please refer to the documentation page of
marginaleffects::avg_comparisons()
.
See also tidy_marginal_contrasts()
for taking into account interactions.
For more information, see vignette("marginal_tidiers", "broom.helpers")
.
See Also
marginaleffects::avg_comparisons()
Other marginal_tieders:
tidy_all_effects()
,
tidy_avg_slopes()
,
tidy_ggpredict()
,
tidy_marginal_contrasts()
,
tidy_marginal_predictions()
,
tidy_margins()
Examples
# Average Marginal Contrasts
df <- Titanic |>
dplyr::as_tibble() |>
tidyr::uncount(n) |>
dplyr::mutate(Survived = factor(Survived, c("No", "Yes")))
mod <- glm(
Survived ~ Class + Age + Sex,
data = df, family = binomial
)
tidy_avg_comparisons(mod)
tidy_plus_plus(mod, tidy_fun = tidy_avg_comparisons)
mod2 <- lm(Petal.Length ~ poly(Petal.Width, 2) + Species, data = iris)
tidy_avg_comparisons(mod2)
# Custumizing the type of contrasts
tidy_avg_comparisons(
mod2,
variables = list(Petal.Width = 2, Species = "pairwise")
)
# Marginal Contrasts at the Mean
tidy_avg_comparisons(mod, newdata = "mean")
tidy_plus_plus(mod, tidy_fun = tidy_avg_comparisons, newdata = "mean")
Marginal Slopes / Effects with marginaleffects::avg_slopes()
Description
Use marginaleffects::avg_slopes()
to estimate marginal slopes / effects and
return a tibble tidied in a way that it could be used by broom.helpers
functions. See marginaleffects::avg_slopes()
for a list of supported
models.
Usage
tidy_avg_slopes(x, conf.int = TRUE, conf.level = 0.95, ...)
Arguments
x |
(a model object, e.g. |
conf.int |
( |
conf.level |
( |
... |
Additional parameters passed to
|
Details
By default, marginaleffects::avg_slopes()
estimate average marginal
effects (AME): an effect is computed for each observed value in the original
dataset before being averaged. Marginal Effects at the Mean (MEM) could be
computed by specifying newdata = "mean"
. Other types of marginal effects
could be computed. Please refer to the documentation page of
marginaleffects::avg_slopes()
.
For more information, see vignette("marginal_tidiers", "broom.helpers")
.
See Also
marginaleffects::avg_slopes()
Other marginal_tieders:
tidy_all_effects()
,
tidy_avg_comparisons()
,
tidy_ggpredict()
,
tidy_marginal_contrasts()
,
tidy_marginal_predictions()
,
tidy_margins()
Examples
# Average Marginal Effects (AME)
df <- Titanic |>
dplyr::as_tibble() |>
tidyr::uncount(n) |>
dplyr::mutate(Survived = factor(Survived, c("No", "Yes")))
mod <- glm(
Survived ~ Class + Age + Sex,
data = df, family = binomial
)
tidy_avg_slopes(mod)
tidy_plus_plus(mod, tidy_fun = tidy_avg_slopes)
mod2 <- lm(Petal.Length ~ poly(Petal.Width, 2) + Species, data = iris)
tidy_avg_slopes(mod2)
# Marginal Effects at the Mean (MEM)
tidy_avg_slopes(mod, newdata = "mean")
tidy_plus_plus(mod, tidy_fun = tidy_avg_slopes, newdata = "mean")
Tidy with broom::tidy()
and checks that all arguments are used
Description
Tidy with broom::tidy()
and checks that all arguments are used
Usage
tidy_broom(x, ...)
Arguments
x |
(a model object, e.g. |
... |
Additional parameters passed to |
See Also
Other custom_tieders:
tidy_multgee()
,
tidy_parameters()
,
tidy_svy_vglm()
,
tidy_vgam()
,
tidy_with_broom_or_parameters()
,
tidy_zeroinfl()
Disambiguate terms
Description
For mixed models, the term
column returned by broom.mixed
may have
duplicated values for random-effect parameters and random-effect values.
In such case, the terms could be disambiguated be prefixing them with the
value of the group
column. tidy_disambiguate_terms()
will not change
any term if there is no group
column in x
. The original term value
is kept in a new column original_term
.
Usage
tidy_disambiguate_terms(x, sep = ".", model = tidy_get_model(x), quiet = FALSE)
Arguments
x |
( |
sep |
( |
model |
(a model object, e.g. |
quiet |
( |
See Also
Other tidy_helpers:
tidy_add_coefficients_type()
,
tidy_add_contrasts()
,
tidy_add_estimate_to_reference_rows()
,
tidy_add_header_rows()
,
tidy_add_n()
,
tidy_add_pairwise_contrasts()
,
tidy_add_reference_rows()
,
tidy_add_term_labels()
,
tidy_add_variable_labels()
,
tidy_attach_model()
,
tidy_group_by()
,
tidy_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
Examples
if (
.assert_package("lme4", boolean = TRUE) &&
.assert_package("broom.mixed", boolean = TRUE) &&
.assert_package("gtsummary", boolean = TRUE)
) {
mod <- lme4::lmer(marker ~ stage + (1 | grade) + (death | response), gtsummary::trial)
mod |>
tidy_and_attach() |>
tidy_disambiguate_terms()
}
Marginal Predictions with ggeffects::ggpredict()
Description
Use ggeffects::ggpredict()
to estimate marginal predictions
and return a tibble tidied in a way that it could be used by broom.helpers
functions.
See https://strengejacke.github.io/ggeffects/ for a list of supported
models.
Usage
tidy_ggpredict(x, conf.int = TRUE, conf.level = 0.95, ...)
Arguments
x |
(a model object, e.g. |
conf.int |
( |
conf.level |
( |
... |
Additional parameters passed to |
Details
By default, ggeffects::ggpredict()
estimate marginal predictions at the
observed mean of continuous variables and at the first modality of categorical
variables (regardless of the type of contrasts used in the model).
For more information, see vignette("marginal_tidiers", "broom.helpers")
.
Note
By default, ggeffects::ggpredict()
estimates marginal predictions for each
individual variable, regardless of eventual interactions.
See Also
ggeffects::ggpredict()
Other marginal_tieders:
tidy_all_effects()
,
tidy_avg_comparisons()
,
tidy_avg_slopes()
,
tidy_marginal_contrasts()
,
tidy_marginal_predictions()
,
tidy_margins()
Examples
df <- Titanic |>
dplyr::as_tibble() |>
tidyr::uncount(n) |>
dplyr::mutate(Survived = factor(Survived, c("No", "Yes")))
mod <- glm(
Survived ~ Class + Age + Sex,
data = df, family = binomial
)
tidy_ggpredict(mod)
tidy_plus_plus(mod, tidy_fun = tidy_ggpredict)
Group results by selected columns
Description
Indicates that results should be grouped. By default
(group_by = auto_group_by()
), results will be grouped according to the
y.level
column (for multinomial models) or the component
column
(multi-components models) if any.
Usage
tidy_group_by(
x,
group_by = auto_group_by(),
group_labels = NULL,
model = tidy_get_model(x)
)
auto_group_by()
Arguments
x |
( |
group_by |
( |
group_labels |
( |
model |
(a model object, e.g. |
Value
The x
tibble with, if relevant, an additional group_by
column.
See Also
Other tidy_helpers:
tidy_add_coefficients_type()
,
tidy_add_contrasts()
,
tidy_add_estimate_to_reference_rows()
,
tidy_add_header_rows()
,
tidy_add_n()
,
tidy_add_pairwise_contrasts()
,
tidy_add_reference_rows()
,
tidy_add_term_labels()
,
tidy_add_variable_labels()
,
tidy_attach_model()
,
tidy_disambiguate_terms()
,
tidy_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
Examples
mod <- multinom(Species ~ Petal.Width + Petal.Length, data = iris)
mod |> tidy_and_attach() |> tidy_group_by()
mod |>
tidy_and_attach() |>
tidy_group_by(group_labels = c(versicolor = "harlequin blueflag"))
mod |> tidy_and_attach() |> tidy_group_by(group_by = NULL)
mod |>
tidy_and_attach() |>
tidy_identify_variables() |>
tidy_group_by(group_by = variable)
Identify the variable corresponding to each model coefficient
Description
tidy_identify_variables()
will add to the tidy tibble
three additional columns: variable
, var_class
, var_type
and var_nlevels
.
Usage
tidy_identify_variables(x, model = tidy_get_model(x), quiet = FALSE)
Arguments
x |
( |
model |
(a model object, e.g. |
quiet |
( |
Details
It will also identify interaction terms and intercept(s).
var_type
could be:
-
"continuous"
, -
"dichotomous"
(categorical variable with 2 levels), -
"categorical"
(categorical variable with 3 levels or more), -
"intercept"
-
"interaction"
-
"ran_pars
(random-effect parameters for mixed models) -
"ran_vals"
(random-effect values for mixed models) -
"unknown"
in the rare cases wheretidy_identify_variables()
will fail to identify the list of variables
For dichotomous and categorical variables, var_nlevels
corresponds to the number
of original levels in the corresponding variables.
For fixest
models, a new column instrumental
is added to indicate
instrumental variables.
See Also
Other tidy_helpers:
tidy_add_coefficients_type()
,
tidy_add_contrasts()
,
tidy_add_estimate_to_reference_rows()
,
tidy_add_header_rows()
,
tidy_add_n()
,
tidy_add_pairwise_contrasts()
,
tidy_add_reference_rows()
,
tidy_add_term_labels()
,
tidy_add_variable_labels()
,
tidy_attach_model()
,
tidy_disambiguate_terms()
,
tidy_group_by()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
Examples
df <- Titanic |>
dplyr::as_tibble() |>
dplyr::mutate(Survived = factor(Survived, c("No", "Yes")))
glm(
Survived ~ Class + Age * Sex,
data = df,
weights = df$n,
family = binomial
) |>
tidy_and_attach() |>
tidy_identify_variables()
lm(
Sepal.Length ~ poly(Sepal.Width, 2) + Species,
data = iris,
contrasts = list(Species = contr.sum)
) |>
tidy_and_attach(conf.int = TRUE) |>
tidy_identify_variables()
Marginal Contrasts with marginaleffects::avg_comparisons()
Description
Use marginaleffects::avg_comparisons()
to estimate marginal contrasts for
each variable of a model and return a tibble tidied in a way that it could
be used by broom.helpers
functions.
See marginaleffects::avg_comparisons()
for a list of supported models.
Usage
tidy_marginal_contrasts(
x,
variables_list = "auto",
conf.int = TRUE,
conf.level = 0.95,
...
)
variables_to_contrast(
model,
interactions = TRUE,
cross = FALSE,
var_categorical = "reference",
var_continuous = 1,
by_categorical = unique,
by_continuous = stats::fivenum
)
Arguments
x |
(a model object, e.g. |
variables_list |
( |
conf.int |
( |
conf.level |
( |
... |
Additional parameters passed to
|
model |
(a model object, e.g. |
interactions |
( |
cross |
( |
var_categorical |
( |
var_continuous |
( |
by_categorical |
( |
by_continuous |
( |
Details
Marginal contrasts are obtained by calling, for each variable or combination
of variables, marginaleffects::avg_comparisons()
.
tidy_marginal_contrasts()
will compute marginal contrasts for each
variable or combination of variables, before stacking the results in a unique
tibble. This is why tidy_marginal_contrasts()
has a variables_list
argument consisting of a list of specifications that will be passed
sequentially to the variables
and the by
argument of
marginaleffects::avg_comparisons()
.
Considering a single categorical variable named cat
, tidy_marginal_contrasts()
will call avg_comparisons(model, variables = list(cat = "reference"))
to obtain average marginal contrasts for this variable.
Considering a single continuous variable named cont
, tidy_marginalcontrasts()
will call avg_comparisons(model, variables = list(cont = 1))
to obtain average marginal contrasts for an increase of one unit.
For a combination of variables, there are several possibilities. You could
compute "cross-contrasts" by providing simultaneously several variables
to variables
and specifying cross = TRUE
to
marginaleffects::avg_comparisons()
. Alternatively, you could compute the
contrasts of a first variable specified to variables
for the
different values of a second variable specified to by
.
The helper function variables_to_contrast()
could be used to automatically
generate a suitable list to be used with variables_list
. Each combination
of variables should be a list with two named elements: "variables"
a list
of named elements passed to variables
and "by"
a list of named elements
used for creating a relevant datagrid
and whose names are passed to by
.
variables_list
's default value, "auto"
, calls
variables_to_contrast(interactions = TRUE, cross = FALSE)
while
"no_interaction"
is a shortcut for
variables_to_contrast(interactions = FALSE)
. "cross"
calls
variables_to_contrast(interactions = TRUE, cross = TRUE)
You can also provide custom specifications (see examples).
By default, average marginal contrasts are computed: contrasts are computed
using a counterfactual grid for each value of the variable of interest,
before averaging the results. Marginal contrasts at the mean could be
obtained by indicating newdata = "mean"
. Other assumptions are possible,
see the help file of marginaleffects::avg_comparisons()
.
For more information, see vignette("marginal_tidiers", "broom.helpers")
.
See Also
marginaleffects::avg_comparisons()
, tidy_avg_comparisons()
Other marginal_tieders:
tidy_all_effects()
,
tidy_avg_comparisons()
,
tidy_avg_slopes()
,
tidy_ggpredict()
,
tidy_marginal_predictions()
,
tidy_margins()
Examples
# Average Marginal Contrasts
df <- Titanic |>
dplyr::as_tibble() |>
tidyr::uncount(n) |>
dplyr::mutate(Survived = factor(Survived, c("No", "Yes")))
mod <- glm(
Survived ~ Class + Age + Sex,
data = df, family = binomial
)
tidy_marginal_contrasts(mod)
tidy_plus_plus(mod, tidy_fun = tidy_marginal_contrasts)
mod2 <- lm(Petal.Length ~ poly(Petal.Width, 2) + Species, data = iris)
tidy_marginal_contrasts(mod2)
tidy_marginal_contrasts(
mod2,
variables_list = variables_to_predict(
mod2,
continuous = 3,
categorical = "pairwise"
)
)
# Model with interactions
mod3 <- glm(
Survived ~ Sex * Age + Class,
data = df, family = binomial
)
tidy_marginal_contrasts(mod3)
tidy_marginal_contrasts(mod3, "no_interaction")
tidy_marginal_contrasts(mod3, "cross")
tidy_marginal_contrasts(
mod3,
variables_list = list(
list(variables = list(Class = "pairwise"), by = list(Sex = unique)),
list(variables = list(Age = "all")),
list(variables = list(Class = "sequential", Sex = "reference"))
)
)
mod4 <- lm(Sepal.Length ~ Petal.Length * Petal.Width + Species, data = iris)
tidy_marginal_contrasts(mod4)
tidy_marginal_contrasts(
mod4,
variables_list = list(
list(
variables = list(Species = "sequential"),
by = list(Petal.Length = c(2, 5))
),
list(
variables = list(Petal.Length = 2),
by = list(Species = unique, Petal.Width = 2:4)
)
)
)
# Marginal Contrasts at the Mean
tidy_marginal_contrasts(mod, newdata = "mean")
tidy_marginal_contrasts(mod3, newdata = "mean")
Marginal Means with deprecated marginaleffects::marginal_means()
Description
This function is deprecated.
marginal_means()
is not anymore exported
by marginaleffects
. Use instead tidy_marginal_predictions()
with
the option newdata = "balanced"
.
Usage
tidy_marginal_means(x, conf.int = TRUE, conf.level = 0.95, ...)
Arguments
x |
(a model object, e.g. |
conf.int |
( |
conf.level |
( |
... |
Additional parameters. |
Marginal Predictions with marginaleffects::avg_predictions()
Description
Use marginaleffects::avg_predictions()
to estimate marginal predictions for
each variable of a model and return a tibble tidied in a way that it could
be used by broom.helpers
functions.
See marginaleffects::avg_predictions()
for a list of supported models.
Usage
tidy_marginal_predictions(
x,
variables_list = "auto",
conf.int = TRUE,
conf.level = 0.95,
...
)
variables_to_predict(
model,
interactions = TRUE,
categorical = unique,
continuous = stats::fivenum
)
plot_marginal_predictions(x, variables_list = "auto", conf.level = 0.95, ...)
Arguments
x |
(a model object, e.g. |
variables_list |
( |
conf.int |
( |
conf.level |
( |
... |
Additional parameters passed to
|
model |
(a model object, e.g. |
interactions |
( |
categorical |
( |
continuous |
( |
Details
Marginal predictions are obtained by calling, for each variable,
marginaleffects::avg_predictions()
with the same variable being used for
the variables
and the by
argument.
Considering a categorical variable named cat
, tidy_marginal_predictions()
will call avg_predictions(model, variables = list(cat = unique), by = "cat")
to obtain average marginal predictions for this variable.
Considering a continuous variable named cont
, tidy_marginal_predictions()
will call avg_predictions(model, variables = list(cont = "fivenum"), by = "cont")
to obtain average marginal predictions for this variable at the minimum, the
first quartile, the median, the third quartile and the maximum of the observed
values of cont
.
By default, average marginal predictions are computed: predictions are made
using a counterfactual grid for each value of the variable of interest,
before averaging the results. Marginal predictions at the mean could be
obtained by indicating newdata = "mean"
. Other assumptions are possible,
see the help file of marginaleffects::avg_predictions()
.
tidy_marginal_predictions()
will compute marginal predictions for each
variable or combination of variables, before stacking the results in a unique
tibble. This is why tidy_marginal_predictions()
has a variables_list
argument consisting of a list of specifications that will be passed
sequentially to the variables
argument of marginaleffects::avg_predictions()
.
The helper function variables_to_predict()
could be used to automatically
generate a suitable list to be used with variables_list
. By default, all
unique values are retained for categorical variables and fivenum
(i.e.
Tukey's five numbers, minimum, quartiles and maximum) for continuous variables.
When interactions = FALSE
, variables_to_predict()
will return a list of
all individual variables used in the model. If interactions = FALSE
, it
will search for higher order combinations of variables (see
model_list_higher_order_variables()
).
variables_list
's default value, "auto"
, calls
variables_to_predict(interactions = TRUE)
while "no_interaction"
is a
shortcut for variables_to_predict(interactions = FALSE)
.
You can also provide custom specifications (see examples).
plot_marginal_predictions()
works in a similar way and returns a list of
plots that could be combined with patchwork::wrap_plots()
(see examples).
For more information, see vignette("marginal_tidiers", "broom.helpers")
.
See Also
marginaleffects::avg_predictions()
Other marginal_tieders:
tidy_all_effects()
,
tidy_avg_comparisons()
,
tidy_avg_slopes()
,
tidy_ggpredict()
,
tidy_marginal_contrasts()
,
tidy_margins()
Examples
# example code
# Average Marginal Predictions
df <- Titanic |>
dplyr::as_tibble() |>
tidyr::uncount(n) |>
dplyr::mutate(Survived = factor(Survived, c("No", "Yes")))
mod <- glm(
Survived ~ Class + Age + Sex,
data = df, family = binomial
)
tidy_marginal_predictions(mod)
tidy_plus_plus(mod, tidy_fun = tidy_marginal_predictions)
if (require("patchwork")) {
plot_marginal_predictions(mod) |> patchwork::wrap_plots()
plot_marginal_predictions(mod) |>
patchwork::wrap_plots() &
ggplot2::scale_y_continuous(limits = c(0, 1), label = scales::percent)
}
mod2 <- lm(Petal.Length ~ poly(Petal.Width, 2) + Species, data = iris)
tidy_marginal_predictions(mod2)
if (require("patchwork")) {
plot_marginal_predictions(mod2) |> patchwork::wrap_plots()
}
tidy_marginal_predictions(
mod2,
variables_list = variables_to_predict(mod2, continuous = "threenum")
)
tidy_marginal_predictions(
mod2,
variables_list = list(
list(Petal.Width = c(0, 1, 2, 3)),
list(Species = unique)
)
)
tidy_marginal_predictions(
mod2,
variables_list = list(list(Species = unique, Petal.Width = 1:3))
)
# Model with interactions
mod3 <- glm(
Survived ~ Sex * Age + Class,
data = df, family = binomial
)
tidy_marginal_predictions(mod3)
tidy_marginal_predictions(mod3, "no_interaction")
if (require("patchwork")) {
plot_marginal_predictions(mod3) |>
patchwork::wrap_plots()
plot_marginal_predictions(mod3, "no_interaction") |>
patchwork::wrap_plots()
}
tidy_marginal_predictions(
mod3,
variables_list = list(
list(Class = unique, Sex = "Female"),
list(Age = unique)
)
)
# Marginal Predictions at the Mean
tidy_marginal_predictions(mod, newdata = "mean")
if (require("patchwork")) {
plot_marginal_predictions(mod, newdata = "mean") |>
patchwork::wrap_plots()
}
Average Marginal Effects with margins::margins()
Description
Usage
tidy_margins(x, conf.int = TRUE, conf.level = 0.95, ...)
Arguments
x |
(a model object, e.g. |
conf.int |
( |
conf.level |
( |
... |
Additional parameters passed to |
Details
The margins
package is no longer under active development and may be
removed from CRAN sooner or later. It is advised to use the marginaleffects
package instead, offering more functionalities. You could have a look at the
article
dedicated to marginal estimates with broom.helpers
. tidy_avg_slopes()
could be used as an alternative.
Use margins::margins()
to estimate average marginal effects (AME) and
return a tibble tidied in a way that it could be used by broom.helpers
functions. See margins::margins()
for a list of supported models.
By default, margins::margins()
estimate average marginal effects (AME): an
effect is computed for each observed value in the original dataset before
being averaged.
For more information, see vignette("marginal_tidiers", "broom.helpers")
.
Note
When applying margins::margins()
, custom contrasts are ignored.
Treatment contrasts (stats::contr.treatment()
) are applied to all
categorical variables. Interactions are also ignored.
See Also
margins::margins()
Other marginal_tieders:
tidy_all_effects()
,
tidy_avg_comparisons()
,
tidy_avg_slopes()
,
tidy_ggpredict()
,
tidy_marginal_contrasts()
,
tidy_marginal_predictions()
Examples
df <- Titanic |>
dplyr::as_tibble() |>
tidyr::uncount(n) |>
dplyr::mutate(Survived = factor(Survived, c("No", "Yes")))
mod <- glm(
Survived ~ Class + Age + Sex,
data = df, family = binomial
)
tidy_margins(mod)
tidy_plus_plus(mod, tidy_fun = tidy_margins)
Tidy a multgee
model
Description
A tidier for models generated with multgee::nomLORgee()
or multgee::ordLORgee()
.
Term names will be updated to be consistent with generic models. The original
term names are preserved in an "original_term"
column.
Usage
tidy_multgee(x, conf.int = TRUE, conf.level = 0.95, ...)
Arguments
x |
( |
conf.int |
( |
conf.level |
( |
... |
Additional parameters passed to |
Details
To be noted, for multgee::nomLORgee()
, the baseline y
category is the
latest modality of y
.
See Also
Other custom_tieders:
tidy_broom()
,
tidy_parameters()
,
tidy_svy_vglm()
,
tidy_vgam()
,
tidy_with_broom_or_parameters()
,
tidy_zeroinfl()
Examples
library(multgee)
h <- housing
h$status <- factor(
h$y,
labels = c("street", "community", "independant")
)
mod <- multgee::nomLORgee(
status ~ factor(time) * sec,
data = h,
id = id,
repeated = time,
)
mod |> tidy_multgee()
mod2 <- ordLORgee(
formula = y ~ factor(time) + factor(trt) + factor(baseline),
data = multgee::arthritis,
id = id,
repeated = time,
LORstr = "uniform"
)
mod2 |> tidy_multgee()
Tidy a model with parameters package
Description
Use parameters::model_parameters()
to tidy a model and apply
parameters::standardize_names(style = "broom")
to the output
Usage
tidy_parameters(x, conf.int = TRUE, conf.level = 0.95, ...)
Arguments
x |
(a model object, e.g. |
conf.int |
( |
conf.level |
( |
... |
Additional parameters passed to |
Note
For betareg::betareg()
, the component column in the results is standardized
with broom::tidy()
, using "mean"
and "precision"
values.
See Also
Other custom_tieders:
tidy_broom()
,
tidy_multgee()
,
tidy_svy_vglm()
,
tidy_vgam()
,
tidy_with_broom_or_parameters()
,
tidy_zeroinfl()
Examples
lm(Sepal.Length ~ Sepal.Width + Species, data = iris) |>
tidy_parameters()
Tidy a model and compute additional informations
Description
This function will apply sequentially:
Usage
tidy_plus_plus(
model,
tidy_fun = tidy_with_broom_or_parameters,
conf.int = TRUE,
conf.level = 0.95,
exponentiate = FALSE,
model_matrix_attr = TRUE,
variable_labels = NULL,
instrumental_suffix = " (instrumental)",
term_labels = NULL,
interaction_sep = " * ",
categorical_terms_pattern = "{level}",
relabel_poly = FALSE,
disambiguate_terms = TRUE,
disambiguate_sep = ".",
add_reference_rows = TRUE,
no_reference_row = NULL,
add_pairwise_contrasts = FALSE,
pairwise_variables = all_categorical(),
keep_model_terms = FALSE,
pairwise_reverse = TRUE,
contrasts_adjust = NULL,
emmeans_args = list(),
add_estimate_to_reference_rows = TRUE,
add_header_rows = FALSE,
show_single_row = NULL,
add_n = TRUE,
intercept = FALSE,
include = everything(),
group_by = auto_group_by(),
group_labels = NULL,
keep_model = FALSE,
tidy_post_fun = NULL,
quiet = FALSE,
strict = FALSE,
...
)
Arguments
model |
(a model object, e.g. |
tidy_fun |
( |
conf.int |
( |
conf.level |
( |
exponentiate |
( |
model_matrix_attr |
( |
variable_labels |
( |
instrumental_suffix |
( |
term_labels |
( |
interaction_sep |
( |
categorical_terms_pattern |
( |
relabel_poly |
Should terms generated with |
disambiguate_terms |
( |
disambiguate_sep |
( |
add_reference_rows |
( |
no_reference_row |
( |
add_pairwise_contrasts |
( |
pairwise_variables |
( |
keep_model_terms |
( |
pairwise_reverse |
( |
contrasts_adjust |
( |
emmeans_args |
( |
add_estimate_to_reference_rows |
( |
add_header_rows |
( |
show_single_row |
( |
add_n |
( |
intercept |
( |
include |
( |
group_by |
( |
group_labels |
( |
keep_model |
( |
tidy_post_fun |
( |
quiet |
( |
strict |
( |
... |
other arguments passed to |
Note
tidy_post_fun
is applied to the result at the end of tidy_plus_plus()
and receive only one argument (the result of tidy_plus_plus()
). However,
if needed, the model is still attached to the tibble as an attribute, even
if keep_model = FALSE
. Therefore, it is possible to use tidy_get_model()
within tidy_fun
if, for any reason, you need to access the source model.
See Also
Other tidy_helpers:
tidy_add_coefficients_type()
,
tidy_add_contrasts()
,
tidy_add_estimate_to_reference_rows()
,
tidy_add_header_rows()
,
tidy_add_n()
,
tidy_add_pairwise_contrasts()
,
tidy_add_reference_rows()
,
tidy_add_term_labels()
,
tidy_add_variable_labels()
,
tidy_attach_model()
,
tidy_disambiguate_terms()
,
tidy_group_by()
,
tidy_identify_variables()
,
tidy_remove_intercept()
,
tidy_select_variables()
Examples
ex1 <- lm(Sepal.Length ~ Sepal.Width + Species, data = iris) |>
tidy_plus_plus()
ex1
df <- Titanic |>
dplyr::as_tibble() |>
dplyr::mutate(
Survived = factor(Survived, c("No", "Yes"))
) |>
labelled::set_variable_labels(
Class = "Passenger's class",
Sex = "Gender"
)
ex2 <- glm(
Survived ~ Class + Age * Sex,
data = df, weights = df$n,
family = binomial
) |>
tidy_plus_plus(
exponentiate = TRUE,
add_reference_rows = FALSE,
categorical_terms_pattern = "{level} / {reference_level}",
add_n = TRUE
)
ex2
ex3 <-
glm(
response ~ poly(age, 3) + stage + grade * trt,
na.omit(gtsummary::trial),
family = binomial,
contrasts = list(
stage = contr.treatment(4, base = 3),
grade = contr.sum
)
) |>
tidy_plus_plus(
exponentiate = TRUE,
variable_labels = c(age = "Age (in years)"),
add_header_rows = TRUE,
show_single_row = all_dichotomous(),
term_labels = c("poly(age, 3)3" = "Cubic age"),
keep_model = TRUE
)
ex3
Remove intercept(s)
Description
Will remove terms where var_type == "intercept"
.
Usage
tidy_remove_intercept(x, model = tidy_get_model(x))
Arguments
x |
( |
model |
(a model object, e.g. |
Details
If the variable
column is not yet available in x
,
tidy_identify_variables()
will be automatically applied.
See Also
Other tidy_helpers:
tidy_add_coefficients_type()
,
tidy_add_contrasts()
,
tidy_add_estimate_to_reference_rows()
,
tidy_add_header_rows()
,
tidy_add_n()
,
tidy_add_pairwise_contrasts()
,
tidy_add_reference_rows()
,
tidy_add_term_labels()
,
tidy_add_variable_labels()
,
tidy_attach_model()
,
tidy_disambiguate_terms()
,
tidy_group_by()
,
tidy_identify_variables()
,
tidy_plus_plus()
,
tidy_select_variables()
Examples
df <- Titanic |>
dplyr::as_tibble() |>
dplyr::mutate(Survived = factor(Survived))
glm(Survived ~ Class + Age + Sex, data = df, weights = df$n, family = binomial) |>
tidy_and_attach() |>
tidy_remove_intercept()
Select variables to keep/drop
Description
Will remove unselected variables from the results.
To remove the intercept, use tidy_remove_intercept()
.
Usage
tidy_select_variables(x, include = everything(), model = tidy_get_model(x))
Arguments
x |
( |
include |
( |
model |
(a model object, e.g. |
Details
If the variable
column is not yet available in x
,
tidy_identify_variables()
will be automatically applied.
Value
The x
tibble limited to the included variables (and eventually the intercept),
sorted according to the include
parameter.
See Also
Other tidy_helpers:
tidy_add_coefficients_type()
,
tidy_add_contrasts()
,
tidy_add_estimate_to_reference_rows()
,
tidy_add_header_rows()
,
tidy_add_n()
,
tidy_add_pairwise_contrasts()
,
tidy_add_reference_rows()
,
tidy_add_term_labels()
,
tidy_add_variable_labels()
,
tidy_attach_model()
,
tidy_disambiguate_terms()
,
tidy_group_by()
,
tidy_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
Examples
df <- Titanic |>
dplyr::as_tibble() |>
dplyr::mutate(Survived = factor(Survived))
res <-
glm(Survived ~ Class + Age * Sex, data = df, weights = df$n, family = binomial) |>
tidy_and_attach() |>
tidy_identify_variables()
res
res |> tidy_select_variables()
res |> tidy_select_variables(include = "Class")
res |> tidy_select_variables(include = -c("Age", "Sex"))
res |> tidy_select_variables(include = starts_with("A"))
res |> tidy_select_variables(include = all_categorical())
res |> tidy_select_variables(include = all_dichotomous())
res |> tidy_select_variables(include = all_interaction())
res |> tidy_select_variables(
include = c("Age", all_categorical(dichotomous = FALSE), all_interaction())
)
Tidy a svy_vglm
model
Description
A tidier for models generated with
svyVGAM::svy_vglm()
.
Term names will be updated to be consistent with generic models. The original
term names are preserved in an "original_term"
column. Depending on the
model, additional column "group"
, "component"
and/or "y.level"
may be
added to the results.
Usage
tidy_svy_vglm(x, conf.int = TRUE, conf.level = 0.95, ...)
Arguments
x |
( |
conf.int |
( |
conf.level |
( |
... |
Additional parameters passed to |
See Also
Other custom_tieders:
tidy_broom()
,
tidy_multgee()
,
tidy_parameters()
,
tidy_vgam()
,
tidy_with_broom_or_parameters()
,
tidy_zeroinfl()
Examples
library(svyVGAM)
mod <- svy_vglm(
Species ~ Sepal.Length + Sepal.Width,
family = multinomial(),
design = survey::svydesign(~1, data = iris)
)
mod |> tidy_svy_vglm(exponentiate = TRUE)
Tidy a vglm
or a vgam
model
Description
A tidier for models generated with
VGAM::vglm()
or VGAM::vgam()
.
Term names will be updated to be consistent with generic models. The original
term names are preserved in an "original_term"
column. Depending on the
model, additional column "group"
, "component"
and/or "y.level"
may be
added to the results.
Usage
tidy_vgam(x, conf.int = TRUE, conf.level = 0.95, ...)
Arguments
x |
( |
conf.int |
( |
conf.level |
( |
... |
Additional parameters passed to |
See Also
Other custom_tieders:
tidy_broom()
,
tidy_multgee()
,
tidy_parameters()
,
tidy_svy_vglm()
,
tidy_with_broom_or_parameters()
,
tidy_zeroinfl()
Examples
library(VGAM)
mod <- vglm(
Species ~ Sepal.Length + Sepal.Width,
family = multinomial(),
data = iris
)
mod |> tidy_vgam(exponentiate = TRUE)
mod <- vglm(
Species ~ Sepal.Length + Sepal.Width,
family = multinomial(parallel = TRUE),
data = iris
)
mod |> tidy_vgam(exponentiate = TRUE)
Tidy a model with broom or parameters
Description
Try to tidy a model with broom::tidy()
. If it fails, will try to tidy the
model using parameters::model_parameters()
through tidy_parameters()
.
Usage
tidy_with_broom_or_parameters(x, conf.int = TRUE, conf.level = 0.95, ...)
Arguments
x |
(a model object, e.g. |
conf.int |
( |
conf.level |
( |
... |
Additional parameters passed to |
See Also
Other custom_tieders:
tidy_broom()
,
tidy_multgee()
,
tidy_parameters()
,
tidy_svy_vglm()
,
tidy_vgam()
,
tidy_zeroinfl()
Tidy a zeroinfl
or a hurdle
model
Description
A tidier for models generated with pscl::zeroinfl()
or pscl::hurdle()
.
Term names will be updated to be consistent with generic models. The original
term names are preserved in an "original_term"
column.
Usage
tidy_zeroinfl(x, conf.int = TRUE, conf.level = 0.95, component = NULL, ...)
Arguments
x |
( |
conf.int |
( |
conf.level |
( |
component |
( |
... |
Additional parameters passed to |
See Also
Other custom_tieders:
tidy_broom()
,
tidy_multgee()
,
tidy_parameters()
,
tidy_svy_vglm()
,
tidy_vgam()
,
tidy_with_broom_or_parameters()
Examples
library(pscl)
mod <- zeroinfl(
art ~ fem + mar + phd,
data = pscl::bioChemists
)
mod |> tidy_zeroinfl(exponentiate = TRUE)