# Example of a three-way interaction plot in R

Is this an example of interaction plot?
However, in the bottom of the article it is wrtitten, that it's not a 3-way-interaction (link to article). How should I understooth it? If it's not a 3-way-interaction plot then how can it been done in R?

library(MASS)
library(effects)

wvs.2 <- polr(poverty ~ country*(gender + religion + degree + ns(age, 4)),data = WVS)

plot(Effect(focal.predictors = c("country","age","gender"), mod = wvs.2, xlevels = list(age = 20:80), latent = TRUE), rug = FALSE, ylim = c(0,3.5))


Edited - new question:
WHat does ns(, 4) exactly do? I know that if we didn't use this the the lines will be linear but I just didn't understooth what it exactly does. Is it similar to something like agea^2? Or boes ns(age, 4) for instance take the 4th number for age and then plot and that's why it's curved instead of linear? in the paper by John fox they use bs(, 4) instead. It does the same. I know that it is explained in the article and in the paper by JOhn FOx but I didn't understooth their explanation.

EDIT 2
The summary of the estimated model shows interactions between countryage and countrygender which are the 3 variables plotted in the picture above. My question is then is the plot actually a plot/visualization of 2 different interactions? 1 for countryage and 1 for countrygender?
I'm asking because I have to write about a similar plot with different data and variables etc. But I used the same method, however I'm in doubt about what I should write. Is it a two-way interaction with the effect of gender added or is it 2 different 2-way interaction plots combined in multiple plots?

> Anova(wvs.1)
Analysis of Deviance Table (Type II tests)

Response: poverty
LR Chisq Df Pr(>Chisq)
country           250.881  3  < 2.2e-16 ***
gender             10.749  1  0.0010435 **
religion            4.132  1  0.0420698 *
degree              4.284  1  0.0384725 *
age                49.950  1  1.577e-12 ***
country:gender      3.049  3  0.3841657
country:religion   21.143  3  9.833e-05 ***
country:degree     12.861  3  0.0049476 **
country:age        17.529  3  0.0005501 ***

• Your edit is really a separate question. ns(, 4) transforms your data using natural splines, with 4 knots. bs() transforms using basis splines. The transformations are (AFAIK) different, but the fits can of course be similar. Take a look at the help pages, ?ns and ?bs. Frank Harrell's Regression Modeling Strategies has a very good discussion of splines. We also have a splines tag. This search may be helpful. Nov 29, 2022 at 14:07

This is not an interaction plot in the strict sense of the term. An interaction plot has a single plot and multiple lines. The horizontal axis shows a predictor (categorical or continuous), the vertical axis is a response, and the multiple fitted lines show how the fitted response depends on the predictor, where each line corresponds to one level of a grouping factor. Take a look at the figure in the Wikipedia article: the horizontal axis is education level, the vertical axis is proclivity to care about sea level rise, and the grouping factor is political stance with three levels, thus we get three lines.

Your plot is paneled. It does show (or can show) a three-way interaction, simply not in a single plot, but in eight plots, because you have one continuous predictor (age, on the horizontal axis) and two categorical predictors (gender in rows and countries in columns). This is a very good representation, which allows us to see how the response-age relationship varies by country and gender.

Your confusion stems from the fact that this could show a three-way interaction, but in this particular case, no three-way interaction was modeled. The model included a two-way interaction between country and age and an additional main effect of gender. Thus, the curves differ between countries (that's the country-age interaction), but they have the same shape between genders within each country, they are just shifted up or down between the genders (that's the main effect of gender). If the model hadn't included gender at all, then you could of course still create a plot like this, but then both rows would be identical, because there would be no fitted difference between genders.

• Thanks for yuor answer! So it's not a 3-way interaction but a 2-way interaction of country*age and they just added an extra variabel which is gender to see how the the effects vary? But the curves does not vary for gender because they did not fit a 3-way interaction between country, age and gender? If I want to fit a 3-way interaction I can then plot polr(poverty ~ country*(gender + religion + degree + ns(age, 4) + ns(age, 4)*gndr),data = WVS) Is all this correct understooth and enough?
– rr19
Nov 29, 2022 at 13:30
• I edited the post with a new question after the plot. I would really appreciate if you could give an answer on that if possible of course.
– rr19
Nov 29, 2022 at 13:31
• one last question: should plot in my post actually not be understooth at two different interactions visualized in 8 panels/plots Because in the fitted model it is country*(gender + ... + ns(age,4) so it's actually the interaction of country*age and country*gender so can it be interpretted as two different interaction combined into 8 plots? Hope my explanation makes sense
– rr19
Nov 29, 2022 at 14:32
• If you really want a 3-way interaction, then something like polr(poverty~gender*religion*ns(age,4), data=WVS) should do the trick, depending on your dataset. Try it and take a look at the results. I'm afraid I don't quite understand your last comment... The model plotted is something like poverty~country*ns(age,4)+gender. Does this help? Nov 29, 2022 at 14:59