The function creates a two-way interaction plot. It will creates a plot with ± 1 SD from the mean of the independent variable. See supported model below. I recommend using concurrently with lm_model or lme_model.

two_way_interaction_plot(
model,
data = NULL,
graph_label_name = NULL,
cateogrical_var = NULL,
y_lim = NULL,
plot_color = FALSE
)

Arguments

model

object from lm, nlme, lme4, or lmerTest

data

data frame. If the function is unable to extract data frame from the object, then you may need to pass it directly

graph_label_name

vector of length 3 or function. Vector should be passed in the form of c(response_var, predict_var1, predict_var2). Function should be passed as a switch function that return the label based on the name passed (e.g., a switch function)

cateogrical_var

list. Specify the upper bound and lower bound directly instead of using ± 1 SD from the mean. Passed in the form of list(var_name1 = c(upper_bound1, lower_bound1),var_name2 = c(upper_bound2, lower_bound2))

y_lim

the plot's upper and lower limit for the y-axis. Length of 2. Example: c(lower_limit, upper_limit)

plot_color

default if FALSE. Set to TRUE if you want to plot in color

Value

an object of class ggplot

Details

It appears that predict cannot handle categorical factors. All variables are converted to numeric before plotting.

Examples

# If you pass the model directly, it can't extract the data-frame from fit object
# Therefore, for now, you must pass the data frame to the function.
# You don't need pass the data if you use lm_model or lme_model.

# lme example
lme_fit <- lme4::lmer("popular ~ extrav*texp + (1 + extrav | class)",
data = popular
)
#> boundary (singular) fit: see help('isSingular')

two_way_interaction_plot(lme_fit,
graph_label_name = c("popular", "extraversion", "teacher experience"),
data = popular
)

lm_fit <- lm(Sepal.Length ~ Sepal.Width * Petal.Width,
data = iris
)

two_way_interaction_plot(lm_fit, data = iris)

label_name <- function(var_name) {
var_name_processed <- switch(var_name,
"extrav" = "Extroversion",
"texp" = "Teacher Experience",
"popular" = "popular"
)
if (is.null(var_name_processed)) {
var_name_processed <- var_name
}
return(var_name_processed)
}

two_way_interaction_plot(lme_fit, data = popular, graph_label_name = label_name)

`