scale() 函数
1. 用法
scale(x, center = TRUE, scale = TRUE)
备注: 之前一直听别人说scale(标准化)是除以什么根方差 ,不太懂,所以自己就验证了一下。 结论1: 在同时使用center =T 和 scale =T 的情况下,scale用的值是方差 结论2: 在只使用center =F 和 scale =T, scale用的值是根方差
sqrt(sum(x^2)/(n-1))
2.举列
只中心化,不标准化的验证:
attach(mtcars)
a = mtcars[1:5,]$gear
> a
[1] 4 4 4 3 3
> mean(a)
[1] 3.6
> sd(a)
[1] 0.5477226
> scale(a,center = T,scale = F)
[,1]
[1,] 0.4
[2,] 0.4
[3,] 0.4
[4,] -0.6
[5,] -0.6
attr(,"scaled:center")
[1] 3.6
> a - mean(a)
[1] 0.4 0.4 0.4 -0.6 -0.6
只标准化, 不中心化的验证:
> scale(a,center = F,scale = T)
[,1]
[1,] 0.9847319
[2,] 0.9847319
[3,] 0.9847319
[4,] 0.7385489
[5,] 0.7385489
attr(,"scaled:scale")
[1] 4.062019
> a/4.062019
[1] 0.984732 0.984732 0.984732 0.738549 0.738549
> scale_num = sqrt(sum(a^2)/(length(a)-1))
> scale_num
[1] 4.062019
既标准化,也中心化的验证:
> scale(a,center = T,scale = T)
[,1]
[1,] 0.7302967
[2,] 0.7302967
[3,] 0.7302967
[4,] -1.0954451
[5,] -1.0954451
attr(,"scaled:center")
[1] 3.6
attr(,"scaled:scale")
[1] 0.5477226
> (a - mean(a))/sd(a)
[1] 0.7302967 0.7302967 0.7302967 -1.0954451 -1.0954451
|