Learn R Programming

regrrr (version 0.1.3)

plot_effect: plotting the marginal effect of X on Y, with or without one or multiple interaction terms

Description

plotting the marginal effect of X on Y, with or without one or multiple interaction terms

Usage

plot_effect(
  reg.coef,
  data,
  model,
  by_color = FALSE,
  x_var.name = NULL,
  y_var.name = NULL,
  moderator.name = NULL,
  min_x = 0.001,
  max_x = 0.999,
  mdrt_quantile_05 = NULL,
  mdrt_quantile_50 = NULL,
  mdrt_quantile_95 = NULL,
  mod.n.sd = 1,
  confidence_interval = FALSE,
  v = NULL,
  CI_Ribbon = FALSE,
  title = NULL,
  xlab = "X_Var.name",
  ylab = "Y_Var.name",
  moderator.lab = "Moderator_name",
  mdrt.low.name = "Low",
  mdrt.mid.name = NULL,
  mdrt.high.name = "High",
  y.high.lim = NULL,
  y.low.lim = NULL,
  spline_labels = c("LHS", "RHS")
)

Arguments

reg.coef

a coefficient matrix of regression result, e.g. summary(lm_model)$coef

data

the data used in regression, a data frame

model

the model object, such as a "lm" object

by_color

plot interactions by colors, otherwise by line types

x_var.name

x name in the regression model, a string

y_var.name

y name in the regression model, a string

moderator.name

moderating variable name in the regression model, a string

min_x

the min of x scale, in percentile of x

max_x

the max of x scale, in percentile of x

mdrt_quantile_05

set the low level of moderator, in percentile

mdrt_quantile_50

set the middle level of moderator, in percentile

mdrt_quantile_95

set the high level of moderator, in percentile

mod.n.sd

set the moderating strength, in the number of s.d. units, which can take negative values

confidence_interval

if TRUE, plot confidence intervals

v

a customized variance-covariance matrix

CI_Ribbon

if TRUE, plot confidence interval ribbons, if FALSE, plot error bars

title

the title of the plot

xlab

label of X

ylab

label of Y

moderator.lab

label of moderator

mdrt.low.name

the label of low-level moderator

mdrt.mid.name

the label of mid-level moderator

mdrt.high.name

the label of high-level moderator

y.high.lim

specify the upper limit of y

y.low.lim

specify the lower limit of y

spline_labels

label of the spline variable; when the main varaible is a linear spline and spline labels are supplied, the moderation effect will be presented by facets.

Examples

Run this code
# NOT RUN {
data(mtcars)
m1 <- lm(mpg ~ vs + carb + hp + wt + wt * hp , data = mtcars)
plot_effect(reg.coef = summary(m1)$coefficients,
                 data = mtcars, model = m1,
                 x_var.name = "wt", y_var.name = "mpg", moderator.name = "hp",
                 confidence_interval = TRUE,  CI_Ribbon = TRUE,
                 xlab = "Weight", ylab = "MPG", moderator.lab = "Horsepower")
# }
# NOT RUN {
#' @examples
# }
# NOT RUN {
data(mtcars)
m2 <- lm(mpg ~ vs + carb + hp + wt + wt * hp + wt * vs, data = mtcars)
plot_effect(reg.coef = summary(m2)$coefficients,
            data = mtcars, model = m2,
            x_var.name = "wt", y_var.name = "mpg", moderator.name = "hp",
            confidence_interval = TRUE,  CI_Ribbon = FALSE,
            xlab = "Weight", ylab = "MPG", moderator.lab = "Horsepower")
# }
# NOT RUN {
#' @examples
# }
# NOT RUN {
data(mtcars)
m3 <- lm(mpg ~ vs + carb + hp + lspline(wt, knots = 4, marginal = FALSE) * hp, data = mtcars)
plot_effect(reg.coef=summary(m3)$coefficients, 
            data = mtcars, model = m3, x_var.name = "wt", y_var.name = "mpg", moderator.name = "hp",
            xlab="Weight", ylab="MPG", moderator.lab="Horsepower") 
# }
# NOT RUN {
# }
# NOT RUN {
# this shows the function is compatible with ggplot2 customization
library(extrafont)
m1 <- lm(mpg ~ vs + carb + hp + wt + wt * hp , data = mtcars)
plot_effect(reg.coef = summary(m1)$coefficients, 
                 data = mtcars, model = m1, 
                 x_var.name = "wt", y_var.name = "mpg", moderator.name = "hp", 
                 confidence_interval = TRUE,  CI_Ribbon = TRUE, 
                 xlab = "Weight", ylab = "MPG", moderator.lab = "Horsepower") + 
ggplot2::theme(text=ggplot2::element_text(family="Times New Roman", size = 16))
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab