育种数据分析之放飞自我分享 http://blog.sciencenet.cn/u/yijiaobai 关注:生物统计,数量遗传,混合线性模型,生物信息,R,Perl,Python,GWAS,GS相关方法,文章及代码

博文

通过代码理解中心化和标准化

已有 2152 次阅读 2021-6-15 22:45 |个人分类:农学统计|系统分类:科研笔记

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 生成数据

「生成数据:」

library(tidyverse)
y = c(0.1,0.2,0.2,0.1,0.3,0.8,0.5)
> y
[1] 0.1 0.2 0.2 0.1 0.3 0.8 0.5

2.2 样本方差

「公式计算:」

> var(y)
[1] 0.0647619

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

> sum((y - mean(y))^2)/(length(y)-1)
[1] 0.0647619

2.3 样本标准差

「公式计算:」

> ## 样本标准差
> sd(y)
[1] 0.2544836

「手动计算:」

> sqrt(var(y))
[1] 0.2544836

或者:

> sqrt(sum((y - mean(y))^2)/(length(y)-1))
[1] 0.2544836

2.4 总体方差

> ## 总体方差
> sum((y - mean(y))^2)/length(y)
[1] 0.0555102

或者:

> mean((y-mean(y))^2)
[1] 0.0555102

2.5 总体标准差

> ## 总体标准差
> sqrt(sum((y - mean(y))^2)/length(y))
[1] 0.235606

或者:

> sqrt(mean((y-mean(y))^2))
[1] 0.235606

3. 中心化和标准化

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

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

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

「scale用法:」

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

「公式计算:」

> ## 标准化: 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

「手动计算:」

> (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

「公式计算:」

> ## 标准化: 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

「手动计算:」

> y - mean(y)
[1] -0.21428571 -0.11428571 -0.11428571 -0.21428571 -0.01428571  0.48571429  0.18571429

3.4 center=F,scale=F

「公式计算:」

> ## 标准化: 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

「手动计算:」

> y
[1] 0.1 0.2 0.2 0.1 0.3 0.8 0.5

3.5 center=F,scale=T

「公式计算:」

> ## 标准化: 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

「手动计算:」

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

> 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相关的知识。




https://wap.sciencenet.cn/blog-2577109-1291350.html

上一篇:亲缘关系推断软件:king
下一篇:tidyverse之汇总统计
收藏 IP: 223.90.189.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-19 23:18

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部