Visualization of trilateral interaction between two continuous variables and one categorical variable in R

I have a model in R that includes a significant three-way interaction between two continuous independent variables IVContinuousA, IVContinuousB, IV Categorical and one categorical variable (with two levels: control and treatment). Dependent Variable Continuous (DV).

model <- lm(DV ~ IVContinuousA * IVContinuousB * IVCategorical)

Here you can find the data

I'm trying to figure out a way to visualize this in R, to make it easier to interpret (maybe in ggplot2?).

This blog post was a bit inspired. I thought I could dichotomize IVContinuousBto high and low values ​​(so that would be two level itself:

IVContinuousBHigh <- mean(IVContinuousB) + sd (IVContinuousB) 
IVContinuousBLow <- mean(IVContinuousB) - sd (IVContinuousB)

Then I planned to build a connection between DV and IV ContinuousA and match the lines representing the slopes of this relationship for different combinations of category IV and my new dichotomous IVContinuousB:

IVCategoricalControland IVContinuousBHigh
IVCategoricalControland IVContinuousBLow
IVCategoricalTreatmentand IVContinuousBHigh
IVCategoricalTreatmentandIVContinuousBLow

My first question is that this seems like a viable solution to create an interpretable timeline for this triangular interaction? I want to avoid 3D graphics if possible, since I do not find them intuitive ... Or is there another way to do this? Perhaps the graphic plots for the different combinations above?

If this is an approved solution, my second question is how can I generate data to predict suitable rows to represent the various combinations above?

: - - , ggplot2?

Cross Validated, , , ( CV-, :))

,

Sarah

, DV NA ( ), - IV. .

FYI IVContinuousA IV :

A < -ggplot (data = data, aes (x = AOTAverage, y = SciconC, group = MisinfoCondition, shape = MisinfoCondition, col = MisinfoCondition,)) + geom_point (size = 2) + geom_smooth (method = 'lm ", = ~ )

, IVContinuousB....

+4
2

. , Treatment Control

library(tidyverse)
  theme_set(theme_classic() +
          theme(panel.background=element_rect(colour="grey40", fill=NA))

dat = read_excel("Some Data.xlsx")  # I downloaded your data file

mod <- lm(DV ~ IVContinuousA * IVContinuousB * IVCategorical, data=dat)

# Function to create prediction grid data frame
make_pred_dat = function(data=dat, nA=20, nB=5) {
  nCat = length(unique(data$IVCategorical))
  d = with(data, 
           data.frame(IVContinuousA=rep(seq(min(IVContinuousA), max(IVContinuousA), length=nA), nB*2),
                      IVContinuousB=rep(rep(seq(min(IVContinuousB), max(IVContinuousB), length=nB), each=nA), nCat),
                      IVCategorical=rep(unique(IVCategorical), each=nA*nB)))

  d$DV = predict(mod, newdata=d)

  return(d)
}

IVContinuousA DV IVContinuousB

IVContinuousA IVContinuousB , , .

ggplot(make_pred_dat(), aes(x=IVContinuousA, y=DV, colour=IVCategorical)) + 
  geom_line() +
  facet_grid(. ~ round(IVContinuousB,2)) +
  ggtitle("IVContinuousA vs. DV, by Level of IVContinousB") +
  labs(colour="")

enter image description here

, IVContinuousB:

ggplot(make_pred_dat(nB=3), 
       aes(x=IVContinuousA, y=DV, colour=IVCategorical, linetype=factor(round(IVContinuousB,2)))) + 
  geom_line() +
  #facet_grid(. ~ round(IVContinuousB,2)) +
  ggtitle("IVContinuousA vs. DV, by Level of IVContinousB") +
  labs(colour="", linetype="IVContinuousB") +
  scale_linetype_manual(values=c("1434","11","62")) +
  guides(linetype=guide_legend(reverse=TRUE))

enter image description here

, DV - DV IVContinuousA IVContinuousB

IVContinuousA IVContinuousB.

ggplot(make_pred_dat(nA=100, nB=100) %>% 
         group_by(IVContinuousA, IVContinuousB) %>% 
         arrange(IVCategorical) %>% 
         summarise(DV = diff(DV)), 
       aes(x=IVContinuousA, y=IVContinuousB)) + 
  geom_tile(aes(fill=DV)) +
  scale_fill_gradient2(low="red", mid="white", high="blue") +
  labs(fill=expression(Delta*DV~(Treatment - Control)))

enter image description here

+5

, .

Duncan car, , , .

library(car)
# the data
data("Duncan")

# the fitted model; education and income are continuous, type is categorical
lm0 <- lm(prestige ~ education * income * type, data = Duncan)

# turning education into high and low values (you can extend this to more 
# levels)
edu_high <- mean(Duncan$education)  + sd(Duncan$education)
edu_low <- mean(Duncan$education)  - sd(Duncan$education)

# the values below should be used for predictions, each combination of the 
# categories must be represented:
prediction_mat <- data.frame(income = Duncan$income, 
                         education = rep(c(edu_high, edu_low),each = 
                         nrow(Duncan)),
                         type = rep(levels(Duncan$type), each = 
                         nrow(Duncan)*2))


predicted <- predict(lm0, newdata = prediction_mat)


# rearranging the fitted values and the values used for predictions
df <- data.frame(predicted,
             income = Duncan$income,
             edu_group =rep(c("edu_high", "edu_low"),each = nrow(Duncan)),
             type = rep(levels(Duncan$type), each = nrow(Duncan)*2))


# plotting the fitted regression lines
ggplot(df, aes(x = income, y = predicted, group = type, col = type)) + 
geom_line() + 
facet_grid(. ~ edu_group)

enter image description here

+5

Source: https://habr.com/ru/post/1694405/


All Articles