add_interaction_terms: Creates interaction terms for specified variables in a data...

View source: R/utils.R

add_interaction_termsR Documentation

Creates interaction terms for specified variables in a data frame Interaction terms are named as <var1>_int_<var2> (e.g., Z1_int_Z2 for the product of Z1 and Z2).

Description

Creates interaction terms for specified variables in a data frame Interaction terms are named as <var1>_int_<var2> (e.g., Z1_int_Z2 for the product of Z1 and Z2).

Usage

add_interaction_terms(data, Z)

Arguments

data

Data frame. The data frame containing the variables for which interaction terms are to be created.

Z

Character vector. The names of the variables for which interaction terms are to be created.

Value

A list with two components:

  • data: The modified data frame with added interaction terms.

  • new_terms: A character vector of the names of the added interaction terms (e.g., Z1_int_2).

Examples

data_generator <-  function(N){
Z1 <- rnorm(N,0,1)
Z2 <- rnorm(N,0,1)
X <- rnorm(N, Z1 + Z2, 1)
Y <- rnorm(N, Z1 + Z2, 1)
df <- data.frame(Z1, Z2, X, Y)
return(df)
}
dat <- data_generator(250)
interaction_terms <- add_interaction_terms(data = dat, Z = c("Z1", "Z2"))
head(interaction_terms$data$Z1_int_Z2)


CCI documentation built on Aug. 29, 2025, 5:17 p.m.