rsquaredacademy / olsrr

Tools for developing OLS regression models
https://olsrr.rsquaredacademy.com/
Other
102 stars 22 forks source link

mixlm interferes with olsrr::ols_vif_tol and olsrr::ols_mallows_cp #204

Closed jtheog closed 1 year ago

jtheog commented 1 year ago

Please briefly describe your problem and what output you expect. If you have a question, please don't use this form. Instead, ask on https://stackoverflow.com/.

Please include a minimal reproducible example (AKA a reprex). If you've never heard of a reprex before, start by reading https://www.tidyverse.org/help/#reprex.


Brief description of the problem When olsrr and mixlm packages are loaded together, ols_vif_tol() and ols_mallows_cp() do not recognize linear models as linear models.

The situation below throws the following error msg: "Hmmm.. Looks like you have specified the wrong model. Follow the below steps to debug this error... ..."

library(olsrr)
library(mixlm)

mod01 <- lm(mpg ~ disp + hp + wt, data = mtcars)
mod02 <- lm(mpg ~ disp + hp, data = mtcars)

ols_vif_tol(mod01)
ols_mallows_cp(mod02, mod01)

Whereas the situation below without the mixlm package loaded is successful. Note: if you still have the mixlm package attached from the previous situation, you must un-attach it to get the olsrr functions to work.

library(olsrr)
#library(mixlm)

mod01 <- lm(mpg ~ disp + hp + wt, data = mtcars)
mod02 <- lm(mpg ~ disp + hp, data = mtcars)

ols_vif_tol(mod01)
ols_mallows_cp(mod02, mod01)
aravindhebbali commented 1 year ago

Thanks @jtheog for filing this issue. We have pushed a fix to the development branch.

# Install development version from GitHub
# install.packages("devtools")
devtools::install_github("rsquaredacademy/olsrr@develop")

The error was the result of a bug in olsrr checking if the supplied model belonged to the lm class. The bug has been fixed. Let us know if you face any other issues.

library(olsrr)
#> 
#> Attaching package: 'olsrr'
#> The following object is masked from 'package:datasets':
#> 
#>     rivers
library(mixlm)
#> 
#> Attaching package: 'mixlm'
#> The following objects are masked from 'package:stats':
#> 
#>     glm, lm

mod01 <- lm(mpg ~ disp + hp + wt, data = mtcars)
mod02 <- lm(mpg ~ disp + hp, data = mtcars)

ols_vif_tol(mod01)
#>   Variables Tolerance      VIF
#> 1      disp 0.1365278 7.324517
#> 2        hp 0.3654126 2.736633
#> 3        wt 0.2064146 4.844618
ols_mallows_cp(mod02, mod01)
#> [1] 14.7087

Created on 2022-12-19 by the reprex package (v0.3.0)