using `scale` function inside `purrr` gives incorrect output

The confidence intervals for mlm objects are incorrect when used with purrr + scale workflow and also the resulting dataframes has mangled rownames.

Note below the values for disp:(Intercept) and disp:wt terms and the rownames:

set.seed(123)

confint(lm(cbind(mpg, disp) ~ wt, data = as.data.frame(scale(mtcars))))

#>                       2.5 %     97.5 %
#> mpg:(Intercept)  -0.1824544  0.1824544
#> mpg:wt           -1.0530332 -0.6822855
#> disp:(Intercept) -0.1687740  0.1687740
#> disp:wt           0.7165054  1.0594545

library(tidyverse)

confint(lm(
  formula = cbind(mpg, disp) ~ wt,
  data = modify_if(.x = mtcars, .p = is.numeric, .f = scale)
))

#>                   2.5 %     97.5 %
#> :(Intercept) -0.1824544  0.1824544
#> :wt          -1.0530332 -0.6822855
#> :(Intercept) -0.1824544  0.1824544
#> :wt          -1.0530332 -0.6822855

Created on 2020-02-22 by the reprex package (v0.3.0.9001)

Session info
sessioninfo::session_info()
#> - Session info ---------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.6.2 (2019-12-12)
#>  os       Windows 10 x64              
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  English_United States.1252  
#>  ctype    English_United States.1252  
#>  tz       Europe/Berlin               
#>  date     2020-02-22                  
#> 
#> - Packages -------------------------------------------------------------------
#>  package     * version    date       lib source                           
#>  assertthat    0.2.1      2019-03-21 [1] CRAN (R 3.6.0)                   
#>  backports     1.1.5      2019-10-02 [1] CRAN (R 3.6.1)                   
#>  broom         0.5.3.9000 2020-02-18 [1] local                            
#>  cellranger    1.1.0      2016-07-27 [1] CRAN (R 3.5.1)                   
#>  cli           2.0.1      2020-01-08 [1] CRAN (R 3.6.2)                   
#>  colorspace    1.4-1      2019-03-18 [1] CRAN (R 3.6.0)                   
#>  crayon        1.3.4      2017-09-16 [1] CRAN (R 3.5.1)                   
#>  DBI           1.1.0      2019-12-15 [1] CRAN (R 3.6.1)                   
#>  dbplyr        1.4.2      2019-06-17 [1] CRAN (R 3.6.0)                   
#>  digest        0.6.24     2020-02-12 [1] CRAN (R 3.6.2)                   
#>  dplyr       * 0.8.4      2020-01-31 [1] CRAN (R 3.6.2)                   
#>  evaluate      0.14       2019-05-28 [1] CRAN (R 3.6.0)                   
#>  fansi         0.4.1      2020-01-08 [1] CRAN (R 3.6.2)                   
#>  forcats     * 0.4.0      2019-02-17 [1] CRAN (R 3.5.2)                   
#>  fs            1.3.1      2019-05-06 [1] CRAN (R 3.6.0)                   
#>  generics      0.0.2      2019-03-05 [1] Github (r-lib/generics@c15ac43)  
#>  ggplot2     * 3.2.1      2019-08-10 [1] CRAN (R 3.6.2)                   
#>  glue          1.3.1      2019-03-12 [1] CRAN (R 3.6.0)                   
#>  gtable        0.3.0      2019-03-25 [1] CRAN (R 3.6.0)                   
#>  haven         2.2.0      2019-11-08 [1] CRAN (R 3.6.1)                   
#>  highr         0.8        2019-03-20 [1] CRAN (R 3.6.0)                   
#>  hms           0.5.3      2020-01-08 [1] CRAN (R 3.6.2)                   
#>  htmltools     0.4.0      2019-10-04 [1] CRAN (R 3.6.1)                   
#>  httr          1.4.1      2019-08-05 [1] CRAN (R 3.6.1)                   
#>  jsonlite      1.6.1      2020-02-02 [1] CRAN (R 3.6.2)                   
#>  knitr         1.28       2020-02-06 [1] CRAN (R 3.6.2)                   
#>  lazyeval      0.2.2      2019-03-15 [1] CRAN (R 3.6.0)                   
#>  lifecycle     0.1.0      2019-08-01 [1] CRAN (R 3.6.1)                   
#>  lubridate     1.7.4      2018-04-11 [1] CRAN (R 3.5.1)                   
#>  magrittr      1.5        2014-11-22 [1] CRAN (R 3.5.1)                   
#>  modelr        0.1.5      2019-08-08 [1] CRAN (R 3.6.1)                   
#>  munsell       0.5.0      2018-06-12 [1] CRAN (R 3.5.1)                   
#>  pillar        1.4.3      2019-12-20 [1] CRAN (R 3.6.2)                   
#>  pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 3.6.1)                   
#>  purrr       * 0.3.3      2019-10-18 [1] CRAN (R 3.6.1)                   
#>  R6            2.4.1      2019-11-12 [1] CRAN (R 3.6.1)                   
#>  Rcpp          1.0.3      2019-11-08 [1] CRAN (R 3.6.1)                   
#>  readr       * 1.3.1      2018-12-21 [1] CRAN (R 3.6.0)                   
#>  readxl        1.3.1      2019-03-13 [1] CRAN (R 3.6.0)                   
#>  reprex        0.3.0.9001 2020-01-25 [1] Github (tidyverse/reprex@e3fcadb)
#>  rlang         0.4.4      2020-01-28 [1] CRAN (R 3.6.2)                   
#>  rmarkdown     2.1        2020-01-20 [1] CRAN (R 3.6.2)                   
#>  rstudioapi    0.11       2020-02-07 [1] CRAN (R 3.6.2)                   
#>  rvest         0.3.5      2019-11-08 [1] CRAN (R 3.6.1)                   
#>  scales        1.1.0      2019-11-18 [1] CRAN (R 3.6.1)                   
#>  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 3.6.0)                   
#>  stringi       1.4.5      2020-01-11 [1] CRAN (R 3.6.2)                   
#>  stringr     * 1.4.0      2019-02-10 [1] CRAN (R 3.6.0)                   
#>  styler        1.3.1.9000 2020-02-19 [1] Github (r-lib/styler@06b4eae)    
#>  tibble      * 2.1.3      2019-06-06 [1] CRAN (R 3.6.2)                   
#>  tidyr       * 1.0.2      2020-01-24 [1] CRAN (R 3.6.2)                   
#>  tidyselect    1.0.0      2020-01-27 [1] CRAN (R 3.6.2)                   
#>  tidyverse   * 1.3.0      2019-11-21 [1] CRAN (R 3.6.1)                   
#>  vctrs         0.2.3      2020-02-20 [1] CRAN (R 3.6.2)                   
#>  withr         2.1.2      2018-03-15 [1] CRAN (R 3.5.1)                   
#>  xfun          0.12       2020-01-13 [1] CRAN (R 3.6.2)                   
#>  xml2          1.2.2      2019-08-09 [1] CRAN (R 3.6.1)                   
#>  yaml          2.2.1      2020-02-01 [1] CRAN (R 3.6.2)                   
#> 
#> [1] C:/Users/inp099/Documents/R/win-library/3.6
#> [2] C:/Program Files/R/R-3.6.2/library

Is this a bug or this is not the correct way to use scale with purrr?

P.S. Discovered this here.

Does StupidWolf's answer on S/O answer your question?

Hi @IndrajeetPatil. The problem is not a bug. It is because the modify_if keep the attribute from the scale function in each column. And the inner function model.frame of lm cannot correctly extract the name of mpg and disp, which may due to the attribute remained. So, remove the attribute in modify_if with as.numeric will make lm function normally.

set.seed(123)

x <- as.data.frame(scale(mtcars))

confint(lm(cbind(mpg, disp) ~ wt, data = x))
#>                       2.5 %     97.5 %
#> mpg:(Intercept)  -0.1824544  0.1824544
#> mpg:wt           -1.0530332 -0.6822855
#> disp:(Intercept) -0.1687740  0.1687740
#> disp:wt           0.7165054  1.0594545

library(tidyverse)

y <- modify_if(.x = mtcars, .p = is.numeric, ~{scale(.x)})

confint(lm(
  formula = cbind(mpg, disp) ~ wt,
  data = y
))
#>                   2.5 %     97.5 %
#> :(Intercept) -0.1824544  0.1824544
#> :wt          -1.0530332 -0.6822855
#> :(Intercept) -0.1824544  0.1824544
#> :wt          -1.0530332 -0.6822855

str(x)
#> 'data.frame':    32 obs. of  11 variables:
#>  $ mpg : num  0.151 0.151 0.45 0.217 -0.231 ...
#>  $ cyl : num  -0.105 -0.105 -1.225 -0.105 1.015 ...
#>  $ disp: num  -0.571 -0.571 -0.99 0.22 1.043 ...
#>  $ hp  : num  -0.535 -0.535 -0.783 -0.535 0.413 ...
#>  $ drat: num  0.568 0.568 0.474 -0.966 -0.835 ...
#>  $ wt  : num  -0.6104 -0.3498 -0.917 -0.0023 0.2277 ...
#>  $ qsec: num  -0.777 -0.464 0.426 0.89 -0.464 ...
#>  $ vs  : num  -0.868 -0.868 1.116 1.116 -0.868 ...
#>  $ am  : num  1.19 1.19 1.19 -0.814 -0.814 ...
#>  $ gear: num  0.424 0.424 0.424 -0.932 -0.932 ...
#>  $ carb: num  0.735 0.735 -1.122 -1.122 -0.503 ...

str(y)
#> 'data.frame':    32 obs. of  11 variables:
#>  $ mpg : num [1:32, 1] 0.151 0.151 0.45 0.217 -0.231 ...
#>   ..- attr(*, "scaled:center")= num 20.1
#>   ..- attr(*, "scaled:scale")= num 6.03
#>  $ cyl : num [1:32, 1] -0.105 -0.105 -1.225 -0.105 1.015 ...
#>   ..- attr(*, "scaled:center")= num 6.19
#>   ..- attr(*, "scaled:scale")= num 1.79
#>  $ disp: num [1:32, 1] -0.571 -0.571 -0.99 0.22 1.043 ...
#>   ..- attr(*, "scaled:center")= num 231
#>   ..- attr(*, "scaled:scale")= num 124
#>  $ hp  : num [1:32, 1] -0.535 -0.535 -0.783 -0.535 0.413 ...
#>   ..- attr(*, "scaled:center")= num 147
#>   ..- attr(*, "scaled:scale")= num 68.6
#>  $ drat: num [1:32, 1] 0.568 0.568 0.474 -0.966 -0.835 ...
#>   ..- attr(*, "scaled:center")= num 3.6
#>   ..- attr(*, "scaled:scale")= num 0.535
#>  $ wt  : num [1:32, 1] -0.6104 -0.3498 -0.917 -0.0023 0.2277 ...
#>   ..- attr(*, "scaled:center")= num 3.22
#>   ..- attr(*, "scaled:scale")= num 0.978
#>  $ qsec: num [1:32, 1] -0.777 -0.464 0.426 0.89 -0.464 ...
#>   ..- attr(*, "scaled:center")= num 17.8
#>   ..- attr(*, "scaled:scale")= num 1.79
#>  $ vs  : num [1:32, 1] -0.868 -0.868 1.116 1.116 -0.868 ...
#>   ..- attr(*, "scaled:center")= num 0.438
#>   ..- attr(*, "scaled:scale")= num 0.504
#>  $ am  : num [1:32, 1] 1.19 1.19 1.19 -0.814 -0.814 ...
#>   ..- attr(*, "scaled:center")= num 0.406
#>   ..- attr(*, "scaled:scale")= num 0.499
#>  $ gear: num [1:32, 1] 0.424 0.424 0.424 -0.932 -0.932 ...
#>   ..- attr(*, "scaled:center")= num 3.69
#>   ..- attr(*, "scaled:scale")= num 0.738
#>  $ carb: num [1:32, 1] 0.735 0.735 -1.122 -1.122 -0.503 ...
#>   ..- attr(*, "scaled:center")= num 2.81
#>   ..- attr(*, "scaled:scale")= num 1.62

model.frame(cbind(mpg, disp) ~ wt, x)
#>                     cbind(mpg, disp).mpg cbind(mpg, disp).disp           wt
#> Mazda RX4                     0.15088482           -0.57061982 -0.610399567
#> Mazda RX4 Wag                 0.15088482           -0.57061982 -0.349785269
#> Datsun 710                    0.44954345           -0.99018209 -0.917004624
#> Hornet 4 Drive                0.21725341            0.22009369 -0.002299538
#> Hornet Sportabout            -0.23073453            1.04308123  0.227654255
#> Valiant                      -0.33028740           -0.04616698  0.248094592
#> Duster 360                   -0.96078893            1.04308123  0.360516446
#> Merc 240D                     0.71501778           -0.67793094 -0.027849959
#> Merc 230                      0.44954345           -0.72553512 -0.068730634
#> Merc 280                     -0.14777380           -0.50929918  0.227654255
#> Merc 280C                    -0.38006384           -0.50929918  0.227654255
#> Merc 450SE                   -0.61235388            0.36371309  0.871524874
#> Merc 450SL                   -0.46302456            0.36371309  0.524039143
#> Merc 450SLC                  -0.81145962            0.36371309  0.575139986
#> Cadillac Fleetwood           -1.60788262            1.94675381  2.077504765
#> Lincoln Continental          -1.60788262            1.84993175  2.255335698
#> Chrysler Imperial            -0.89442035            1.68856165  2.174596366
#> Fiat 128                      2.04238943           -1.22658929 -1.039646647
#> Honda Civic                   1.71054652           -1.25079481 -1.637526508
#> Toyota Corolla                2.29127162           -1.28790993 -1.412682800
#> Toyota Corona                 0.23384555           -0.89255318 -0.768812180
#> Dodge Challenger             -0.76168319            0.70420401  0.309415603
#> AMC Javelin                  -0.81145962            0.59124494  0.222544170
#> Camaro Z28                   -1.12671039            0.96239618  0.636460997
#> Pontiac Firebird             -0.14777380            1.36582144  0.641571082
#> Fiat X1-9                     1.19619000           -1.22416874 -1.310481114
#> Porsche 914-2                 0.98049211           -0.89093948 -1.100967659
#> Lotus Europa                  1.71054652           -1.09426581 -1.741772228
#> Ford Pantera L               -0.71190675            0.97046468 -0.048290296
#> Ferrari Dino                 -0.06481307           -0.69164740 -0.457097039
#> Maserati Bora                -0.84464392            0.56703942  0.360516446
#> Volvo 142E                    0.21725341           -0.88529152 -0.446876870

model.frame(cbind(mpg, disp) ~ wt, y)
#>                     cbind(mpg, disp).1 cbind(mpg, disp).2           wt
#> Mazda RX4                   0.15088482        -0.57061982 -0.610399567
#> Mazda RX4 Wag               0.15088482        -0.57061982 -0.349785269
#> Datsun 710                  0.44954345        -0.99018209 -0.917004624
#> Hornet 4 Drive              0.21725341         0.22009369 -0.002299538
#> Hornet Sportabout          -0.23073453         1.04308123  0.227654255
#> Valiant                    -0.33028740        -0.04616698  0.248094592
#> Duster 360                 -0.96078893         1.04308123  0.360516446
#> Merc 240D                   0.71501778        -0.67793094 -0.027849959
#> Merc 230                    0.44954345        -0.72553512 -0.068730634
#> Merc 280                   -0.14777380        -0.50929918  0.227654255
#> Merc 280C                  -0.38006384        -0.50929918  0.227654255
#> Merc 450SE                 -0.61235388         0.36371309  0.871524874
#> Merc 450SL                 -0.46302456         0.36371309  0.524039143
#> Merc 450SLC                -0.81145962         0.36371309  0.575139986
#> Cadillac Fleetwood         -1.60788262         1.94675381  2.077504765
#> Lincoln Continental        -1.60788262         1.84993175  2.255335698
#> Chrysler Imperial          -0.89442035         1.68856165  2.174596366
#> Fiat 128                    2.04238943        -1.22658929 -1.039646647
#> Honda Civic                 1.71054652        -1.25079481 -1.637526508
#> Toyota Corolla              2.29127162        -1.28790993 -1.412682800
#> Toyota Corona               0.23384555        -0.89255318 -0.768812180
#> Dodge Challenger           -0.76168319         0.70420401  0.309415603
#> AMC Javelin                -0.81145962         0.59124494  0.222544170
#> Camaro Z28                 -1.12671039         0.96239618  0.636460997
#> Pontiac Firebird           -0.14777380         1.36582144  0.641571082
#> Fiat X1-9                   1.19619000        -1.22416874 -1.310481114
#> Porsche 914-2               0.98049211        -0.89093948 -1.100967659
#> Lotus Europa                1.71054652        -1.09426581 -1.741772228
#> Ford Pantera L             -0.71190675         0.97046468 -0.048290296
#> Ferrari Dino               -0.06481307        -0.69164740 -0.457097039
#> Maserati Bora              -0.84464392         0.56703942  0.360516446
#> Volvo 142E                  0.21725341        -0.88529152 -0.446876870

z <- modify_if(.x = mtcars, .p = is.numeric, ~{as.numeric(scale(.x))})

confint(lm(
  formula = cbind(mpg, disp) ~ wt,
  data = z
))
#>                       2.5 %     97.5 %
#> mpg:(Intercept)  -0.1824544  0.1824544
#> mpg:wt           -1.0530332 -0.6822855
#> disp:(Intercept) -0.1687740  0.1687740
#> disp:wt           0.7165054  1.0594545

Created on 2020-02-23 by the reprex package (v0.3.0)

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.