Skip to content

Instantly share code, notes, and snippets.

@BlasBenito
Created March 25, 2021 20:56
Show Gist options
  • Save BlasBenito/e360cf329cf523ab3407baa61aa97a9e to your computer and use it in GitHub Desktop.
Save BlasBenito/e360cf329cf523ab3407baa61aa97a9e to your computer and use it in GitHub Desktop.
Shows how variable interaction shapes change with scaling or a variable having 0 in its range
#preparing data
library(car)
data("Prestige")
df <- Prestige[, c(
"income",
"education",
"prestige"
)]
#making a version of education with its range crossing 0
df$education_0 <- df$education - mean(df$education)
#raw nteraction
df$raw_interaction <- df$education * df$prestige
#raw interaction, 1 crossing 0
df$raw_interaction_0 <- df$education_0 * df$prestige
#interaction of scaled variables
df$interaction_of_scaled <- scale(df$education) * scale(df$prestige)
#interaction of scaled variables, 1 crossing 0
df$interaction_of_scaled_0 <- scale(df$education_0) * scale(df$prestige)
#scaled interaction
df$scaled_interaction <- scale(df$education * df$prestige)
#scaled interaction, 1 crossing 0
df$scaled_interaction_0 <- scale(df$education_0 * df$prestige)
#plotting all that
par(mfrow=c(3, 2))
plot(df$raw_interaction, df$income, main = "interaction of raw variables", xlab = "Interaction", ylab = "Response")
plot(df$raw_interaction_0, df$income, main = "interaction of raw variables (one crosses 0)", xlab = "Interaction", ylab = "Response")
plot(df$interaction_of_scaled, df$income, main = "interaction of scaled variables", xlab = "Interaction", ylab = "Response")
plot(df$interaction_of_scaled_0, df$income, main = "interaction of scaled variables (one crosses 0)", xlab = "Interaction", ylab = "Response")
plot(df$scaled_interaction, df$income, main = "scaled interaction", xlab = "Interaction", ylab = "Response")
plot(df$scaled_interaction_0, df$income, main = "scaled interaction (one crosses 0)", xlab = "Interaction", ylab = "Response")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment