Moderated mulitple regression is a common analysis in psychology. Unfortunately, there are a large number of steps associated with creating the desired output and code for both 2D and 3D graphs of the moderated regression.
This package conducts the required analyses and creates both 2D and 3D graphs of the moderated regression using ggplot and plot.ly, respectively. Additionally, regression results of the analyses are displayed in the console, output in list format, and embeded in APA style tables (as per apaTables) if a filename (ending in .doc or .rtf) is specified.
The development version of fastInteraction hosted here on Github. Eventually, a version will be posted to the CRAN.
Currently only interactions between two continous variables is supported.
install.packages("devtools") devtools::install_github("dstanley4/fastInteraction") library(fastInteraction)
fastInteraction uses data (i.e., cohen_data) consistent with Table 7.4.1 from Cohen, Cohen, West, and Aiken (2003) as the demonstration data set so that values obtained in the output can be checked against those in the book.
The code below creates both 2D and 3D graphs and displays regression results to the console:
fast.int(data = cohen_exercise, criterion = endurance, predictor = age, moderator = exercise, center.predictors = TRUE)
Slightly modified code creates both 2D and 3D graphs (with custom axis labels), displays regression results to the console, and creates a Word document with the regression tables in APA style.
new_axis_labels <- list(criterion = "Endurance", predictor = "Age (centered)", moderator = "Exercise (centered)") fast.int(data = cohen_exercise, criterion = endurance, predictor = age, moderator = exercise, center.predictors = TRUE, axis.labels = new_axis_labels)
If you specify a path with a filename (e.g., path = "tables.doc") then the tables below will be created in Word document.
You can also put the output into a custom object and access key components such different versions of the graphs, the regression object itself, even the exact data analyzed after centering and drops due to missing data.
library(fastInteraction) new_axis_labels <- list(criterion = "Endurance", predictor = "Age (centered)", moderator = "Exercise (centered)") myoutput <- fast.int(data = cohen_exercise, criterion = endurance, predictor = age, moderator = exercise, center.predictors = TRUE, axis.labels = new_axis_labels)
# Obtain the ggplot graph presented in the Plots tab formatted.ggplot.graph <- myoutput$graph2D.formatted ggsave("graph2D.png", formatted.ggplot.graph)
# Obtain the ggplot graph presented prior to the formatting commands being applied # If yout want to modify this graph simply add your own ggplot command as illustrated below: unformatted.ggplot.graph <- myoutput$graph2D.unformatted custom.formatted.ggplot.graph <- unformatted.ggplot.graph + coord_cartesian(ylim = c(0, 60)) + theme_grey(18) ggsave("graph2Dcustom.png", custom.formatted.ggplot.graph)
Saving the 3D graph is a bit tricky. The easiest way is to adjust the graph until it is in the orientation you like in the Viewer panel of RStudio and then use the buttons in the Viewer to save it. Note that with this approach, the larger the graph is on your screen the larger the dimensions of the final graph will be. Make it very large to obtain a version of the graph that has a resolution sufficiently high for publication.
A beter, but more complicated, approach for obtaining a publication quality graph is to install orca install link here. I suggest using Method 4 Stand alone binaries because it is a bit easier.
A complication with saving the 3D graph via orca is that you need to set the orientation of the graph in the code you use to create the graph. I suggest using the fast.int command several times, each time varing the camera positions (cam.position), to obtain the desired graph orientation. Once the desired orientation is obtained, the graph can be saved with the orca command. You specify the camera position via cam.position using theta (horizontal rotation angle in degrees -180 to 180, most likely), phi (vertical rotation angle in degrees -90 to 90, most likely), and distance (from the origin of the graph, start with 3 or 4 and increase this value if needed).
library(fastInteraction) new_axis_labels <- list(criterion = "Endurance", predictor = "Age (centered)", moderator = "Exercise (centered)") cam_position <- list(theta = -20, phi = 20, distance = 2.5) myoutput <- fast.int(data = cohen_exercise, criterion = endurance, predictor = age, moderator = exercise, center.predictors = TRUE, axis.labels = new_axis_labels, cam.position = cam_position) graph3D <- myoutput$graph3D library(plotly) # This next line requires you install orca software as per link above # This lines saves 1000 pixel by 1000 pixel version of the graph in PNG format orca(graph3D, file = "graph3D.png", width = 1000, height = 1000)
# Obtain the regression object presented in tables regression.lm.object <- myoutput$regression.lm.object print(regression.lm.object) summary(regression.lm.object)
# Obtain the regression object presented in tables regression.lm.object <- myoutput$regression.lm.object data_analyzed <- regression.lm.object$model head(data_analyzed)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.