Skip to content

tidypredict_fit errors on use of : in models with stepwise dropped terms #74

@bengowan

Description

@bengowan

I noticed tidypredict_fit is fine with multiple interactions in formulas, but for some reason gives errors for : in isolated x:y interactions of models that have dropped terms.

Here is a reprex using a fully specified interaction formula on mtcars, stepped down with stats::step. tidypredict_fit is fine withthe full model, which includes :  terms, but errors on the stepped down model for some reason.  

Thanks!

# fit a model with interaction terms, tidypredict is fine with * and : formulat functions
cars_lm <- lm(mpg ~ disp * cyl * hp * gear, data = mtcars)

cars_lm
#> 
#> Call:
#> lm(formula = mpg ~ disp * cyl * hp * gear, data = mtcars)
#> 
#> Coefficients:
#>      (Intercept)              disp               cyl                hp  
#>        3.165e+02         2.463e-01        -9.459e+01        -2.305e+00  
#>             gear          disp:cyl           disp:hp            cyl:hp  
#>       -7.426e+01         1.779e-01         1.059e-03         5.829e-01  
#>        disp:gear          cyl:gear           hp:gear       disp:cyl:hp  
#>       -5.569e-02         2.545e+01         5.652e-01        -1.153e-03  
#>    disp:cyl:gear      disp:hp:gear       cyl:hp:gear  disp:cyl:hp:gear  
#>       -5.381e-02        -2.973e-04        -1.534e-01         3.298e-04

tidypredict::tidypredict_fit(cars_lm)
#> 316.520309196644 + (disp * 0.246313794518502) + (cyl * -94.5872871981695) + 
#>     (hp * -2.30500650482876) + (gear * -74.2574697779736) + (disp * 
#>     cyl * 0.177946383535088) + (disp * hp * 0.00105867448489592) + 
#>     (cyl * hp * 0.582885559233175) + (disp * gear * -0.0556906400658316) + 
#>     (cyl * gear * 25.4498752936947) + (hp * gear * 0.565209740210809) + 
#>     (disp * cyl * hp * -0.00115279922544789) + (disp * cyl * 
#>     gear * -0.0538061321226812) + (disp * hp * gear * -0.000297318088672666) + 
#>     (cyl * hp * gear * -0.15344456894624) + (disp * cyl * hp * 
#>     gear * 0.000329762929171967)

# drop certain items from interactions, tidypredict now erros on the use of :  
cars_drop <- step(cars_lm, direction = 'backward', trace = 0)

cars_drop
#> 
#> Call:
#> lm(formula = mpg ~ disp + cyl + hp + gear + disp:hp + cyl:hp + 
#>     disp:gear + cyl:gear + hp:gear + disp:hp:gear + cyl:hp:gear, 
#>     data = mtcars)
#> 
#> Coefficients:
#>  (Intercept)          disp           cyl            hp          gear  
#>    84.708839      1.207121    -52.778801     -0.457745     -8.625052  
#>      disp:hp        cyl:hp     disp:gear      cyl:gear       hp:gear  
#>    -0.005828      0.277657     -0.333912     13.056098      0.065701  
#> disp:hp:gear   cyl:hp:gear  
#>     0.001572     -0.066664

#but errors here
tidypredict::tidypredict_fit(cars_drop)
#> Error: Functions inside the formula are not supported.
#> - Functions detected: `:`. Use `dplyr` transformations to prepare the data.

sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.0.0 (2020-04-24)
#>  os       macOS Catalina 10.15.5      
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/Los_Angeles         
#>  date     2020-06-18                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date       lib source                            
#>  assertthat    0.2.1      2019-03-21 [1] CRAN (R 4.0.0)                    
#>  cli           2.0.2      2020-02-28 [1] CRAN (R 4.0.0)                    
#>  crayon        1.3.4      2017-09-16 [1] CRAN (R 4.0.0)                    
#>  digest        0.6.25     2020-02-23 [1] CRAN (R 4.0.0)                    
#>  dplyr         1.0.0      2020-05-29 [1] CRAN (R 4.0.0)                    
#>  ellipsis      0.3.1      2020-05-15 [1] CRAN (R 4.0.0)                    
#>  evaluate      0.14       2019-05-28 [1] CRAN (R 4.0.0)                    
#>  fansi         0.4.1      2020-01-08 [1] CRAN (R 4.0.0)                    
#>  generics      0.0.2      2018-11-29 [1] CRAN (R 4.0.0)                    
#>  glue          1.4.1      2020-05-13 [1] CRAN (R 4.0.0)                    
#>  highr         0.8        2019-03-20 [1] CRAN (R 4.0.0)                    
#>  htmltools     0.4.0.9003 2020-06-08 [1] Github (rstudio/htmltools@984b39c)
#>  knitr         1.28       2020-02-06 [1] CRAN (R 4.0.0)                    
#>  lifecycle     0.2.0      2020-03-06 [1] CRAN (R 4.0.0)                    
#>  magrittr      1.5        2014-11-22 [1] CRAN (R 4.0.0)                    
#>  pillar        1.4.4      2020-05-05 [1] CRAN (R 4.0.0)                    
#>  pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.0.0)                    
#>  purrr         0.3.4      2020-04-17 [1] CRAN (R 4.0.0)                    
#>  R6            2.4.1      2019-11-12 [1] CRAN (R 4.0.0)                    
#>  rlang         0.4.6      2020-05-02 [1] CRAN (R 4.0.0)                    
#>  rmarkdown     2.2        2020-05-31 [1] CRAN (R 4.0.0)                    
#>  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 4.0.0)                    
#>  stringi       1.4.6      2020-02-17 [1] CRAN (R 4.0.0)                    
#>  stringr       1.4.0      2019-02-10 [1] CRAN (R 4.0.0)                    
#>  tibble        3.0.1      2020-04-20 [1] CRAN (R 4.0.0)                    
#>  tidypredict   0.4.5      2020-02-10 [1] CRAN (R 4.0.0)                    
#>  tidyselect    1.1.0      2020-05-11 [1] CRAN (R 4.0.0)                    
#>  vctrs         0.3.0      2020-05-11 [1] CRAN (R 4.0.0)                    
#>  withr         2.2.0      2020-04-20 [1] CRAN (R 4.0.0)                    
#>  xfun          0.14       2020-05-20 [1] CRAN (R 4.0.0)                    
#>  yaml          2.2.1      2020-02-01 [1] CRAN (R 4.0.0)                    
#> 
#> [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library

Created on 2020-06-18 by the reprex package (v0.3.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions