add_interaction_terms | R Documentation |
<var1>_int_<var2>
(e.g., Z1_int_Z2
for the product of Z1
and Z2
).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
).
add_interaction_terms(data, Z)
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. |
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
).
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.