Add an estimate value to references rows for categorical variables
Source:R/tidy_add_estimate_to_reference_rows.R
tidy_add_estimate_to_reference_rows.Rd
For categorical variables with a treatment contrast
(stats::contr.treatment()
) or a SAS contrast (stats::contr.SAS()
),
will add an estimate equal to 0
(or 1
if exponentiate = TRUE
)
to the reference row.
Usage
tidy_add_estimate_to_reference_rows(
x,
exponentiate = attr(x, "exponentiate"),
conf.level = attr(x, "conf.level"),
model = tidy_get_model(x),
quiet = FALSE
)
Arguments
- x
(
data.frame
)
A tidy tibble as produced bytidy_*()
functions.- exponentiate
(
logical
)
Whether or not to exponentiate the coefficient estimates. It should be consistent with the original call tobroom::tidy()
- conf.level
(
numeric
)
Confidence level, by default use the value indicated previously intidy_and_attach()
, used only for sum contrasts.- model
(a model object, e.g.
glm
)
The corresponding model, if not attached tox
.- quiet
(
logical
)
Whetherbroom.helpers
should not return a message when requested output cannot be generated. Default isFALSE
.
Details
For categorical variables with a sum contrast (stats::contr.sum()
),
the estimate value of the reference row will be equal to the sum of
all other coefficients multiplied by -1
(eventually exponentiated if
exponentiate = TRUE
), and obtained with emmeans::emmeans()
.
The emmeans
package should therefore be installed.
For sum contrasts, the model coefficient corresponds
to the difference of each level with the grand mean.
For sum contrasts, confidence intervals and p-values will also
be computed and added to the reference rows.
For other variables, no change will be made.
If the reference_row
column is not yet available in x
,
tidy_add_reference_rows()
will be automatically applied.
See also
Other tidy_helpers:
tidy_add_coefficients_type()
,
tidy_add_contrasts()
,
tidy_add_header_rows()
,
tidy_add_n()
,
tidy_add_pairwise_contrasts()
,
tidy_add_reference_rows()
,
tidy_add_term_labels()
,
tidy_add_variable_labels()
,
tidy_attach_model()
,
tidy_disambiguate_terms()
,
tidy_group_by()
,
tidy_identify_variables()
,
tidy_plus_plus()
,
tidy_remove_intercept()
,
tidy_select_variables()
Examples
# \donttest{
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()
#> # A tibble: 9 × 14
#> term variable var_class var_type var_nlevels contrasts contrasts_type
#> <chr> <chr> <chr> <chr> <int> <chr> <chr>
#> 1 (Intercept) (Intercep… NA interce… NA NA NA
#> 2 Class1st Class factor categor… 4 contr.SAS treatment
#> 3 Class2nd Class factor categor… 4 contr.SAS treatment
#> 4 Class3rd Class factor categor… 4 contr.SAS treatment
#> 5 ClassCrew Class factor categor… 4 contr.SAS treatment
#> 6 Age1 Age factor dichoto… 2 contr.sum sum
#> 7 Age2 Age factor dichoto… 2 contr.sum sum
#> 8 SexFemale Sex factor dichoto… 2 contr.tr… treatment
#> 9 SexMale Sex factor dichoto… 2 contr.tr… treatment
#> # ℹ 7 more variables: reference_row <lgl>, estimate <dbl>, std.error <dbl>,
#> # statistic <dbl>, p.value <dbl>, conf.low <dbl>, conf.high <dbl>
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()
#> # A tibble: 12 × 14
#> term variable var_class var_type var_nlevels contrasts contrasts_type
#> <chr> <chr> <chr> <chr> <int> <chr> <chr>
#> 1 (Intercept) (Interce… NA interce… NA NA NA
#> 2 stage1 stage factor categor… 4 contr.tr… treatment
#> 3 stage2 stage factor categor… 4 contr.tr… treatment
#> 4 stage3 stage factor categor… 4 contr.tr… treatment
#> 5 stage4 stage factor categor… 4 contr.tr… treatment
#> 6 grade1 grade factor categor… 3 contr.tr… treatment
#> 7 grade2 grade factor categor… 3 contr.tr… treatment
#> 8 grade3 grade factor categor… 3 contr.tr… treatment
#> 9 trt1 trt character dichoto… 2 contr.SAS treatment
#> 10 trt2 trt character dichoto… 2 contr.SAS treatment
#> 11 grade1:trt1 grade:trt NA interac… NA NA NA
#> 12 grade3:trt1 grade:trt NA interac… NA NA NA
#> # ℹ 7 more variables: reference_row <lgl>, estimate <dbl>, std.error <dbl>,
#> # statistic <dbl>, p.value <dbl>, conf.low <dbl>, conf.high <dbl>
# }