I would like to plot an interaction (one independent variable -3 modalities treated as categorical-, one moderator variable -7 modalities treated as continuous; finally, a binary dependent variable -0 or 1).
Specifically, I am intending to make a graph with DV in y axis and the categorical IV in x axis. Now, I would like to plot two lines for my continuous moderator variable, representing the +1sd and the -1sd from the mean at each level of the 3 levels of the independent variable (as it is traditionally done in this kind of graphs), and not the seven lines that represent each of the modalities.
How can I ask the R software to calculate and display these two specific information pieces only in the graph using ggplot?
[EDIT 1] Here is a subset of my data: content is the categorical IV, Motivcentered, the moderator (continuous), resp is my DV (binary) :
structure(list(content = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3),
resp = c(1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1),
motivcentered = c(-0.25, 1.75, 1.75, -0.25, -2.25, 1.75, 1.75, -1.25, 0.75, -0.25, 0.75, -0.25, -4.25, -1.25, 1.75),
id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
item = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)),
.Names = c("content", "resp", "motivcentered", "id", "item"),
row.names = c(NA, -15L),
class = "data.frame", codepage = 65001L)
[EDIT 2] I'm trying to plot new data using now a categorical ID (with a continuous moderator and a categorical DV). The categorical ID is the only difference with my previous request (see above). I'm facing issues plotting such a graph (still plotting the +1sd, -1sd and mean lines for the moderator) since it does not display each modality of my IV (3 modalities, which should appear on the x-axis). Does any of you would know how to deal with that issue using the subset provided?
答案 0 :(得分:2)
Edit 1: After providing a small sample data set, I updated my answer. If the continuous variable is your moderator, I guess you want to have content
along the x-axis, predicted probabilities on the y-axis and different line for md, +1 and -1 sd for motivcentered
.
I copied your data into a data frame called mydat
:
# make categorical
mydat$content <- as.factor(mydat$content)
# fit model
fit <- glm(resp ~ content * motivcentered,
family = binomial("logit"),
data = mydat)
# load library
library(sjPlot)
# show plot
sjp.int(fit, type = "eff",
moderatorValues = "meansd",
swapPredictors = T)
moderatorValues
indicates, which values of the moderator variable you would like to use, option meansd
is mean, +1/-1 sd. By default, sjp.int
assumes the variable with less unique values to be the moderator, however, you want it the other way round. Thus, swapPredictors
now uses content
as DV along x-axis, and motivcentered
as moderator.
The plot looks like this (a bit strange, very likely due to the limited amount of observations):
For logistic regressions, it hardly makes sense to have just the values 0 and 1 on the y-axis, hence, the interaction effect on the predicted probabilities is shown.
You have various options to change the plot-appearance, e.g.:
sjp.int(fit,
type = "eff",
moderatorValues = "meansd",
swapPredictors = T,
showCI = T,
facet.grid = T,
legendLabels = c("-1 sd", "mean", "+1 sd"))
(Note that the plot again looks a bit strange, especially the confidence intervals, due to limited observations)
Edit 2: Original post first had no reproducible example, so I tried a "generic" guess here: Probably the sjp.int
function of the sjPlot-package works for you?
Assuming that you want to have the marginal effects of your interaction term, with mean and +/- 1 sd, the function call would look like this:
library(sjmisc) # for sample data
data(efc)
mydf <- data.frame(usage = efc$tot_sc_e,
sex = efc$c161sex,
education = efc$c172code,
burden = efc$neg_c_7,
barthel = efc$barthtot)
# convert gender predictor to factor
mydf$sex <- relevel(factor(mydf$sex), ref = "2")
# fit samplemodel
fit <- lm(usage ~ .*., data = mydf)
library(sjPlot)
sjp.int(fit,
type = "eff",
moderatorValues = "meansd")
The resulting plot may look like this:
The figure was taken from this package vignette, section Different moderator values for effect display plot type; the section, where all related examples are shown, is called Choose the values of continuous moderators intentionally.