2

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?

theB
  • 6,450
  • 1
  • 28
  • 38
Bastien
  • 23
  • 4
  • Please take the time to create a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). You should provide sample input. If possible, maybe you can sketch your desired output, or find another image of a plot that looks like what you are trying to create. This will make it easier to help you. – MrFlick Jul 31 '15 at 19:42

1 Answers1

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):

enter image description here

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)

enter image description here

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:

enter image description here

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.

Daniel
  • 7,252
  • 6
  • 26
  • 38
  • So, pray tell, what exactly might `fit` actually be? – IRTFM Aug 01 '15 at 03:45
  • A fitted model as returned by, for instance, lm, glm, lmer, glmer... with interaction terms. See linked package vignette in my post. – Daniel Aug 01 '15 at 06:00
  • Ok, I added the complete code as shown in the package vignette, so it's directly reproducible. – Daniel Aug 01 '15 at 19:05
  • I upvoted it because it is "useful" in illustrating the notion of an interaction plot, but I don't think it actually addresses the request for illustrating the use of ggplot2 for an interaction plot using a logistic regression model. I suspect that producing a ggplot "interaction plot" won't be too difficult, but the questioner has not responded the request for a dataset to use as an example. – IRTFM Aug 01 '15 at 19:44
  • Dear all, I sincerely thank you for your reply. I pologize for not providing a dataset, I am not familiar with the forum. Well, as a beginner, I don't even know how to share my dataset (I'm running it from a spss file, using the Rcmdr command). Maybe would you let me know the way to sahre that datased if it can be useful for the community. Thanks again for your great help. – Bastien Aug 01 '15 at 20:42
  • @BondedDust why does it not illustrate interactions for glm? `sjp.int` produces a ggplot-object and works for glm(er) objects as well. – Daniel Aug 01 '15 at 20:45
  • @Bastien See comment to your post, where @MrFlick posted a link with descriptions on how to do a reproducible example. I suggest loading the SPSS data with the `haven` package, and you can paste your data frame that is needed for the example using `dput` in R. Simply copy/paste the output of `dput`. – Daniel Aug 01 '15 at 20:47
  • Regarding my comment: Perhaps posting one of the later examples from the ?sjp.int help page instead of just posting the first one on the page would have made clear that binomial models were also handled. When I look at the class of the value returned from that plotting call it is "sjpint" and returns FALSE for inheritance of either "gg" or "ggplot". – IRTFM Aug 01 '15 at 21:07
  • Ok, agree. I better could have chosen an example with glm. The return value is a list with both the ggplot-object and the data frame that makes up the plot (for example, to build your own ggplot). – Daniel Aug 01 '15 at 21:11
  • @Daniel,This is exactly the kind of figure I was looking for. Thanks again (and all the community) for your helpful explanations (and despite my inaccuracies in explaining my issue). Best – Bastien Aug 02 '15 at 21:33
  • @Bastien if my post answers your question, I would appreciate if you "accept" it. :-) – Daniel Aug 03 '15 at 12:31