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)