Skip to contents

[Experimental]
Helpers to generate formatted tables of a MAIHDA analysis as proposed by Evans et al. (SSM - Population Health 2024, doi:10.1016/j.ssmph.2024.101664 ). It relies on the MAIHDA package. This package being under active development, the proposed functions here are experimental.

Usage

tbl_maihda(
  x,
  ...,
  global_p = FALSE,
  twomodels_labels = c("Null model", "Adjusted model"),
  statistics_header = "Summary statistics",
  statistics_labels = list(bsv = "Between-stratum variance", bssd =
    "Between-stratum standard deviation", vpc = "Variance Partition Coefficient (VPC)",
    pcv = "Proportional Change in Variance (PCV)", auc =
    "Area Under Receiver Operating Characteristic Curve (AUC)", mor =
    "Median Odds Ratio (MOR)", csvpc = "Context share (VPC)"),
  statistics_include = -dplyr::any_of("bssd"),
  notes = TRUE,
  notes_labels = list(n_strata = "Strata:", nobs = "Observations:", engine = "Engine:",
    family = "Family:", context = "Variable(s) in context:")
)

tbl_partially_adjusted_maihda(
  x,
  ...,
  global_p = FALSE,
  twomodels_labels = c("Null model", "Fully adjusted model"),
  statistics_header = "Summary statistics",
  statistics_labels = list(bsv = "Between-stratum variance", bssd =
    "Between-stratum standard deviation", vpc = "Variance Partition Coefficient (VPC)",
    pcv = "Proportional Change in Variance (PCV)", auc =
    "Area Under Receiver Operating Characteristic Curve (AUC)", mor =
    "Median Odds Ratio (MOR)", csvpc = "Context share (VPC)"),
  statistics_include = -dplyr::any_of("bssd"),
  notes = TRUE,
  notes_labels = list(n_strata = "Strata:", nobs = "Observations:", engine = "Engine:",
    family = "Family:", context = "Variable(s) in context:"),
  return_data = FALSE
)

tbl_strata_info(
  x,
  breaks = c(10, 20, 30, 50, 100),
  column_labels = list(size = "Sample size per stratum", n = "Number of strata", prop =
    "Proportion of strata"),
  total_label = "Total number of strata:"
)

tbl_strata_predictions(
  x,
  n_strata = 5L,
  scale = c("response", "link"),
  which = c("null", "adjusted"),
  column_labels = list(rank = "Rank", n = "n", predicted = "Predicted", ci = "95% CI"),
  group_labels = list("highest", "lowest"),
  digits = 1L,
  return_data = FALSE
)

plot_maihda_predictions_by(
  x,
  by,
  scale = c("response", "link"),
  which = c("null", "adjusted"),
  sort = TRUE
)

glance_maihda_model(x)

Arguments

x

a MAIHDA object (maihda_analysis or maihda_model); for tbl_maihda() it could also be a list of maihda_model objects; for tbl_partially_adjusted_maihda(), only a maihda_analysis computed with MAIHDA::maihda(decomposition = "two-model") is allowed; for tbl_strata_info(), the result of MAIHDA::make_strata() is also accepted

...

additional parameters passed to gtsummary::tbl_regression()

global_p

display global p-value instead of terms p-value (see gtsummary::add_global_p()), not available if engine = "wemix".

twomodels_labels

for a two-model MAIHDA analysis, labels for the two models

statistics_header

string header of the summary statistics

statistics_labels

name list of labels for the summary statistics

statistics_include

<tidy-select>
names of summary statistics to be included: must be column names of the tibble returned by glance_maihda_model()

notes

display some notes (number of strata, of observations, engine, model family) about the analysis?

notes_labels

name list of labels for the notes

return_data

return a data frame instead of a table

breaks

breaks for sample size per stratum

column_labels

named list of column labels

total_label

string of the total label in the notes

n_strata

number of strata to show at each end (top and bottom), use Inf or NULL to show all strata

scale

Scale for the predicted stratum values: "response" (default) or "link". For a cumulative (ordinal) model the response scale is the expected category score.

which

For a two-model analysis, which model's predictions to rank the strata by: "null" (default) or "adjusted". Ignored for a crossed-dimensions analysis or a single model.

group_labels

labels for group names

digits

number of decimals for predictions

by

<tidy-select>
list of variables to compare by

sort

should the plot be sorted?

Details

tbl_maihda() is intended to replicate Table 3 of Evans et al. 2024, with fixed effects, between-stratum variance and model summary statistics including VPC (variance partition coefficient) and PCV (proportional change in variance). It accepts a maihda_analysis object created with MAIHDA::maihda(), a single maihda_model created with MAIHDA::fit_maihda() or a list of several maihfda_model objects. For this last case, PCV should be manually added to the models to be displayed (see examples).

tbl_partially_adjusted_maihda() is an helper allowing to compute and display all partially adjusted models (see examples).

tbl_strata_info() is intended to replicate Table 2, showing the number of strata having a certain sample size.

tbl_strata_predictions() is intended to replicate Table, showing the strata with the highest and the lowest predicted value. If a maihda_analysis object is passed to tbl_strata_predictions(), the null model is taken into account by default for computing the predicted values, following the behavior of MAIHDA::maihda_table(). It should be noted that in Evans et al. 2024, the authors used the adjusted model, which could be done with the argument which = "adjusted".

plot_maihda_predictions_by() allows to visually compare predicted values by strata according to one or several specific variable defining the strata.

To be noted, themes from the gtsummary package are taken into account for formatting the different values.

Examples

# \donttest{
theme_gtsummary_bold_labels()

# gaussian model

data("maihda_health_data", package = "MAIHDA")
a <- MAIHDA::maihda(
  BMI ~ Age + Gender + Race + (1 | Gender:Race),
  data = maihda_health_data
)

a |> tbl_strata_info(breaks = c(50, 100, 150))
Sample size per stratum Number of strata Proportion of strata
≥ 150 4 40.0%
≥ 100 7 70.0%
≥ 50 10 100.0%
< 50 0 0.0%
Total number of strata: 10
a |> tbl_maihda()
Characteristic
Null model
Adjusted model
Beta 95% CI Beta 95% CI
(Intercept) 28 27, 29 30 28, 32
Age 0.02 0.00, 0.03 0.02 0.00, 0.03
Gender



    female

    male

-0.45 -2.1, 1.2
Race



    Black

    Hispanic

-1.8 -4.4, 0.81
    Mexican

-1.1 -3.6, 1.5
    White

-1.9 -4.4, 0.48
    Other

-4.4 -6.9, -1.8
Summary statistics



Between-stratum variance 2.74
1.38
Variance Partition Coefficient (VPC) 5.85%
3.03%
Proportional Change in Variance (PCV)

82.1%
Abbreviation: CI = Confidence Interval
Strata: 10, Observations: 3000, Engine: lme4, Family: gaussian
a |> tbl_strata_predictions()
Rank Gender Race n Predicted 95% CI
1 female Black 182 32.1 31.2, 33.0
2 female Mexican 99 29.6 28.3, 30.8
3 male Mexican 143 29.3 28.3, 30.3
4 male White 990 29.2 28.8, 29.6
5 male Hispanic 75 29.0 27.6, 30.3
6 male Black 154 28.9 27.9, 29.9
7 female Hispanic 91 28.6 27.4, 29.9
8 female White 1044 28.4 28.0, 28.8
9 male Other 111 26.8 25.6, 27.9
10 female Other 111 26.4 25.2, 27.5
a |> plot_maihda_predictions_by(Race) # a binomial example m <- MAIHDA::maihda( Survived ~ Age + Sex + Class + (1 | Age:Sex:Class), data = titanic, family = binomial ) #> Binary outcome 'Survived' recoded to 0/1: 'No' = 0 (reference), 'Yes' = 1 (modeled event). Set the factor levels (or supply a 0/1 outcome) to control which level is the event. m |> tbl_strata_info()
Sample size per stratum Number of strata Proportion of strata
≥ 100 6 42.9%
≥ 50 7 50.0%
≥ 30 9 64.3%
≥ 20 10 71.4%
≥ 10 12 85.7%
< 10 2 14.3%
Total number of strata: 14
m |> tbl_maihda(exponentiate = TRUE)
Characteristic
Null model
Adjusted model
OR 95% CI p-value OR 95% CI p-value
(Intercept) 2.44 0.68, 8.69 0.2 17.5 3.68, 83.7 <0.001
Adult or Child





    Adult



    Child


8.71 1.74, 43.6 0.008
Sex of passenger





    Female



    Male


0.08 0.02, 0.27 <0.001
Passenger's class





    1st



    2nd


0.32 0.05, 2.02 0.2
    3rd


0.03 0.01, 0.22 <0.001
    Crew


0.28 0.04, 2.15 0.2
Summary statistics





Between-stratum variance 4.89

0.988

Variance Partition Coefficient (VPC) 59.8%

23.1%

Proportional Change in Variance (PCV)


79.8%

Area Under Receiver Operating Characteristic Curve (AUC) 0.768

0.768

Median Odds Ratio (MOR) 8.24

2.58

Abbreviations: CI = Confidence Interval, OR = Odds Ratio
Strata: 14, Observations: 2201, Engine: lme4, Family: binomial
m |> tbl_strata_predictions(n_strata = NULL)
Rank Adult or Child Sex of passenger Passenger's class n Predicted 95% CI
1 Adult Female 1st 144 96.9% 92.5%, 98.7%
2 Child Female 2nd 13 96.3% 70.3%, 99.6%
3 Child Male 2nd 11 95.8% 66.7%, 99.6%
4 Child Male 1st 5 93.0% 47.4%, 99.5%
5 Adult Female Crew 23 86.1% 66.5%, 95.1%
6 Adult Female 2nd 93 85.8% 77.3%, 91.5%
7 Child Female 1st 1 84.1% 15.4%, 99.4%
8 Adult Female 3rd 165 46.2% 38.7%, 53.8%
9 Child Female 3rd 31 45.9% 29.7%, 63.0%
10 Adult Male 1st 175 32.8% 26.2%, 40.0%
11 Child Male 3rd 48 27.9% 17.1%, 41.9%
12 Adult Male Crew 862 22.3% 19.7%, 25.2%
13 Adult Male 3rd 462 16.3% 13.3%, 20.0%
14 Adult Male 2nd 168 8.73% 5.32%, 14.0%
m |> tbl_strata_predictions(which = "adjusted", n_strata = 3)
Rank Adult or Child Sex of passenger Passenger's class n Predicted 95% CI
3 highest
1 Child Female 1st 1 99.4% 95.7%, 99.9%
2 Child Female 2nd 13 98.4% 91.1%, 99.7%
3 Adult Female 1st 144 96.8% 92.9%, 98.6%
3 lowest
12 Adult Male Crew 862 22.3% 19.7%, 25.2%
13 Adult Male 3rd 462 15.9% 12.9%, 19.5%
14 Adult Male 2nd 168 9.20% 5.76%, 14.4%
m |> plot_maihda_predictions_by(Sex) m |> plot_maihda_predictions_by(c(Sex, Age)) # Partially adjusted models m0 <- MAIHDA::fit_maihda( Survived ~ 1 + (1 | Age:Sex:Class), data = titanic, family = binomial ) #> Binary outcome 'Survived' recoded to 0/1: 'No' = 0 (reference), 'Yes' = 1 (modeled event). Set the factor levels (or supply a 0/1 outcome) to control which level is the event. m1 <- MAIHDA::fit_maihda( Survived ~ Age + (1 | Age:Sex:Class), data = titanic, family = binomial ) #> Binary outcome 'Survived' recoded to 0/1: 'No' = 0 (reference), 'Yes' = 1 (modeled event). Set the factor levels (or supply a 0/1 outcome) to control which level is the event. m2 <- MAIHDA::fit_maihda( Survived ~ Sex + (1 | Age:Sex:Class), data = titanic, family = binomial ) #> Binary outcome 'Survived' recoded to 0/1: 'No' = 0 (reference), 'Yes' = 1 (modeled event). Set the factor levels (or supply a 0/1 outcome) to control which level is the event. m3 <- MAIHDA::fit_maihda( Survived ~ Class + (1 | Age:Sex:Class), data = titanic, family = binomial ) #> Binary outcome 'Survived' recoded to 0/1: 'No' = 0 (reference), 'Yes' = 1 (modeled event). Set the factor levels (or supply a 0/1 outcome) to control which level is the event. # manually adding PCV m1$pcv <- MAIHDA::calculate_pcv(m0, m1) m2$pcv <- MAIHDA::calculate_pcv(m0, m2) m3$pcv <- MAIHDA::calculate_pcv(m0, m3) list(Null = m0, Age = m1, Sex = m2, Class = m3) |> tbl_maihda(exponentiate = TRUE, global_p = TRUE)
Characteristic
Null
Age
Sex
Class
OR 95% CI p-value OR 95% CI p-value OR 95% CI p-value OR 95% CI p-value
(Intercept) 2.44 0.68, 8.69 0.2 1.14 0.25, 5.26 0.9 6.52 1.52, 28.1 0.012 9.73 0.97, 97.4 0.053
Adult or Child




0.12





    Adult









    Child


9.71 0.56, 170






Sex of passenger







0.030


    Female









    Male





0.11 0.02, 0.80



Passenger's class










0.2
    1st









    2nd








0.57 0.03, 12.1
    3rd








0.05 0.00, 0.94
    Crew








0.14 0.00, 4.83
Summary statistics











Between-stratum variance 4.89

4.78

3.02

3.64

Variance Partition Coefficient (VPC) 59.8%

59.2%

47.9%

52.6%

Proportional Change in Variance (PCV)


2.11%

38.1%

25.4%

Area Under Receiver Operating Characteristic Curve (AUC) 0.768

0.768

0.768

0.768

Median Odds Ratio (MOR) 8.24

8.05

5.25

6.18

Abbreviations: CI = Confidence Interval, OR = Odds Ratio
Strata: 14, Observations: 2201, Engine: lme4, Family: binomial
# in one call m |> tbl_partially_adjusted_maihda(exponentiate = TRUE)
Characteristic
Null model
Adult or Child
Sex of passenger
Passenger’s class
Fully adjusted model
OR 95% CI p-value OR 95% CI p-value OR 95% CI p-value OR 95% CI p-value OR 95% CI p-value
(Intercept) 2.44 0.68, 8.69 0.2 1.14 0.25, 5.26 0.9 6.52 1.52, 28.1 0.012 9.73 0.97, 97.4 0.053 17.5 3.68, 83.7 <0.001
Adult or Child














    Adult










    Child


9.71 0.56, 170 0.12





8.71 1.74, 43.6 0.008
Sex of passenger














    Female










    Male





0.11 0.02, 0.80 0.030


0.08 0.02, 0.27 <0.001
Passenger's class














    1st










    2nd








0.57 0.03, 12.1 0.7 0.32 0.05, 2.02 0.2
    3rd








0.05 0.00, 0.94 0.046 0.03 0.01, 0.22 <0.001
    Crew








0.14 0.00, 4.83 0.3 0.28 0.04, 2.15 0.2
Summary statistics














Between-stratum variance 4.89

4.78

3.02

3.64

0.988

Variance Partition Coefficient (VPC) 59.8%

59.2%

47.9%

52.6%

23.1%

Proportional Change in Variance (PCV)


2.11%

38.1%

25.4%

79.8%

Area Under Receiver Operating Characteristic Curve (AUC) 0.768

0.768

0.768

0.768

0.768

Median Odds Ratio (MOR) 8.24

8.05

5.25

6.18

2.58

Abbreviations: CI = Confidence Interval, OR = Odds Ratio
Strata: 14, Observations: 2201, Engine: lme4, Family: binomial
# sample-weighted data if (rlang::is_installed("WeMix")) { d <- titanic d$weight <- 1 wm <- MAIHDA::maihda( Survived ~ Age + Sex + Class + (1 | Age:Sex:Class), data = d, family = binomial, sampling_weights = "weight", engine = "wemix" ) wm |> tbl_maihda(exponentiate = TRUE) } #> Binary outcome 'Survived' recoded to 0/1: 'No' = 0 (reference), 'Yes' = 1 (modeled event). Set the factor levels (or supply a 0/1 outcome) to control which level is the event.
Characteristic
Null model
Adjusted model
OR 95% CI OR 95% CI
(Intercept) 2.41
17.6
Adult or Child



    Adult

    Child

8.70
Sex of passenger



    Female

    Male

0.08
Passenger's class



    1st

    2nd

0.32
    3rd

0.03
    Crew

0.28
Summary statistics



Between-stratum variance 4.98
0.990
Variance Partition Coefficient (VPC) 60.2%
23.1%
Proportional Change in Variance (PCV)

80.1%
Area Under Receiver Operating Characteristic Curve (AUC) 0.768
0.768
Median Odds Ratio (MOR) 8.41
2.58
Abbreviations: CI = Confidence Interval, OR = Odds Ratio
Strata: 14, Observations: 2201, Engine: wemix, Family: binomial
# }