plot2WayInteraction {petersenlab} | R Documentation |
Plot 2-way interaction.
Description
Generates a plot of a 2-way interaction.
Usage
plot2WayInteraction(
predictor,
outcome,
moderator,
predictorLabel = "predictor",
outcomeLabel = "outcome",
moderatorLabel = "moderator",
varList,
varTypes,
values = NA,
interaction = "normal",
legendLabels = NA,
legendLocation = "topright",
ylim = NA,
pvalues = TRUE,
data
)
Arguments
predictor |
character name of predictor variable (variable on x-axis). |
outcome |
character name of outcome variable (variable on y-axis). |
moderator |
character name of moderator variable (variable on z-axis). |
predictorLabel |
label on x-axis of plot |
outcomeLabel |
label on y-axis of plot |
moderatorLabel |
label on z-axis of plot |
varList |
names of predictor variables in model |
varTypes |
types of predictor variables in model; one of:
|
values |
specifies values at which to plot moderator (must specify
varType = |
interaction |
one of:
|
legendLabels |
vector of 2 labels for the two levels of the moderator;
leave as |
legendLocation |
one of: |
ylim |
vector of min and max values on y-axis (e,g., |
pvalues |
whether to include p-values of each slope in plot ( |
data |
name of data object |
Details
Generates a plot of a 2-way interaction: the association between a predictor and an outcome at two levels of the moderator.
Value
Plot of two-way interaction.
See Also
Other plot:
addText()
,
ppPlot()
,
vwReg()
Other multipleRegression:
lmCombine()
,
ppPlot()
,
update_nested()
Examples
# Prepare Data
predictor <- rnorm(1000, 10, 3)
moderator <- rnorm(1000, 50, 10)
outcome <- (1.7 * predictor) + (1.3 * moderator) +
(1.5 * predictor * moderator) + rnorm(1000, sd = 3)
covariate <- rnorm(1000)
df <- data.frame(predictor, moderator, outcome, covariate)
# Linear Regression
lmModel <- lm(outcome ~ predictor + moderator + predictor:moderator,
data = df, na.action = "na.exclude")
summary(lmModel)
# 1. Plot 2-Way Interaction
plot2WayInteraction(predictor = "predictor",
outcome = "outcome",
moderator = "moderator",
varList = c("predictor","moderator","covariate"),
varTypes = c("sd","binary","mean"),
data = df)
# 2. Specify y-axis Range
plot2WayInteraction(predictor = "predictor",
outcome = "outcome",
moderator = "moderator",
varList = c("predictor","moderator","covariate"),
varTypes = c("sd","binary","mean"),
ylim = c(10,50), #new
data = df)
# 3. Add Variable Labels
plot2WayInteraction(predictor = "predictor",
outcome = "outcome",
moderator = "moderator",
varList = c("predictor","moderator","covariate"),
varTypes = c("sd","binary","mean"),
ylim = c(10,50),
predictorLabel = "Stress", #new
outcomeLabel = "Aggression", #new
moderatorLabel = "Gender", #new
data = df)
# 4. Change Legend Labels
plot2WayInteraction(predictor = "predictor",
outcome = "outcome",
moderator = "moderator",
varList = c("predictor","moderator","covariate"),
varTypes = c("sd","binary","mean"),
ylim = c(10,50),
predictorLabel = "Stress",
outcomeLabel = "Aggression",
moderatorLabel = "Gender",
legendLabels = c("Boys","Girls"), #new
data = df)
# 5. Move Legend Location
plot2WayInteraction(predictor = "predictor",
outcome = "outcome",
moderator = "moderator",
varList = c("predictor","moderator","covariate"),
varTypes = c("sd","binary","mean"),
ylim = c(10,50),
predictorLabel = "Stress",
outcomeLabel = "Aggression",
moderatorLabel = "Gender",
legendLabels = c("Boys","Girls"),
legendLocation = "topleft", #new
data = df)
#6. Turn Off p-Values
plot2WayInteraction(predictor = "predictor",
outcome = "outcome",
moderator = "moderator",
varList = c("predictor","moderator","covariate"),
varTypes = c("sd","binary","mean"),
ylim = c(10,50),
predictorLabel = "Stress",
outcomeLabel = "Aggression",
moderatorLabel = "Gender",
legendLabels = c("Boys","Girls"),
legendLocation = "topleft",
pvalues = FALSE, #new
data = df)
#7. Get Regression Output from Mean-Centered Predictor and Moderator
plot2WayInteraction(predictor = "predictor",
outcome = "outcome",
moderator = "moderator",
varList = c("predictor","moderator","covariate"),
varTypes = c("sd","binary","mean"),
ylim = c(10,50),
predictorLabel = "Stress",
outcomeLabel = "Aggression",
moderatorLabel = "Gender",
legendLabels = c("Boys","Girls"),
legendLocation = "topleft",
interaction = "meancenter", #new
data = df)
#8. Get Regression Output from Orthogonalized Interaction Term
plot2WayInteraction(predictor = "predictor",
outcome = "outcome",
moderator = "moderator",
varList = c("predictor","moderator","covariate"),
varTypes = c("sd","binary","mean"),
ylim = c(10,50),
predictorLabel = "Stress",
outcomeLabel = "Aggression",
moderatorLabel = "Gender",
legendLabels = c("Boys","Girls"),
legendLocation = "topleft",
interaction = "orthogonalize", #new
data = df)