Skip to contents

Automatically identifies interaction terms in a fitted model involving two categorical factors (e.g., `~ A * B`) and calculates measures of additive interaction (RERI, AP, S) for each combination of non-reference levels. This function requires the `addint()` function to be available.

Usage

addintlist(
  model,
  factor1_name,
  factor2_name,
  measures = "all",
  conf.level = 0.95,
  digits = 3
)

Arguments

model

A fitted model object (e.g., `svycoxph`, `svyglm`) of type `"interaction"` (containing a `factor1 * factor2` term).

factor1_name

Character string: The name of the first factor variable in the interaction (e.g., `"Race1"`).

factor2_name

Character string: The name of the second factor variable (e.g., `"ObeseStatus"`).

measures

A character vector specifying which measures to calculate via `addint()`. Options: `"RERI"`, `"AP"`, `"S"`, or `"all"`. Default is `"all"`.

conf.level

Confidence level for the interval (default 0.95).

digits

Integer. Number of decimal places for rounding estimates and CIs in the final table. Default is 3. (Set to NULL for no rounding).

Value

A `tibble` (data frame) summarizing the additive interaction results. Columns include:

  • `Factor1`: Name of the first factor.

  • `Level1`: Non-reference level of the first factor for the comparison.

  • `Factor2`: Name of the second factor.

  • `Level2`: Non-reference level of the second factor for the comparison.

  • `Measure`: The additive interaction measure calculated ("RERI", "AP", or "S").

  • `Estimate`: Point estimate of the measure.

  • `SE`: Standard Error (Note: For S, this is SE of log(S)).

  • `CI_low`: Lower confidence interval bound.

  • `CI_upp`: Upper confidence interval bound.

Returns an empty tibble if errors occur.

Details

This function serves as a wrapper around `addint()`. It determines the levels of the two specified factors from the model object, constructs the expected coefficient names for main effects and interaction terms (checking both `A:B` and `B:A` formats), and then calls `addint()` with `type = "interaction"` for each valid combination found in the model. The results are compiled into a single summary table.

See also

Examples

# \donttest{
# --- Load required libraries for the example ---
# Ensure the 'addint' function is defined or loaded from the package
# source("R/addint.R") # If running interactively before package build

if (requireNamespace("survey", quietly = TRUE) &&
    requireNamespace("NHANES", quietly = TRUE) &&
    requireNamespace("dplyr", quietly = TRUE) &&
    requireNamespace("tidyr", quietly = TRUE) &&
    requireNamespace("msm", quietly = TRUE)) {

  library(survey)
  library(NHANES)
  library(dplyr)
  library(tidyr)
  library(msm)

  # --- 1. Data Preparation (NHANES Example) ---
  data(NHANESraw)

  vars_needed <- c("Age", "Race1", "BPSysAve", "BMI", "ObeseStatus", "Hypertension_130",
                   "SDMVPSU", "SDMVSTRA", "WTMEC2YR")

  nhanes_adults_processed <- NHANESraw %>%
    filter(Age >= 20) %>%
    mutate(
      ObeseStatus = factor(ifelse(BMI >= 30, "Obese", "Not Obese"),
                           levels = c("Not Obese", "Obese")),
      Hypertension_130 = factor(ifelse(BPSysAve >= 130, "Yes", "No"),
                                levels = c("No", "Yes")),
      Race1 = relevel(as.factor(Race1), ref = "White")
    ) %>%
    select(all_of(vars_needed)) %>%
    drop_na()

  adult_design_binary <- svydesign(
    id = ~SDMVPSU, strata = ~SDMVSTRA, weights = ~WTMEC2YR,
    nest = TRUE, data = nhanes_adults_processed
  )

  # --- 2. Fit Interaction Term Model ---
  interaction_model_logit <- svyglm(
    Hypertension_130 ~ Race1 * ObeseStatus + Age,
    design = adult_design_binary, family = quasibinomial()
  )

  # --- 3. Calculate all additive interactions ---
  all_interactions_table <- addintlist(
    model = interaction_model_logit,
    factor1_name = "Race1",
    factor2_name = "ObeseStatus",
    measures = "all"
  )

  # Print the results table
  print(all_interactions_table, n=50)

} else {
  print("Required packages (survey, NHANES, dplyr, tidyr, msm) not found.")
}
#> Calculating for: Race1_Black_vs_ObeseStatus_Obese
#> Calculating for: Race1_Hispanic_vs_ObeseStatus_Obese
#> Calculating for: Race1_Mexican_vs_ObeseStatus_Obese
#> Calculating for: Race1_Other_vs_ObeseStatus_Obese
#> # A tibble: 12 × 9
#>    Factor1 Level1   Factor2     Level2 Measure Estimate    SE CI_low CI_upp
#>    <chr>   <chr>    <chr>       <chr>  <chr>      <dbl> <dbl>  <dbl>  <dbl>
#>  1 Race1   Black    ObeseStatus Obese  RERI       0.025 0.312 -0.587  0.637
#>  2 Race1   Black    ObeseStatus Obese  AP         0.01  0.122 -0.23   0.25 
#>  3 Race1   Black    ObeseStatus Obese  S          1.02  0.205  0.68   1.52 
#>  4 Race1   Hispanic ObeseStatus Obese  RERI       0.258 0.194 -0.123  0.639
#>  5 Race1   Hispanic ObeseStatus Obese  AP         0.157 0.105 -0.048  0.362
#>  6 Race1   Hispanic ObeseStatus Obese  S          1.67  0.382  0.788  3.52 
#>  7 Race1   Mexican  ObeseStatus Obese  RERI       0.36  0.253 -0.136  0.857
#>  8 Race1   Mexican  ObeseStatus Obese  AP         0.187 0.116 -0.041  0.415
#>  9 Race1   Mexican  ObeseStatus Obese  S          1.64  0.348  0.829  3.24 
#> 10 Race1   Other    ObeseStatus Obese  RERI      -0.299 0.305 -0.896  0.299
#> 11 Race1   Other    ObeseStatus Obese  AP        -0.259 0.316 -0.878  0.36 
#> 12 Race1   Other    ObeseStatus Obese  S          0.337 1.76   0.011 10.6  
# }