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:
"mean"= plots at mean of variable – should be used for ALL covariates (apart from main predictor and moderator)"sd"= plots at +/- 1 sd of variable (for most continuous predictors and moderators)"binary"= plots at values of 0,1 (for binary predictors and moderators)"full"= plots full range of variable (for variables like age when on x-axis)"values"= allows plotting moderator at specific values (e.g.,0,1,2)"factor"= plots moderator at different categories (e.g.,TRUE,FALSE)
- values
specifies values at which to plot moderator (must specify varType =
"values")- interaction
one of:
"normal"= standard interaction"meancenter"= calculates the interaction from a mean-centered predictor and moderator (subtracting each individual's value from the variable mean to set the mean of the variable to zero)"orthogonalize"= makes the interaction orthogonal to the predictor and moderator by regressing the interaction on the predictor and outcome and saving the residual
- legendLabels
vector of 2 labels for the two levels of the moderator; leave as
NAto see the actual levels of the moderator- legendLocation
one of:
"topleft","topright","bottomleft", or"bottomright"- ylim
vector of min and max values on y-axis (e,g.,
0,10)- pvalues
whether to include p-values of each slope in plot (
TRUEorFALSE)- 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.
See also
Other plot:
addText(),
ppPlot(),
semPlotInteraction(),
vwReg()
Other multipleRegression:
lmCombine(),
ppPlot(),
semPlotInteraction(),
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)
#>
#> Call:
#> lm(formula = outcome ~ predictor + moderator + predictor:moderator,
#> data = df, na.action = "na.exclude")
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -10.0896 -1.9659 -0.0187 2.1029 8.7619
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) -0.216485 1.686134 -0.128 0.898
#> predictor 1.745824 0.161956 10.780 <2e-16 ***
#> moderator 1.313737 0.033451 39.273 <2e-16 ***
#> predictor:moderator 1.498493 0.003225 464.656 <2e-16 ***
#> ---
#> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#>
#> Residual standard error: 3.011 on 996 degrees of freedom
#> Multiple R-squared: 0.9999, Adjusted R-squared: 0.9999
#> F-statistic: 2.918e+06 on 3 and 996 DF, p-value: < 2.2e-16
#>
# 1. Plot 2-Way Interaction
plot2WayInteraction(predictor = "predictor",
outcome = "outcome",
moderator = "moderator",
varList = c("predictor","moderator","covariate"),
varTypes = c("sd","binary","mean"),
data = df)
#>
#> Call:
#> lm(formula = modelFormula, data = modelData, na.action = "na.omit")
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -10.1568 -1.9395 -0.0341 2.1356 9.2076
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) -0.158696 1.684676 -0.094 0.9250
#> predictor 1.740697 0.161812 10.758 <2e-16 ***
#> moderator 1.312281 0.033426 39.259 <2e-16 ***
#> covariate -0.175114 0.099412 -1.761 0.0785 .
#> predictor:moderator 1.498620 0.003222 465.071 <2e-16 ***
#> ---
#> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#>
#> Residual standard error: 3.007 on 995 degrees of freedom
#> Multiple R-squared: 0.9999, Adjusted R-squared: 0.9999
#> F-statistic: 2.193e+06 on 4 and 995 DF, p-value: < 2.2e-16
#>
# 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)
#>
#> Call:
#> lm(formula = modelFormula, data = modelData, na.action = "na.omit")
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -10.1568 -1.9395 -0.0341 2.1356 9.2076
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) -0.158696 1.684676 -0.094 0.9250
#> predictor 1.740697 0.161812 10.758 <2e-16 ***
#> moderator 1.312281 0.033426 39.259 <2e-16 ***
#> covariate -0.175114 0.099412 -1.761 0.0785 .
#> predictor:moderator 1.498620 0.003222 465.071 <2e-16 ***
#> ---
#> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#>
#> Residual standard error: 3.007 on 995 degrees of freedom
#> Multiple R-squared: 0.9999, Adjusted R-squared: 0.9999
#> F-statistic: 2.193e+06 on 4 and 995 DF, p-value: < 2.2e-16
#>
# 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)
#>
#> Call:
#> lm(formula = modelFormula, data = modelData, na.action = "na.omit")
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -10.1568 -1.9395 -0.0341 2.1356 9.2076
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) -0.158696 1.684676 -0.094 0.9250
#> predictor 1.740697 0.161812 10.758 <2e-16 ***
#> moderator 1.312281 0.033426 39.259 <2e-16 ***
#> covariate -0.175114 0.099412 -1.761 0.0785 .
#> predictor:moderator 1.498620 0.003222 465.071 <2e-16 ***
#> ---
#> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#>
#> Residual standard error: 3.007 on 995 degrees of freedom
#> Multiple R-squared: 0.9999, Adjusted R-squared: 0.9999
#> F-statistic: 2.193e+06 on 4 and 995 DF, p-value: < 2.2e-16
#>
# 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)
#>
#> Call:
#> lm(formula = modelFormula, data = modelData, na.action = "na.omit")
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -10.1568 -1.9395 -0.0341 2.1356 9.2076
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) -0.158696 1.684676 -0.094 0.9250
#> predictor 1.740697 0.161812 10.758 <2e-16 ***
#> moderator 1.312281 0.033426 39.259 <2e-16 ***
#> covariate -0.175114 0.099412 -1.761 0.0785 .
#> predictor:moderator 1.498620 0.003222 465.071 <2e-16 ***
#> ---
#> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#>
#> Residual standard error: 3.007 on 995 degrees of freedom
#> Multiple R-squared: 0.9999, Adjusted R-squared: 0.9999
#> F-statistic: 2.193e+06 on 4 and 995 DF, p-value: < 2.2e-16
#>
# 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)
#>
#> Call:
#> lm(formula = modelFormula, data = modelData, na.action = "na.omit")
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -10.1568 -1.9395 -0.0341 2.1356 9.2076
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) -0.158696 1.684676 -0.094 0.9250
#> predictor 1.740697 0.161812 10.758 <2e-16 ***
#> moderator 1.312281 0.033426 39.259 <2e-16 ***
#> covariate -0.175114 0.099412 -1.761 0.0785 .
#> predictor:moderator 1.498620 0.003222 465.071 <2e-16 ***
#> ---
#> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#>
#> Residual standard error: 3.007 on 995 degrees of freedom
#> Multiple R-squared: 0.9999, Adjusted R-squared: 0.9999
#> F-statistic: 2.193e+06 on 4 and 995 DF, p-value: < 2.2e-16
#>
#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)
#>
#> Call:
#> lm(formula = modelFormula, data = modelData, na.action = "na.omit")
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -10.1568 -1.9395 -0.0341 2.1356 9.2076
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) -0.158696 1.684676 -0.094 0.9250
#> predictor 1.740697 0.161812 10.758 <2e-16 ***
#> moderator 1.312281 0.033426 39.259 <2e-16 ***
#> covariate -0.175114 0.099412 -1.761 0.0785 .
#> predictor:moderator 1.498620 0.003222 465.071 <2e-16 ***
#> ---
#> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#>
#> Residual standard error: 3.007 on 995 degrees of freedom
#> Multiple R-squared: 0.9999, Adjusted R-squared: 0.9999
#> F-statistic: 2.193e+06 on 4 and 995 DF, p-value: < 2.2e-16
#>
#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)
#>
#> Call:
#> lm(formula = modelFormula, data = modelDataMC, na.action = "na.omit")
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -10.1568 -1.9395 -0.0341 2.1356 9.2076
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 824.525940 0.095153 8665.253 <2e-16 ***
#> predictor 76.610898 0.031467 2434.657 <2e-16 ***
#> moderator 16.161836 0.009443 1711.528 <2e-16 ***
#> covariate -0.175114 0.099412 -1.761 0.0785 .
#> predictor:moderator 1.498620 0.003222 465.071 <2e-16 ***
#> ---
#> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#>
#> Residual standard error: 3.007 on 995 degrees of freedom
#> Multiple R-squared: 0.9999, Adjusted R-squared: 0.9999
#> F-statistic: 2.193e+06 on 4 and 995 DF, p-value: < 2.2e-16
#>
#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)
#>
#> Call:
#> lm(formula = modelFormulaOrtho, data = modelDataOrtho, na.action = "na.omit")
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -10.1568 -1.9395 -0.0341 2.1356 9.2076
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) -7.359e+02 5.791e-01 -1270.834 <2e-16 ***
#> predictor 7.557e+01 3.139e-02 2407.785 <2e-16 ***
#> moderator 1.622e+01 9.442e-03 1718.241 <2e-16 ***
#> covariate -1.751e-01 9.941e-02 -1.761 0.0785 .
#> predictorXmoderator 1.499e+00 3.222e-03 465.071 <2e-16 ***
#> ---
#> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#>
#> Residual standard error: 3.007 on 995 degrees of freedom
#> Multiple R-squared: 0.9999, Adjusted R-squared: 0.9999
#> F-statistic: 2.193e+06 on 4 and 995 DF, p-value: < 2.2e-16
#>
