Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interact_plot errors with scale-ed variables #33

Closed
mattansb opened this issue Mar 13, 2020 · 4 comments
Closed

interact_plot errors with scale-ed variables #33

mattansb opened this issue Mar 13, 2020 · 4 comments

Comments

@mattansb
Copy link

library(interactions)

iris$Sepal.Length_c <- scale(iris$Sepal.Length, center = TRUE, scale = FALSE)
iris$Sepal.Width_c <- scale(iris$Sepal.Width, center = TRUE, scale = FALSE)

fit <- lm(Petal.Length ~ Sepal.Length_c * Sepal.Width_c, iris)

interact_plot(fit, pred = Sepal.Length_c, modx = Sepal.Width_c)
#> Error: variables 'Sepal.Length_c', 'Sepal.Width_c' were specified with different types from the fit

However this doesn't happen with sim_slopes or johnson_neyman.

sim_slopes(fit, pred = Sepal.Length_c, modx = Sepal.Width_c)
#> JOHNSON-NEYMAN INTERVAL 
#> 
#> When Sepal.Width_c is OUTSIDE the interval [-10.36, -2.05], the slope of
#> Sepal.Length_c is p < .05.
#> 
#> Note: The range of observed values of Sepal.Width_c is [-1.06, 1.34]
#> 
#> SIMPLE SLOPES ANALYSIS 
#> 
#> Slope of Sepal.Length_c when Sepal.Width_c = -0.44 (- 1 SD): 
#> 
#>   Est.   S.E.   t val.      p
#> ------ ------ -------- ------
#>   1.53   0.10    14.78   0.00
#> 
#> Slope of Sepal.Length_c when Sepal.Width_c =  0.00 (Mean): 
#> 
#>   Est.   S.E.   t val.      p
#> ------ ------ -------- ------
#>   1.75   0.06    27.64   0.00
#> 
#> Slope of Sepal.Length_c when Sepal.Width_c =  0.44 (+ 1 SD): 
#> 
#>   Est.   S.E.   t val.      p
#> ------ ------ -------- ------
#>   1.97   0.09    21.75   0.00

johnson_neyman(fit, pred = Sepal.Length_c, modx = Sepal.Width_c)
#> JOHNSON-NEYMAN INTERVAL 
#> 
#> When Sepal.Width_c is OUTSIDE the interval [-10.36, -2.05], the slope of
#> Sepal.Length_c is p < .05.
#> 
#> Note: The range of observed values of Sepal.Width_c is [-1.06, 1.34]

Created on 2020-03-13 by the reprex package (v0.3.0)

@jacob-long
Copy link
Owner

So it seems the source of this problem is that scale() returns a matrix, which is not recognized by these plotting functions since R otherwise lets me treat these one-dimensional matrices as if they are numeric vectors. But when I try to use the predict() function, R complains because it wants me to give it a matrix.

This is such an odd case that I'm not sure if it's worth me trying to anticipate and fix at this juncture.

A quick fix is to just do some simple coercion like this:

library(interactions)

iris$Sepal.Length_c <- as.numeric(scale(iris$Sepal.Length, center = TRUE, scale = FALSE))
iris$Sepal.Width_c <- as.numeric(scale(iris$Sepal.Width, center = TRUE, scale = FALSE))

fit <- lm(Petal.Length ~ Sepal.Length_c * Sepal.Width_c, iris)

interact_plot(fit, pred = Sepal.Length_c, modx = Sepal.Width_c)

@mattansb
Copy link
Author

This is such an odd case that I'm not sure if it's worth me trying to anticipate and fix at this juncture.

I'm afraid that odd as it is, it is also common to center variables, especially in moderation analyses (especially in the social sciences). One of those things that just "caught on" and cannot be untaught :/

@jacob-long
Copy link
Owner

Well @mattansb I think you've been proven right as I've gotten at least two emails about this same problem since you created this issue.

As it turns out, fixing this isn't as complicated as I first expected. The source of the problem was in my other package, jtools, and will be fixed with its next release (coming imminently).

To some extent, this is all just a quirk of how R automatically treats a one-dimensional matrix of numbers and a vector of numbers as if they are the same sometimes but not other times. Now my internal functions just do a check to see if what looks like a numeric variable is actually a one-dimensional matrix created by scale().

@mattansb
Copy link
Author

Thanks Jacob!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants