我是靠谱客的博主 粗犷枕头,这篇文章主要介绍R语言做数据中心化和标准化scale函数手动实现,现在分享给大家,希望可以做个参考。

1. R中的方差和标准差

方差:var,是样本方差

var(y) instructs R to calculate the sample variance of Y. In other words it uses n-1 ‘degrees of freedom’, where n is the number of observations in Y.

标准差:sd,是样本标准差

var(y) instructs R to calculate the sample variance of Y. In other words it uses n-1 ‘degrees of freedom’, where n is the number of observations in Y.

varsd的关系

sd = sqrt(var),两者都是样本的结果。

sd(y) = sqrt(var(y)). In other words, this is the uncorrected sample standard deviation.

2. 手动计算方差和标准差

2.1 生成数据

生成数据:

复制代码
1
2
3
library(tidyverse) y = c(0.1,0.2,0.2,0.1,0.3,0.8,0.5)
复制代码
1
2
3
> y [1] 0.1 0.2 0.2 0.1 0.3 0.8 0.5

2.2 样本方差

公式计算:

复制代码
1
2
3
> var(y) [1] 0.0647619

手动计算:注意,分母是n-1

复制代码
1
2
3
> sum((y - mean(y))^2)/(length(y)-1) [1] 0.0647619

2.3 样本标准差

公式计算:

复制代码
1
2
3
4
> ## 样本标准差 > sd(y) [1] 0.2544836

手动计算:

复制代码
1
2
3
> sqrt(var(y)) [1] 0.2544836

或者:

复制代码
1
2
3
> sqrt(sum((y - mean(y))^2)/(length(y)-1)) [1] 0.2544836

2.4 总体方差

复制代码
1
2
3
4
> ## 总体方差 > sum((y - mean(y))^2)/length(y) [1] 0.0555102

或者:

复制代码
1
2
3
> mean((y-mean(y))^2) [1] 0.0555102

2.5 总体标准差

复制代码
1
2
3
4
> ## 总体标准差 > sqrt(sum((y - mean(y))^2)/length(y)) [1] 0.235606

或者:

复制代码
1
2
3
> sqrt(mean((y-mean(y))^2)) [1] 0.235606

3. 中心化和标准化

中心化和标准化意义一样,都是消除量纲的影响

  • 中心化:数据-均值
  • 标准化:(数据-均值)/标准差

3.1 R中的scale函数中心化和标准化

scale用法:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Description scale centers and/or scales the columns of a numeric table. Usage ## S4 method for signature 'db.obj' scale(x, center = TRUE, scale = TRUE) Arguments x A db.obj object. It represents a table/view in the database if it is an db.data.frame object, or a series of operations applied on an existing db.data.frame object if it is a db.Rquery object. center either a logical value or a numeric vector of length equal to the number of columns of 'x'. scale either a logical value or a numeric vector of length equal to the number of columns of 'x'.

参数解释:

  • center 为TRUE时,中心化,为FALSE不中心化
  • sacle 为TRUE时,为标准化,为FALSE不标准化

3.2 center=T,scale=T

scale默认的参数是center=T,scale=T

公式计算:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> ## 标准化: center =T, scale =T > scale(y,center = T,scale = T) [,1] [1,] -0.84204134 [2,] -0.44908871 [3,] -0.44908871 [4,] -0.84204134 [5,] -0.05613609 [6,] 1.90862703 [7,] 0.72976916 attr(,"scaled:center") [1] 0.3142857 attr(,"scaled:scale") [1] 0.2544836

手动计算:

复制代码
1
2
3
> (y - mean(y))/sd(y) [1] -0.84204134 -0.44908871 -0.44908871 -0.84204134 -0.05613609 1.90862703 0.72976916

3.3 center=T,scale=F

公式计算:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
> ## 标准化: center =T, scale =F > scale(y,center = T,scale = F) [,1] [1,] -0.21428571 [2,] -0.11428571 [3,] -0.11428571 [4,] -0.21428571 [5,] -0.01428571 [6,] 0.48571429 [7,] 0.18571429 attr(,"scaled:center") [1] 0.3142857

手动计算:

复制代码
1
2
3
> y - mean(y) [1] -0.21428571 -0.11428571 -0.11428571 -0.21428571 -0.01428571 0.48571429 0.18571429

3.4 center=F,scale=F

公式计算:

复制代码
1
2
3
4
5
6
7
8
9
10
11
> ## 标准化: center =F, scale =F > scale(y,center = F,scale = F) [,1] [1,] 0.1 [2,] 0.2 [3,] 0.2 [4,] 0.1 [5,] 0.3 [6,] 0.8 [7,] 0.5

手动计算:

复制代码
1
2
3
> y [1] 0.1 0.2 0.2 0.1 0.3 0.8 0.5

3.5 center=F,scale=T

公式计算:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
> ## 标准化: center =F, scale =T > scale(y,center = F,scale = T) [,1] [1,] 0.2357023 [2,] 0.4714045 [3,] 0.4714045 [4,] 0.2357023 [5,] 0.7071068 [6,] 1.8856181 [7,] 1.1785113 attr(,"scaled:scale") [1] 0.4242641

手动计算:

手动计算,不仅仅是分子变化了,分母也变化了,注意!

复制代码
1
2
3
> y/sqrt(sum(y^2)/(length(y)-1)) [1] 0.2357023 0.4714045 0.4714045 0.2357023 0.7071068 1.8856181 1.1785113

4. 果然,没有什么问题是写篇博客不能解决的

如果有,那就多写几篇。

欢迎关注我的公众号:育种数据分析之放飞自我。主要分享R语言,Python,育种数据分析,生物统计,数量遗传学,混合线性模型,GWAS和GS相关的知识。

最后

以上就是粗犷枕头最近收集整理的关于R语言做数据中心化和标准化scale函数手动实现的全部内容,更多相关R语言做数据中心化和标准化scale函数手动实现内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(73)

评论列表共有 0 条评论

立即
投稿
返回
顶部