Takes a formula and a dataframe as input, conducts an analysis of variance
using the base R aov function and prints the results
(AOV summary table, table of overall model information and
table of means) to the console and as a plotted interaction graph (line or
bar) using ggplot2. Also uses Brown-Forsythe test for homogeneity of
variance. Users can also choose to save the plot out as a png file.
Plot2WayANOVA(formula, dataframe = NULL, confidence=.95,
plottype = "bar", xlab = NULL, ylab = NULL, title = NULL,
subtitle = NULL, interact.line.size = 2, mean.plotting = TRUE,
mean.ci = TRUE, mean.size = 4, mean.color = "darkred",
PlotSave = FALSE)a valid R formula with a numeric dependent (outcome)
variable, and two independent (predictor) variables e.g. mpg~am*vs.
The independent variables are forced to factors (with warning) if possible.
a dataframe or an object that can be coerced to a dataframe
what confidence level for confidence intervals
bar or line (quoted)
Labels for `x` and `y` axis variables. If `NULL` (default), variable names for `x` and `y` will be used.
The text for the plot title. A generic default is provided.
The text for the plot subtitle. If `NULL` (default), key model information is provided as a subtitle.
Line size for the line connecting mean points (Default: `2`).
Logical that decides whether mean is to be highlighted and its value to be displayed (Default: `TRUE`).
Logical that decides whether 95 is to be displayed (Default: `TRUE`).
Point size for the data point corresponding to mean (Default: `4`).
Color for the data point corresponding to mean (Default: `"darkred"`).
a logical indicating whether the user wants to save the plot as a png file
A list with 5 elements which is returned invisibly. The items are always sent
to the console for display The plot is always sent to the default plot device
but for user convenience the function also returns a named list with the following items
in case the user desires to save them or further process them. $ANOVATable,
$ModelSummary, $MeansTable, $BFTest, and $SWTest.
Details about how the function works in order of steps taken.
Some basic error checking to ensure a valid formula and dataframe. Only accepts fully *crossed* formula to check for interaction term
Ensure the dependent (outcome) variable is numeric and that the two independent (predictor) variables are or can be coerced to factors -- user warned on the console
Remove missing cases -- user warned on the console
Use dplyr to calculate a summarized table of means,
sds, standard errors of the means, confidence intervals, and group sizes.
Use the aov function to execute an Analysis of Variance (ANOVA)
Use the anova_stats function to calculate eta squared values.
If the design is unbalanced warn the user and use Type II sums of squares
Produce a standard ANOVA table with a column for eta-squared appended
Use the leveneTest for testing Homogeneity of Variance
assumption with Brown-Forsythe
Use the shapiro.test for testing normality assumption with Shapiro-Wilk
Use ggplot2 to plot an interaction plot of the type the user specified
# NOT RUN {
Plot2WayANOVA(mpg ~ am * cyl, mtcars, plottype = "line")
Plot2WayANOVA(mpg ~ am * vs, mtcars, confidence = .99)
# }
Run the code above in your browser using DataLab