Yulia Kentieva
Yulia Kentieva

Reputation: 720

How to plot interactions of predictors when the response data is ordinal in r?

I have a dataset with an ordinal target variable. I need to draw an interaction plot of one continuous and one categorical value to see if their interaction matters.

Here, I will use diamonds built-in dataset for reproducibility. Let's pretend that "cut" is a target variable. I tried to use this:

interaction.plot(diamonds$carat, diamonds$clarity, diamonds$cut)

which gives me this error:

Error in plot.window(...) : need finite 'ylim' values

It works for continuous target variables. But my data has an ordinal response variable. Should I recode my target variable for this function or is there a better way of plotting it?

The plot should look like this (https://www.statology.org/interaction-plot-r/):

enter image description here

Upvotes: 1

Views: 365

Answers (1)

Kat
Kat

Reputation: 18754

You have to make the binary field numeric for it to work.

library(tidyverse)

data("diamonds")
str(diamonds)

df1 <- diamonds %>% 
  filter(cut %in% c("Fair", "Good")) %>%    # Make it binary
  mutate(cut = ifelse(cut == "Fair", 0, 1)) # Make it numeric

# check it
str(df1)

# plot it
interaction.plot(x.factor = df1$color, 
                 trace.factor = df1$clarity, 
                 response = df1$cut,
                 trace.label = "Clarity")

enter image description here

Upvotes: 1

Related Questions