test.interaction: Test for interaction between a continuous predictor and a...

View source: R/Interaction_test.R

test.interactionR Documentation

Test for interaction between a continuous predictor and a grouping variable

Description

Fits a linear model of the form y ~ x * by to evaluate whether the association between a continuous predictor and an outcome differs across groups. Optionally produces a publication-ready visualization of group-specific regression lines.

Usage

test.interaction(
  x,
  y,
  by,
  title = NULL,
  xlab = NULL,
  ylab = NULL,
  plot = TRUE,
  style = c("clean", "CI", "facet"),
  conf.level = 0.95,
  verbose = TRUE,
  help = FALSE
)

## S3 method for class 'test.interaction'
print(x, ...)

Arguments

x

Numeric vector representing the continuous predictor.

y

Numeric vector representing the continuous outcome.

by

Grouping variable defining the interaction. Must be coercible to a factor with at least two levels.

title

Optional title title for the plot.

xlab

Optional x-axis label.

ylab

Optional y-axis label.

plot

Logical. Should a plot be generated?

style

Plot style. One of "clean", "CI" or "facet".

conf.level

Confidence level for the interaction interval (default: 0.95).

verbose

Logical. If TRUE, prints detailed messages. Default: TRUE.

help

Logical. If TRUE, shows a detailed explanation of the function. Default: FALSE.

...

Additional arguments passed to other print methods (currently ignored)

Details

The interaction coefficient (\beta) represents the difference in regression slopes between groups, conditional on the reference level of by. The sign and magnitude of this coefficient depend on the chosen reference group.

Confidence intervals are emphasized as the primary inferential quantity.

Value

An object of class "test.interaction" containing:

  • model: the fitted linear model,

  • interaction: estimated interaction effects with confidence intervals,

  • plot: a ggplot object (if plot = TRUE).

Examples


# Simple example: different trends between groups

set.seed(123)

n <- 60

marker <- rnorm(n, 10, 2)
group  <- rep(c("Control", "Treatment"), each = n/2)

# Same intercept, different slopes
response <- 2 +
  ifelse(group == "Control", 0.5, 1.2) * marker +
  rnorm(n, 0, 1)

test.interaction(marker, response, group)


autotestR documentation built on April 29, 2026, 1:09 a.m.