标准化矩阵矩阵的每一列

时间:2018-11-08 16:32:16

标签: r statistics standardized

我想知道是否可以在矩阵或数据帧上使用非标准化函数。我可以毫无疑问地在列上使用标准应用,但是不确定如何反转逻辑。这也已通过缩放功能完成。有什么建议吗?请参阅下面的reprex。

library(tibble)
library(cbar)
df <- tibble(
  x = rnorm(10, 5, .1), 
  y = rnorm(10, 50, 1),
  z = rnorm(10, 500, 20)
)
df
#> # A tibble: 10 x 3
#>        x     y     z
#>    <dbl> <dbl> <dbl>
#>  1  4.99  52.0  530.
#>  2  5.06  48.5  527.
#>  3  5.05  49.5  504.
#>  4  5.08  48.2  512.
#>  5  4.99  49.8  496.
#>  6  4.97  49.2  517.
#>  7  4.97  50.4  483.
#>  8  4.91  49.1  498.
#>  9  4.87  51.6  478.
#> 10  4.86  49.0  531.
scale(df)
#>                 x           y          z
#>  [1,]  0.16214478  1.79905676  1.1749160
#>  [2,]  1.10506079 -1.00798624  1.0366936
#>  [3,]  0.98384421 -0.21586991 -0.1861718
#>  [4,]  1.35024672 -1.18719295  0.2255299
#>  [5,]  0.21101616  0.07949959 -0.6128166
#>  [6,] -0.01764382 -0.40700291  0.4988026
#>  [7,] -0.02722854  0.57290776 -1.2813415
#>  [8,] -0.90900232 -0.51084700 -0.5015272
#>  [9,] -1.38320530  1.47438778 -1.5696862
#> [10,] -1.47523268 -0.59695288  1.2156013
#> attr(,"scaled:center")
#>          x          y          z 
#>   4.973533  49.725633 507.582204 
#> attr(,"scaled:scale")
#>           x           y           z 
#>  0.07522165  1.24812951 19.06825608
apply(df, 2, standardized)
#>                 x           y          z
#>  [1,]  0.16214478  1.79905676  1.1749160
#>  [2,]  1.10506079 -1.00798624  1.0366936
#>  [3,]  0.98384421 -0.21586991 -0.1861718
#>  [4,]  1.35024672 -1.18719295  0.2255299
#>  [5,]  0.21101616  0.07949959 -0.6128166
#>  [6,] -0.01764382 -0.40700291  0.4988026
#>  [7,] -0.02722854  0.57290776 -1.2813415
#>  [8,] -0.90900232 -0.51084700 -0.5015272
#>  [9,] -1.38320530  1.47438778 -1.5696862
#> [10,] -1.47523268 -0.59695288  1.2156013
apply(scale(df), 2, destandardized, y_mu = colMeans(df), y_sd = apply(df, 2, sd))
#> Warning in y_hat * y_sd: longer object length is not a multiple of shorter
#> object length
#> Warning in y_hat * y_sd + y_mu: longer object length is not a multiple of
#> shorter object length
#> Warning in y_hat * y_sd: longer object length is not a multiple of shorter
#> object length
#> Warning in y_hat * y_sd + y_mu: longer object length is not a multiple of
#> shorter object length
#> Warning in y_hat * y_sd: longer object length is not a multiple of shorter
#> object length
#> Warning in y_hat * y_sd + y_mu: longer object length is not a multiple of
#> shorter object length
#>                x          y          z
#>  [1,]   4.985730   5.108861   5.061912
#>  [2,]  51.104892  48.467535  51.019561
#>  [3,] 526.342398 503.465942 504.032232
#>  [4,]   5.075101   4.884231   4.990498
#>  [5,]  49.989008  49.824858  48.960758
#>  [6,] 507.245768 499.821369 517.093500
#>  [7,]   4.971485   5.016628   4.877149
#>  [8,]  48.591080  49.088029  49.099662
#>  [9,] 481.206892 535.696208 477.651026
#> [10,]   4.862564   4.928630   5.064973
Created on 2018-11-08 by the reprex package (v0.2.0).

0 个答案:

没有答案