mrInteractions: Calculate and visualize feature interactions

View source: R/mrInteractions.R

mrInteractionsR Documentation

Calculate and visualize feature interactions

Description

A wrapper around hstats::hstats(). Calculates and visualizes H-statistics for interactions in the model using bootstrapping. See help("hstats") for details on H-statistics.

Usage

mrInteractions(mrIMLobj, num_bootstrap = 1, feature = NULL, top_int = 10)

Arguments

mrIMLobj

A list object output by mrIMLpredicts().

num_bootstrap

The number of bootstrap samples to generate (default: 1).

feature

The response model for which detailed interaction plots should be generated.

top_int

The number of top interactions to display (default: 10).

Value

A list containing:

  • ⁠$p_h2⁠: An ordered bar plot of the variability in each response model that is unexplained by the main effects.

  • ⁠$p_h2_overall⁠: An ordered bar plot of the percentage of prediction variability that can be attributed to interactions with each predictor for the model specified by feature.

  • ⁠$p_h2_pairwise⁠: An ordered bar plot of the strength of the two-way interactions in the model specified by feature. The strength of an interaction is taken to be the un-normalized square root of the H2-pairwise statistic (which is on the prediction scale).

  • ⁠$h2_df⁠: A data frame of the H2 statistics for each response model, along with bootstraps if applicable.

  • ⁠$h2_overall_df⁠: A data frame of the H2-overall statistics for the variable in each response model, along with bootstraps if applicable.

  • ⁠$h2_pairwise_df⁠: A data frame of the H2-pairwise statistics for the variable in each response model, along with bootstraps if applicable.

Examples

library(tidymodels)

data <- MRFcov::Bird.parasites
Y <- data %>%
  select(-scale.prop.zos) %>%
  select(order(everything()))
X <- data %>%
  select(scale.prop.zos)

# Specify a random forest tidy model
model_rf <- rand_forest(
  trees = 50, # 50 trees are set for brevity. Aim to start with 1000
  mode = "classification",
  mtry = tune(),
  min_n = tune()
) %>%
  set_engine("randomForest")

mrIML_rf <- mrIMLpredicts(
  X = X,
  Y = Y,
  X1 = Y,
  Model = model_rf,
  prop = 0.7,
  k = 2,
  racing = FALSE
)

mrIML_interactions_rf <- mrInteractions(
  mrIML_rf,
  num_bootstrap = 5,
  feature = "Plas"
)

mrIML_interactions_rf[[1]]
mrIML_interactions_rf[[2]]
mrIML_interactions_rf[[3]]


nfj1380/mrIML documentation built on June 2, 2025, 1:03 a.m.