easystats / see

:art: Visualisation toolbox for beautiful and publication-ready figures
https://easystats.github.io/see/
Other
891 stars 45 forks source link

Error when plotting check_collinearity() with polynomial terms #346

Open jmgirard opened 4 months ago

jmgirard commented 4 months ago
library(easystats)
#> # Attaching packages: easystats 0.7.2 (red = needs update)
#> ✔ bayestestR  0.13.2   ✔ correlation 0.8.4 
#> ✔ datawizard  0.11.0   ✔ effectsize  0.8.8 
#> ✖ insight     0.20.0   ✖ modelbased  0.8.7 
#> ✔ performance 0.12.0   ✔ parameters  0.21.7
#> ✔ report      0.5.8    ✔ see         0.8.4 
#> 
#> Restart the R-Session and update packages with `easystats::easystats_update()`.

x <- runif(n = 100, min = 100, max = 110)
y <- 0.2 + 1.2 * x + rnorm(100, 0, 1)
df <- data.frame(x, y)

Using I()

fit <- lm(y ~ x + I(x^2), data = df)
(vif <- check_collinearity(fit))
#> # Check for Multicollinearity
#> 
#> High Correlation
#> 
#>    Term     VIF         VIF 95% CI Increased SE Tolerance Tolerance 95% CI
#>       x 6318.52 [4361.06, 9154.78]        79.49  1.58e-04     [0.00, 0.00]
#>  I(x^2) 6318.52 [4361.06, 9154.78]        79.49  1.58e-04     [0.00, 0.00]
plot(vif)
#> Variable `Component` is not in your data frame :/

Using poly(..., raw = TRUE)

fit2 <- lm(y ~ poly(x, degree = 2, raw = TRUE), data = df)
(vif2 <- check_collinearity(fit2))
#> Not enough model terms in the conditional part of the model to check for
#>   multicollinearity.
#> NULL
plot(vif2)
#> Warning in min(x): no non-missing arguments to min; returning Inf
#> Warning in max(x): no non-missing arguments to max; returning -Inf
#> Warning in min(x): no non-missing arguments to min; returning Inf
#> Warning in max(x): no non-missing arguments to max; returning -Inf
#> Error in plot.window(...): need finite 'xlim' values

Using poly(...)

fit3 <- lm(y ~ poly(x, degree = 2), data = df)
(vif3 <- check_collinearity(fit3))
#> Not enough model terms in the conditional part of the model to check for
#>   multicollinearity.
#> NULL
plot(vif3)
#> Warning in min(x): no non-missing arguments to min; returning Inf
#> Warning in min(x): no non-missing arguments to max; returning -Inf
#> Warning in min(x): no non-missing arguments to min; returning Inf
#> Warning in max(x): no non-missing arguments to max; returning -Inf
#> Error in plot.window(...): need finite 'xlim' values

Created on 2024-06-12 with reprex v2.1.0