Title: | Helpers for Model Coefficients Tibbles |
---|---|
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. |
Authors: | Joseph Larmarange [aut, cre] , Daniel D. Sjoberg [aut] |
Maintainer: | Joseph Larmarange <[email protected]> |
License: | GPL (>= 3) |
Version: | 1.17.0 |
Built: | 2024-10-27 05:37:22 UTC |
Source: | https://github.com/larmarange/broom.helpers |
Remove backticks around variable names
.clean_backticks(x, variable_names = x)
.clean_backticks(x, variable_names = x)
x |
( |
variable_names |
( |
Other other_helpers:
.escape_regex()
This functions has been adapted from Hmisc::escapeRegex()
.escape_regex(string)
.escape_regex(string)
string |
( |
Other other_helpers:
.clean_backticks()
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.
.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")
.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")
pkg |
( |
fn |
( |
pkg_search |
( |
boolean |
( |
remove_duplicates |
( |
lib.loc |
( |
get_all_packages_dependencies()
could be used to get the list of
dependencies of all installed packages.
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()
.
.assert_package("broom", boolean = TRUE) .get_package_dependencies() .get_min_version_required("brms")
.assert_package("broom", boolean = TRUE) .get_package_dependencies() .get_min_version_required("brms")
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.
model_compute_terms_contributions(model) ## Default S3 method: model_compute_terms_contributions(model)
model_compute_terms_contributions(model) ## Default S3 method: model_compute_terms_contributions(model)
model |
(a model object, e.g. |
This function does not cover lavaan
models (NULL
is returned).
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()
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()
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()
Return the assign attribute attached to the object returned by
stats::model.matrix()
.
model_get_assign(model) ## Default S3 method: model_get_assign(model) ## S3 method for class 'vglm' model_get_assign(model) ## S3 method for class 'model_fit' model_get_assign(model)
model_get_assign(model) ## Default S3 method: model_get_assign(model) ## S3 method for class 'vglm' model_get_assign(model) ## S3 method for class 'model_fit' model_get_assign(model)
model |
(a model object, e.g. |
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()
lm(hp ~ mpg + factor(cyl), mtcars) |> model_get_assign()
lm(hp ~ mpg + factor(cyl), mtcars) |> model_get_assign()
Indicate the type of coefficient among "generic", "logistic", "poisson", "relative_risk" or "prop_hazard".
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)
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)
model |
(a model object, e.g. |
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()
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()
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
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)
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)
model |
(a model object, e.g. |
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()
glm( am ~ mpg + factor(cyl), data = mtcars, family = binomial, contrasts = list(`factor(cyl)` = contr.sum) ) |> model_get_contrasts()
glm( am ~ mpg + factor(cyl), data = mtcars, family = binomial, contrasts = list(`factor(cyl)` = contr.sum) ) |> model_get_contrasts()
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.
model_get_model(model) ## Default S3 method: model_get_model(model) ## S3 method for class 'mira' model_get_model(model)
model_get_model(model) ## Default S3 method: model_get_model(model) ## S3 method for class 'mira' model_get_model(model)
model |
(a model object, e.g. |
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()
lm(hp ~ mpg + factor(cyl), mtcars) |> model_get_model()
lm(hp ~ mpg + factor(cyl), mtcars) |> model_get_model()
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
.
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 '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)
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 '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)
model |
(a model object, e.g. |
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()
lm(hp ~ mpg + factor(cyl), mtcars) |> model_get_model_frame() |> head()
lm(hp ~ mpg + factor(cyl), mtcars) |> model_get_model_frame() |> head()
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()
.
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 'cch' model_get_terms(model, ...)
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 'cch' model_get_terms(model, ...)
model |
(a model object, e.g. |
... |
Additional arguments passed to |
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.
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()
lm(hp ~ mpg + factor(cyl), mtcars) |> model_get_model_matrix() |> head()
lm(hp ~ mpg + factor(cyl), mtcars) |> model_get_model_matrix() |> head()
For binomial and multinomial logistic models, will also return the number of events.
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)
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)
model |
(a model object, e.g. |
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).
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()
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)
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)
xlevels
Get the number of levels for each factor used in xlevels
model_get_nlevels(model) ## Default S3 method: model_get_nlevels(model)
model_get_nlevels(model) ## Default S3 method: model_get_nlevels(model)
model |
(a model object, e.g. |
a tibble with two columns: "variable"
and "var_nlevels"
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()
lm(hp ~ mpg + factor(cyl), mtcars) |> model_get_nlevels()
lm(hp ~ mpg + factor(cyl), mtcars) |> model_get_nlevels()
This function does not cover lavaan
models (NULL
is returned).
model_get_offset(model) ## Default S3 method: model_get_offset(model)
model_get_offset(model) ## Default S3 method: model_get_offset(model)
model |
(a model object, e.g. |
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()
mod <- glm( response ~ trt + offset(log(ttdeath)), gtsummary::trial, family = poisson ) mod |> model_get_offset()
mod <- glm( response ~ trt + offset(log(ttdeath)), gtsummary::trial, family = poisson ) mod |> model_get_offset()
It is computed with emmeans::emmeans()
.
model_get_pairwise_contrasts( model, variables, pairwise_reverse = TRUE, contrasts_adjust = NULL, conf.level = 0.95, emmeans_args = list() )
model_get_pairwise_contrasts( model, variables, pairwise_reverse = TRUE, contrasts_adjust = NULL, conf.level = 0.95, emmeans_args = list() )
model |
(a model object, e.g. |
variables |
( |
pairwise_reverse |
( |
contrasts_adjust |
optional adjustment method when computing contrasts,
see |
conf.level |
( |
emmeans_args |
( |
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. This support is still experimental.
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()
if (.assert_package("emmeans", boolean = TRUE)) { mod <- lm(Sepal.Length ~ Species, data = iris) mod |> model_get_pairwise_contrasts(variables = "Species") mod |> model_get_pairwise_contrasts( variables = "Species", contrasts_adjust = "none" ) }
if (.assert_package("emmeans", boolean = TRUE)) { mod <- lm(Sepal.Length ~ Species, data = iris) mod |> model_get_pairwise_contrasts(variables = "Species") mod |> model_get_pairwise_contrasts( variables = "Species", contrasts_adjust = "none" ) }
This function does not cover lavaan
models (NULL
is returned).
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)
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)
model |
(a model object, e.g. |
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()
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()
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
model_get_response_variable(model) ## Default S3 method: model_get_response_variable(model)
model_get_response_variable(model) ## Default S3 method: model_get_response_variable(model)
model |
(a model object, e.g. |
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()
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()
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()
Return the result of stats::terms()
applied to the model
or NULL
if it is not possible to get terms from model
.
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)
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)
model |
(a model object, e.g. |
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()
.
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()
lm(hp ~ mpg + factor(cyl), mtcars) |> model_get_terms()
lm(hp ~ mpg + factor(cyl), mtcars) |> model_get_terms()
This function does not cover lavaan
models (NULL
is returned).
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)
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)
model |
(a model object, e.g. |
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.
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()
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()
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
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)
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)
model |
(a model object, e.g. |
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()
lm(hp ~ mpg + factor(cyl), mtcars) |> model_get_xlevels()
lm(hp ~ mpg + factor(cyl), mtcars) |> model_get_xlevels()
It will also identify interaction terms and intercept(s).
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)
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)
model |
(a model object, e.g. |
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
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()
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() iris |> lm( Sepal.Length ~ poly(Sepal.Width, 2) + Species, data = _, contrasts = list(Species = contr.sum) ) |> model_identify_variables()
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() iris |> lm( Sepal.Length ~ poly(Sepal.Width, 2) + Species, data = _, contrasts = list(Species = contr.sum) ) |> model_identify_variables()
List contrasts used by a model
model_list_contrasts(model) ## Default S3 method: model_list_contrasts(model)
model_list_contrasts(model) ## Default S3 method: model_list_contrasts(model)
model |
(a model object, e.g. |
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.
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
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()
glm( am ~ mpg + factor(cyl), data = mtcars, family = binomial, contrasts = list(`factor(cyl)` = contr.sum) ) |> model_list_contrasts()
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
model_list_higher_order_variables(model) ## Default S3 method: model_list_higher_order_variables(model)
model_list_higher_order_variables(model) ## Default S3 method: model_list_higher_order_variables(model)
model |
(a model object, e.g. |
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()
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()
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()
Only for categorical variables with treatment,
SAS, sum or successive differences contrasts (cf. MASS::contr.sdif()
), and
categorical variables with no contrast.
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") )
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") )
model |
(a model object, e.g. |
label_pattern |
( |
variable_labels |
( |
sdif_term_level |
( |
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 with model_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 in label_pattern
.
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()
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))}" )
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))}" )
Including variables used only in an interaction.
model_list_variables( model, labels = NULL, only_variable = FALSE, add_var_type = FALSE ) ## Default S3 method: model_list_variables( model, labels = NULL, only_variable = FALSE, add_var_type = FALSE ) ## S3 method for class 'lavaan' model_list_variables( model, labels = NULL, only_variable = FALSE, add_var_type = FALSE ) ## S3 method for class 'logitr' model_list_variables( model, labels = NULL, only_variable = FALSE, add_var_type = FALSE )
model_list_variables( model, labels = NULL, only_variable = FALSE, add_var_type = FALSE ) ## Default S3 method: model_list_variables( model, labels = NULL, only_variable = FALSE, add_var_type = FALSE ) ## S3 method for class 'lavaan' model_list_variables( model, labels = NULL, only_variable = FALSE, add_var_type = FALSE ) ## S3 method for class 'logitr' model_list_variables( model, labels = NULL, only_variable = FALSE, add_var_type = FALSE )
model |
(a model object, e.g. |
labels |
( |
only_variable |
( |
add_var_type |
( |
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, otherwise variable
)
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
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()
if (.assert_package("gtsummary", boolean = TRUE)) { 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() iris |> lm( Sepal.Length ~ poly(Sepal.Width, 2) + Species, data = _, 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() }
if (.assert_package("gtsummary", boolean = TRUE)) { 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() iris |> lm( Sepal.Length ~ poly(Sepal.Width, 2) + Species, data = _, 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() }
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}
.
scope_tidy(x, data = NULL)
scope_tidy(x, data = NULL)
x |
( |
data |
( |
A data frame.
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()
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()
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()
and all_ran_vals()
for random-effect parameters and
values from a mixed model
(see vignette("broom_mixed_intro", package = "broom.mixed")
)
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") )
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") )
continuous2 |
( |
dichotomous |
( |
contrasts_type |
( |
A character vector of column names selected.
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")) )
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
seq_range(x, length.out = 25)
seq_range(x, length.out = 25)
x |
( |
length.out |
( |
seq_range(x, length.out)
is a shortcut for
seq(min(x, na.rm = TRUE), max(x, na.rm = TRUE), length.out = length.out)
a numeric vector
seq_range(iris$Petal.Length)
seq_range(iris$Petal.Length)
Listing of Supported Models
supported_models
supported_models
A data frame with one row per supported model
Model
Notes
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 |
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() |
Experimental support. Use tidy_multgee() as tidy_fun . |
multgee::ordLORgee() |
Experimental support. 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() |
|
tidycmprsk::crr() |
|
VGAM::vglm() |
Limited support. It is recommended to use tidy_parameters() as tidy_fun . |
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
).
tidy_add_coefficients_type( x, exponentiate = attr(x, "exponentiate"), model = tidy_get_model(x) )
tidy_add_coefficients_type( x, exponentiate = attr(x, "exponentiate"), model = tidy_get_model(x) )
x |
( |
exponentiate |
( |
model |
(a model object, e.g. |
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_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
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")
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 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".
tidy_add_contrasts(x, model = tidy_get_model(x), quiet = FALSE)
tidy_add_contrasts(x, model = tidy_get_model(x), quiet = FALSE)
x |
( |
model |
(a model object, e.g. |
quiet |
( |
If the variable
column is not yet available in x
,
tidy_identify_variables()
will be automatically applied.
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_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
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()
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()
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.
tidy_add_estimate_to_reference_rows( x, exponentiate = attr(x, "exponentiate"), conf.level = attr(x, "conf.level"), model = tidy_get_model(x), quiet = FALSE )
tidy_add_estimate_to_reference_rows( x, exponentiate = attr(x, "exponentiate"), conf.level = attr(x, "conf.level"), model = tidy_get_model(x), quiet = FALSE )
x |
( |
exponentiate |
( |
conf.level |
( |
model |
(a model object, e.g. |
quiet |
( |
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.
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_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
if (.assert_package("gtsummary", boolean = TRUE) && .assert_package("emmeans", boolean = TRUE)) { 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() }
if (.assert_package("gtsummary", boolean = TRUE) && .assert_package("emmeans", boolean = TRUE)) { 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() }
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.
tidy_add_header_rows( x, show_single_row = NULL, model = tidy_get_model(x), quiet = FALSE, strict = FALSE )
tidy_add_header_rows( x, show_single_row = NULL, model = tidy_get_model(x), quiet = FALSE, strict = FALSE )
x |
( |
show_single_row |
( |
model |
(a model object, e.g. |
quiet |
( |
strict |
( |
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.
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_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
if (.assert_package("gtsummary", boolean = TRUE)) { 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() }
if (.assert_package("gtsummary", boolean = TRUE)) { 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 number of observations in a new column n_obs
, taking into account any
weights if they have been defined.
tidy_add_n(x, model = tidy_get_model(x))
tidy_add_n(x, model = tidy_get_model(x))
x |
( |
model |
(a model object, e.g. |
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.
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_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
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()
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()
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")
.
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 )
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 )
x |
( |
variables |
include ( |
keep_model_terms |
( |
pairwise_reverse |
( |
contrasts_adjust |
( |
conf.level |
( |
emmeans_args |
( |
model |
(a model object, e.g. |
quiet |
( |
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.
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_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
if (.assert_package("emmeans", boolean = TRUE)) { 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() } }
if (.assert_package("emmeans", boolean = TRUE)) { 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() } }
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.
tidy_add_reference_rows( x, no_reference_row = NULL, model = tidy_get_model(x), quiet = FALSE )
tidy_add_reference_rows( x, no_reference_row = NULL, model = tidy_get_model(x), quiet = FALSE )
x |
( |
no_reference_row |
( |
model |
(a model object, e.g. |
quiet |
( |
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()
.
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_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
if (.assert_package("gtsummary", boolean = TRUE)) { 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() }
if (.assert_package("gtsummary", boolean = TRUE)) { 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() }
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.
tidy_add_term_labels( x, labels = NULL, interaction_sep = " * ", categorical_terms_pattern = "{level}", model = tidy_get_model(x), quiet = FALSE, strict = FALSE )
tidy_add_term_labels( x, labels = NULL, interaction_sep = " * ", categorical_terms_pattern = "{level}", model = tidy_get_model(x), quiet = FALSE, strict = FALSE )
x |
( |
labels |
( |
interaction_sep |
( |
categorical_terms_pattern |
( |
model |
(a model object, e.g. |
quiet |
( |
strict |
( |
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.
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_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
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}" )
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}" )
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.
tidy_add_variable_labels( x, labels = NULL, interaction_sep = " * ", model = tidy_get_model(x) )
tidy_add_variable_labels( x, labels = NULL, interaction_sep = " * ", model = tidy_get_model(x) )
x |
( |
labels |
( |
interaction_sep |
( |
model |
(a model object, e.g. |
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).
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_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
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" ) )
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" ) )
effects::allEffects()
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.
tidy_all_effects(x, conf.int = TRUE, conf.level = 0.95, ...)
tidy_all_effects(x, conf.int = TRUE, conf.level = 0.95, ...)
x |
(a model object, e.g. |
conf.int |
( |
conf.level |
( |
... |
Additional parameters passed to |
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")
.
If the model contains interactions, effects::allEffects()
will return
marginal predictions for the different levels of the interactions.
effects::allEffects()
Other marginal_tieders:
tidy_avg_comparisons()
,
tidy_avg_slopes()
,
tidy_ggpredict()
,
tidy_marginal_contrasts()
,
tidy_marginal_means()
,
tidy_marginal_predictions()
,
tidy_margins()
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)
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)
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()
.
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)
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)
x |
( |
model |
(a model object, e.g. |
.attributes |
( |
tidy_fun |
( |
conf.int |
( |
conf.level |
( |
exponentiate |
( |
model_matrix_attr |
( |
... |
Other arguments passed to |
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.
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_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
mod <- lm(Sepal.Length ~ Sepal.Width + Species, data = iris) tt <- mod |> tidy_and_attach(conf.int = TRUE) tt tidy_get_model(tt)
mod <- lm(Sepal.Length ~ Sepal.Width + Species, data = iris) tt <- mod |> tidy_and_attach(conf.int = TRUE) tt tidy_get_model(tt)
marginaleffects::avg_comparisons()
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.
tidy_avg_comparisons(x, conf.int = TRUE, conf.level = 0.95, ...)
tidy_avg_comparisons(x, conf.int = TRUE, conf.level = 0.95, ...)
x |
(a model object, e.g. |
conf.int |
( |
conf.level |
( |
... |
Additional parameters passed to
|
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")
.
marginaleffects::avg_comparisons()
Other marginal_tieders:
tidy_all_effects()
,
tidy_avg_slopes()
,
tidy_ggpredict()
,
tidy_marginal_contrasts()
,
tidy_marginal_means()
,
tidy_marginal_predictions()
,
tidy_margins()
# 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")
# 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")
marginaleffects::avg_slopes()
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.
tidy_avg_slopes(x, conf.int = TRUE, conf.level = 0.95, ...)
tidy_avg_slopes(x, conf.int = TRUE, conf.level = 0.95, ...)
x |
(a model object, e.g. |
conf.int |
( |
conf.level |
( |
... |
Additional parameters passed to
|
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")
.
marginaleffects::avg_slopes()
Other marginal_tieders:
tidy_all_effects()
,
tidy_avg_comparisons()
,
tidy_ggpredict()
,
tidy_marginal_contrasts()
,
tidy_marginal_means()
,
tidy_marginal_predictions()
,
tidy_margins()
# 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")
# 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")
broom::tidy()
and checks that all arguments are usedTidy with broom::tidy()
and checks that all arguments are used
tidy_broom(x, ...)
tidy_broom(x, ...)
x |
(a model object, e.g. |
... |
Additional parameters passed to |
Other custom_tieders:
tidy_multgee()
,
tidy_parameters()
,
tidy_with_broom_or_parameters()
,
tidy_zeroinfl()
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
.
tidy_disambiguate_terms(x, sep = ".", model = tidy_get_model(x), quiet = FALSE)
tidy_disambiguate_terms(x, sep = ".", model = tidy_get_model(x), quiet = FALSE)
x |
( |
sep |
( |
model |
(a model object, e.g. |
quiet |
( |
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_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
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() }
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() }
ggeffects::ggpredict()
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.
tidy_ggpredict(x, conf.int = TRUE, conf.level = 0.95, ...)
tidy_ggpredict(x, conf.int = TRUE, conf.level = 0.95, ...)
x |
(a model object, e.g. |
conf.int |
( |
conf.level |
( |
... |
Additional parameters passed to |
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")
.
By default, ggeffects::ggpredict()
estimates marginal predictions for each
individual variable, regardless of eventual interactions.
ggeffects::ggpredict()
Other marginal_tieders:
tidy_all_effects()
,
tidy_avg_comparisons()
,
tidy_avg_slopes()
,
tidy_marginal_contrasts()
,
tidy_marginal_means()
,
tidy_marginal_predictions()
,
tidy_margins()
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)
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)
tidy_identify_variables()
will add to the tidy tibble
three additional columns: variable
, var_class
, var_type
and var_nlevels
.
tidy_identify_variables(x, model = tidy_get_model(x), quiet = FALSE)
tidy_identify_variables(x, model = tidy_get_model(x), quiet = FALSE)
x |
( |
model |
(a model object, e.g. |
quiet |
( |
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 where tidy_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.
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_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
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()
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()
marginaleffects::avg_comparisons()
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.
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 )
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 )
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 |
( |
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")
.
marginaleffects::avg_comparisons()
, tidy_avg_comparisons()
Other marginal_tieders:
tidy_all_effects()
,
tidy_avg_comparisons()
,
tidy_avg_slopes()
,
tidy_ggpredict()
,
tidy_marginal_means()
,
tidy_marginal_predictions()
,
tidy_margins()
# 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")
# 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")
marginaleffects::marginal_means()
This function is deprecated. Use instead tidy_marginal_predictions()
with
the option newdata = "marginalmeans"
.
tidy_marginal_means(x, conf.int = TRUE, conf.level = 0.95, ...)
tidy_marginal_means(x, conf.int = TRUE, conf.level = 0.95, ...)
x |
(a model object, e.g. |
conf.int |
( |
conf.level |
( |
... |
Additional parameters passed to
|
Use marginaleffects::marginal_means()
to estimate marginal means and
return a tibble tidied in a way that it could be used by broom.helpers
functions. See marginaleffects::marginal_means()()
for a list of supported
models.
marginaleffects::marginal_means()
estimate marginal means:
adjusted predictions, averaged across a grid of categorical predictors,
holding other numeric predictors at their means. Please refer to the
documentation page of marginaleffects::marginal_means()
. Marginal means
are defined only for categorical variables.
For more information, see vignette("marginal_tidiers", "broom.helpers")
.
marginaleffects::marginal_means()
Other marginal_tieders:
tidy_all_effects()
,
tidy_avg_comparisons()
,
tidy_avg_slopes()
,
tidy_ggpredict()
,
tidy_marginal_contrasts()
,
tidy_marginal_predictions()
,
tidy_margins()
# Average Marginal Means 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_means(mod) tidy_plus_plus(mod, tidy_fun = tidy_marginal_means) mod2 <- lm(Petal.Length ~ poly(Petal.Width, 2) + Species, data = iris) tidy_marginal_means(mod2)
# Average Marginal Means 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_means(mod) tidy_plus_plus(mod, tidy_fun = tidy_marginal_means) mod2 <- lm(Petal.Length ~ poly(Petal.Width, 2) + Species, data = iris) tidy_marginal_means(mod2)
marginaleffects::avg_predictions()
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.
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, ...)
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, ...)
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 |
( |
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")
.
marginaleffects::avg_predictions()
Other marginal_tieders:
tidy_all_effects()
,
tidy_avg_comparisons()
,
tidy_avg_slopes()
,
tidy_ggpredict()
,
tidy_marginal_contrasts()
,
tidy_marginal_means()
,
tidy_margins()
# 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 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() }
margins::margins()
tidy_margins(x, conf.int = TRUE, conf.level = 0.95, ...)
tidy_margins(x, conf.int = TRUE, conf.level = 0.95, ...)
x |
(a model object, e.g. |
conf.int |
( |
conf.level |
( |
... |
Additional parameters passed to |
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")
.
When applying margins::margins()
, custom contrasts are ignored.
Treatment contrasts (stats::contr.treatment()
) are applied to all
categorical variables. Interactions are also ignored.
margins::margins()
Other marginal_tieders:
tidy_all_effects()
,
tidy_avg_comparisons()
,
tidy_avg_slopes()
,
tidy_ggpredict()
,
tidy_marginal_contrasts()
,
tidy_marginal_means()
,
tidy_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_margins(mod) tidy_plus_plus(mod, tidy_fun = tidy_margins)
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)
multgee
model
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.
tidy_multgee(x, conf.int = TRUE, conf.level = 0.95, ...)
tidy_multgee(x, conf.int = TRUE, conf.level = 0.95, ...)
x |
( |
conf.int |
( |
conf.level |
( |
... |
Additional parameters passed to |
To be noted, for multgee::nomLORgee()
, the baseline y
category is the
latest modality of y
.
Other custom_tieders:
tidy_broom()
,
tidy_parameters()
,
tidy_with_broom_or_parameters()
,
tidy_zeroinfl()
if (.assert_package("multgee", boolean = TRUE)) { 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() }
if (.assert_package("multgee", boolean = TRUE)) { 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() }
Use parameters::model_parameters()
to tidy a model and apply
parameters::standardize_names(style = "broom")
to the output
tidy_parameters(x, conf.int = TRUE, conf.level = 0.95, ...)
tidy_parameters(x, conf.int = TRUE, conf.level = 0.95, ...)
x |
(a model object, e.g. |
conf.int |
( |
conf.level |
( |
... |
Additional parameters passed to |
For betareg::betareg()
, the component column in the results is standardized
with broom::tidy()
, using "mean"
and "precision"
values.
Other custom_tieders:
tidy_broom()
,
tidy_multgee()
,
tidy_with_broom_or_parameters()
,
tidy_zeroinfl()
if (.assert_package("parameters", boolean = TRUE)) { lm(Sepal.Length ~ Sepal.Width + Species, data = iris) |> tidy_parameters() }
if (.assert_package("parameters", boolean = TRUE)) { lm(Sepal.Length ~ Sepal.Width + Species, data = iris) |> tidy_parameters() }
This function will apply sequentially:
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, term_labels = NULL, interaction_sep = " * ", categorical_terms_pattern = "{level}", 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(), keep_model = FALSE, tidy_post_fun = NULL, quiet = FALSE, strict = FALSE, ... )
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, term_labels = NULL, interaction_sep = " * ", categorical_terms_pattern = "{level}", 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(), keep_model = FALSE, tidy_post_fun = NULL, quiet = FALSE, strict = FALSE, ... )
model |
(a model object, e.g. |
tidy_fun |
( |
conf.int |
( |
conf.level |
( |
exponentiate |
( |
model_matrix_attr |
( |
variable_labels |
( |
term_labels |
( |
interaction_sep |
( |
categorical_terms_pattern |
( |
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 |
( |
keep_model |
( |
tidy_post_fun |
( |
quiet |
( |
strict |
( |
... |
other arguments passed to |
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.
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_remove_intercept()
,
tidy_select_variables()
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 if (.assert_package("gtsummary", boolean = TRUE)) { 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 }
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 if (.assert_package("gtsummary", boolean = TRUE)) { 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 }
Will remove terms where var_type == "intercept"
.
tidy_remove_intercept(x, model = tidy_get_model(x))
tidy_remove_intercept(x, model = tidy_get_model(x))
x |
( |
model |
(a model object, e.g. |
If the variable
column is not yet available in x
,
tidy_identify_variables()
will be automatically applied.
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_select_variables()
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()
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()
Will remove unselected variables from the results.
To remove the intercept, use tidy_remove_intercept()
.
tidy_select_variables(x, include = everything(), model = tidy_get_model(x))
tidy_select_variables(x, include = everything(), model = tidy_get_model(x))
x |
( |
include |
( |
model |
(a model object, e.g. |
If the variable
column is not yet available in x
,
tidy_identify_variables()
will be automatically applied.
The x
tibble limited to the included variables (and eventually the intercept),
sorted according to the include
parameter.
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()
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()) )
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()) )
Try to tidy a model with broom::tidy()
. If it fails, will try to tidy the
model using parameters::model_parameters()
through tidy_parameters()
.
tidy_with_broom_or_parameters(x, conf.int = TRUE, conf.level = 0.95, ...)
tidy_with_broom_or_parameters(x, conf.int = TRUE, conf.level = 0.95, ...)
x |
(a model object, e.g. |
conf.int |
( |
conf.level |
( |
... |
Additional parameters passed to |
Other custom_tieders:
tidy_broom()
,
tidy_multgee()
,
tidy_parameters()
,
tidy_zeroinfl()
zeroinfl
or a hurdle
model
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.
tidy_zeroinfl(x, conf.int = TRUE, conf.level = 0.95, component = NULL, ...)
tidy_zeroinfl(x, conf.int = TRUE, conf.level = 0.95, component = NULL, ...)
x |
( |
conf.int |
( |
conf.level |
( |
component |
( |
... |
Additional parameters passed to |
Other custom_tieders:
tidy_broom()
,
tidy_multgee()
,
tidy_parameters()
,
tidy_with_broom_or_parameters()
if (.assert_package("pscl", boolean = TRUE)) { library(pscl) mod <- zeroinfl( art ~ fem + mar + phd, data = pscl::bioChemists ) mod |> tidy_zeroinfl(exponentiate = TRUE) }
if (.assert_package("pscl", boolean = TRUE)) { library(pscl) mod <- zeroinfl( art ~ fem + mar + phd, data = pscl::bioChemists ) mod |> tidy_zeroinfl(exponentiate = TRUE) }