1

I have been trying to solve this for days, so any help would be appreciated!

I am trying to make an interaction plot for an OLS Regression. This is the code I am using:

interact <- lm(ele$vt_c ~ ele$Immigrants:ele$X.qual, data = as.data.frame(ele))
interact_plot(model = interact, pred=Immigrants, modx =X.qual, modx.values = NULL, data = ele)

This is the error that is coming up

Error in ecdf(d[[modx]]) : 'x' must have 1 or more non-missing values
In addition: Warning message:
immigrants and X.qual are not included in an interaction with one another in the model. 

Reproducible data

if (!"interactions" %in% installed.packages())  install.packages("interactions")
library(interactions)

ele = structure(list(vt_c = c(68.37056, 67.55938, 69.25354, 67.54727, 
                          67.39343, 67.81161, 65.81312, 64.68675, 70.8572, 72.1439, 67.39006, 
                          64.89897, 62.81833, 63.82975, 58.99062, 67.69617, 68.17096, 65.24267, 
                          67.08106, 66.47592, 68.40781, 70.40636, 69.50657, 72.37613, 70.24236, 
                          67.50159, 71.77177, 67.09047, 74.58491, 70.64892, 65.20199, 70.03566, 
                          70.23142, 71.62487, 66.87982, 70.72528, 66.97507, 69.38713, 67.20061, 
                          68.79907, 67.05735, 67.38101, 66.10595, 60.97635, 61.9047, 61.28828, 
                          72.11577, 63.04311, 71.04747, 77.16823, 63.77144, 72.5249, 69.10145, 
                          74.61647, 55.0847, 70.97664, 73.40273, 72.02715, 69.28485, 68.66256, 
                          77.92079, 69.78192, 71.32363, 79.13777, 76.21347, 72.96919, 71.95923, 
                          70.94545, 64.8141, 55.98621, 74.19439, 72.70276, 68.77999, 63.09397, 
                          61.72898), Immigrants = c(57.3, 55.1, 50.6, 45.7, 42.8, 51.7, 
                                                    51.2, 50.9, 44.9, 44.5, 44.3, 42.7, 50.5, 50.5, 39.2, 50.6, 39.7, 
                                                    38.9, 39.2, 41.8, 42.5, 43.1, 39.5, 41.1, 44.2, 38.6, 41.8, 40.1, 
                                                    43.8, 41.9, 38.2, 38.9, 37.5, 40.8, 33.2, 41.6, 38.1, 30, 38.8, 
                                                    34.4, 36.5, 32.1, 41.3, 30.6, 32.9, 27.8, 35.4, 28.7, 37.1, 33.3, 
                                                    29.8, 29.8, 33.8, 32.8, 28.8, 32.6, 31.6, 30.7, 28.6, 30.9, 34.7, 
                                                    24.6, 24.7, 28.4, 26, 26.2, 27.4, 26.1, 22.6, 24.7, 32.4, 22.9, 
                                                    26.4, 22.2, 22.1), X.qual = c(32.9, 29.8, 30.8, 32.5, 18.3, 47.3, 
                                                                                  30.5, 29.8, 32.7, 38.5, 42.5, 25.8, 54.5, 52.2, 24.9, 29.3, 30.5, 
                                                                                  23, 37.6, 22.3, 35.2, 54, 39.6, 42.8, 30.4, 41.5, 47.5, 44.5, 
                                                                                  48.4, 31.3, 25.9, 28.2, 41.6, 46.5, 24.8, 36.3, 45.2, 27, 48.7, 
                                                                                  40, 42.1, 19.7, 53.7, 26, 21.8, 12.1, 51.6, 19.2, 46.6, 54.4, 
                                                                                  24.9, 30.1, 47.4, 51.4, 29.7, 57.4, 48.8, 47.6, 34.3, 22.8, 52, 
                                                                                  21.8, 29.6, 55.2, 38.6, 37.4, 39.3, 25.9, 15.7, 19.8, 38.2, 39.3, 
                                                                                  37.7, 18.3, 32.6)), class = "data.frame", row.names = c(NA, -75L
                                                                                  ))

interact <- lm(vt_c ~ Immigrants:X.qual, 
           data = ele)
interact_plot(model = interact, pred=immigrants, 
          modx =X.qual, data = ele)

Thank you!

  • Could you incude (a version of) the data needed to run your code? Doesn't need to be the full dataset, just enough to generate the error you're asking about. – user2554330 Jan 09 '22 at 12:38
  • Hi! thank you so much for answering, I have just updated the question with the code. – Lucia Thomas Jan 09 '22 at 16:30

1 Answers1

0

Welcome to SO, Lucia Thomas!

I read this message and it sounded so much more thorough than what usually write about reproducible questions:

Please make this question reproducible. This includes sample code you've attempted (including listing non-base R packages, and any errors/warnings received), sample unambiguous data (e.g., data.frame(x=...,y=...) or the output from dput(head(x))), and intended output given that input. Refs: stackoverflow.com/q/5963269minimal reproducible example, and stackoverflow.com/tags/r/info.

That being said, I think I can help. Right now you have called each variable as a vector and called a data frame in your call to lm(). This has led to an incompatibility issue between these two functions.

ele = structure(list(vt_c = c(68.37056, 67.55938, 69.25354, 67.54727, 
67.39343, 67.81161, 65.81312, 64.68675, 70.8572, 72.1439, 67.39006, 
64.89897, 62.81833, 63.82975, 58.99062, 67.69617, 68.17096, 65.24267, 
67.08106, 66.47592, 68.40781, 70.40636, 69.50657, 72.37613, 70.24236, 
67.50159, 71.77177, 67.09047, 74.58491, 70.64892, 65.20199, 70.03566, 
70.23142, 71.62487, 66.87982, 70.72528, 66.97507, 69.38713, 67.20061, 
68.79907, 67.05735, 67.38101, 66.10595, 60.97635, 61.9047, 61.28828, 
72.11577, 63.04311, 71.04747, 77.16823, 63.77144, 72.5249, 69.10145, 
74.61647, 55.0847, 70.97664, 73.40273, 72.02715, 69.28485, 68.66256, 
77.92079, 69.78192, 71.32363, 79.13777, 76.21347, 72.96919, 71.95923, 
70.94545, 64.8141, 55.98621, 74.19439, 72.70276, 68.77999, 63.09397, 
61.72898), immigrants = c(57.3, 55.1, 50.6, 45.7, 42.8, 51.7, 
51.2, 50.9, 44.9, 44.5, 44.3, 42.7, 50.5, 50.5, 39.2, 50.6, 39.7, 
38.9, 39.2, 41.8, 42.5, 43.1, 39.5, 41.1, 44.2, 38.6, 41.8, 40.1, 
43.8, 41.9, 38.2, 38.9, 37.5, 40.8, 33.2, 41.6, 38.1, 30, 38.8, 
34.4, 36.5, 32.1, 41.3, 30.6, 32.9, 27.8, 35.4, 28.7, 37.1, 33.3, 
29.8, 29.8, 33.8, 32.8, 28.8, 32.6, 31.6, 30.7, 28.6, 30.9, 34.7, 
24.6, 24.7, 28.4, 26, 26.2, 27.4, 26.1, 22.6, 24.7, 32.4, 22.9, 
26.4, 22.2, 22.1), X.qual = c(32.9, 29.8, 30.8, 32.5, 18.3, 47.3, 
30.5, 29.8, 32.7, 38.5, 42.5, 25.8, 54.5, 52.2, 24.9, 29.3, 30.5, 
23, 37.6, 22.3, 35.2, 54, 39.6, 42.8, 30.4, 41.5, 47.5, 44.5, 
48.4, 31.3, 25.9, 28.2, 41.6, 46.5, 24.8, 36.3, 45.2, 27, 48.7, 
40, 42.1, 19.7, 53.7, 26, 21.8, 12.1, 51.6, 19.2, 46.6, 54.4, 
24.9, 30.1, 47.4, 51.4, 29.7, 57.4, 48.8, 47.6, 34.3, 22.8, 52, 
21.8, 29.6, 55.2, 38.6, 37.4, 39.3, 25.9, 15.7, 19.8, 38.2, 39.3, 
37.7, 18.3, 32.6)), class = "data.frame", row.names = c(NA, -75L
))

Since you called the data frame, call the names of the columns, without the data frame appended:

interact <- lm(vt_c ~ immigrants:X.qual, 
               data = ele)
interact_plot(model = interact, pred=immigrants, 
              modx =X.qual, data = ele)

enter image description here

Kat
  • 15,669
  • 3
  • 18
  • 51
  • Hi @Kat, Thank you so much for your reply. This is my first time using SO, so I had no idea how to word the question. I have tried to improve it now. Removing the data frame seemed to work but there is a new error: 'Error in ecdf(d[[modx]]) : 'x' must have 1 or more non-missing values In addition: Warning message: immigrants and X.qual are not included in an interaction with one another in the model.' which I have put in the question. Do you have any idea how to fix this? – Lucia Thomas Jan 09 '22 at 20:21
  • It looks like you have Immigrants (capitalized) originally. However, when I built the data, I did not capitalize this object name. What do you have in the data frame? What do you have in the function? I'm guessing that's the problem. – Kat Jan 09 '22 at 22:28
  • Make sure the capitalization is the same in the data, the model, and the plot. – Kat Jan 09 '22 at 22:29
  • Oh yes that worked! Thank you so much for all your help and I'm sorry it was such a simple mistake - I am very new to this haha. – Lucia Thomas Jan 09 '22 at 23:45