rsquaredacademy / olsrr

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

all possible regression fails when model formula contains inline functions or interaction variables #4

Closed aravindhebbali closed 7 years ago

aravindhebbali commented 7 years ago

ols_all_subset() returns an error when the formula in the model contains inline functions or interaction variables.

library(caret)
data("Sacramento")
> lm_fit2 <- lm(price  ~ beds + baths + log(sqft), data = Sacramento)
> ols_all_subset(lm_fit2)
Error in eval(predvars, data, env) : object 'sqft' not found
Called from: eval(predvars, data, env)

# interaction variables
> lm_fit3 <- lm(mpg ~ disp + hp + wt + am * disp, data = mtcars)
> ols_all_subset(lm_fit3)
Error in ols_mallows_cp(out$model, model) : 
  model must be a subset of full model
Called from: ols_mallows_cp(out$model, model)

Session Info

> sessionInfo()
R version 3.4.0 (2017-04-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_India.1252  LC_CTYPE=English_India.1252   
[3] LC_MONETARY=English_India.1252 LC_NUMERIC=C                  
[5] LC_TIME=English_India.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] purrr_0.2.2     dplyr_0.5.0     caret_6.0-76    ggplot2_2.2.1  
[5] lattice_0.20-35

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.7        magrittr_1.5       splines_3.4.0     
 [4] MASS_7.3-47        munsell_0.4.3      colorspace_1.2-7  
 [7] R6_2.2.1           foreach_1.4.3      minqa_1.2.4       
[10] stringr_1.1.0      car_2.1-2          plyr_1.8.4        
[13] tools_3.4.0        parallel_3.4.0     nnet_7.3-12       
[16] pbkrtest_0.4-6     grid_3.4.0         gtable_0.2.0      
[19] nlme_3.1-125       mgcv_1.8-17        quantreg_5.19     
[22] DBI_0.5-1          MatrixModels_0.4-1 iterators_1.0.8   
[25] lme4_1.1-11        lazyeval_0.2.0     assertthat_0.2.0  
[28] tibble_1.2         Matrix_1.2-9       nloptr_1.0.4      
[31] reshape2_1.4.2     ModelMetrics_1.1.0 codetools_0.2-15  
[34] stringi_1.1.2      compiler_3.4.0     scales_0.4.1      
[37] stats4_3.4.0       SparseM_1.7       
aravindhebbali commented 7 years ago

ols_all_subset() now works when model formula contains inline functions or interaction variables.

> library(olsrr)
> library(caret)
> data("Sacramento")

> lm_fit2 <- lm(price  ~ beds + baths + log(sqft), data = Sacramento)
> ols_all_subset(lm_fit2)
  Index N           Predictors R-Square Adj. R-Square Mallow's Cp
1     1 1            log(sqft)    0.568         0.567     52.6943
2     2 1                baths    0.331         0.331    589.5143
3     3 1                 beds    0.215         0.214    854.1735
4     4 2       beds log(sqft)    0.591          0.59      2.9559
5     5 2      baths log(sqft)    0.568         0.567     54.5318
6     6 2           beds baths    0.344         0.343    562.4425
7     7 3 beds baths log(sqft)    0.591          0.59           4

# interaction variables
> lm_fit3 <- lm(mpg ~ disp + hp + wt + am * disp, data = mtcars)
> ols_all_subset(lm_fit3)
   Index N            Predictors R-Square Adj. R-Square Mallow's Cp
1      1 1                    wt    0.753         0.745     15.7814
2      2 1                  disp    0.718         0.709     21.8906
3      3 1                    hp    0.602         0.589     42.4214
4      4 1                    am     0.36         0.338     85.4006
5      5 1               disp:am    0.025        -0.008    144.7555
6      6 2                 hp wt    0.827         0.815       4.682
7      7 2                 hp am    0.782         0.767     12.6088
8      8 2               disp wt    0.781         0.766     12.8044
9      9 2            wt disp:am    0.779         0.764     13.1616
10    10 2                 wt am    0.753         0.736     17.7811
11    11 2               disp hp    0.748         0.731     18.5949
12    12 2               disp am    0.733         0.715     21.2358
13    13 2            hp disp:am    0.723         0.703     23.1505
14    14 2          disp disp:am    0.719         0.699     23.8295
15    15 2            am disp:am    0.642         0.618     37.3666
16    16 3              hp wt am     0.84         0.823      4.3607
17    17 3         wt am disp:am     0.83         0.812      6.0674
18    18 3            disp hp wt    0.827         0.808       6.673
19    19 3         hp wt disp:am    0.827         0.809       6.573
20    20 3            disp hp am    0.799         0.778     11.5672
21    21 3       disp wt disp:am    0.796         0.774     12.1161
22    22 3       disp am disp:am     0.79         0.767     13.2175
23    23 3         hp am disp:am    0.789         0.767     13.2901
24    24 3            disp wt am    0.781         0.758     14.7845
25    25 3       disp hp disp:am    0.764         0.739     17.7208
26    26 4      hp wt am disp:am    0.853         0.831      4.0081
27    27 4         disp hp wt am     0.84         0.817      6.3004
28    28 4    disp wt am disp:am    0.836         0.812      6.9704
29    29 4    disp hp wt disp:am    0.827         0.802      8.5727
30    30 4    disp hp am disp:am    0.811         0.783     11.5263
31    31 5 disp hp wt am disp:am    0.853         0.825           6