||
「方差: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.
❞
「var
和sd
的关系」
sd = sqrt(var),两者都是样本的结果。
❝sd(y) = sqrt(var(y)). In other words, this is the uncorrected sample standard deviation.
❞
「生成数据:」
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
「公式计算:」
> var(y) [1] 0.0647619
「手动计算:注意,分母是n-1」
> sum((y - mean(y))^2)/(length(y)-1) [1] 0.0647619
「公式计算:」
> ## 样本标准差 > sd(y) [1] 0.2544836
「手动计算:」
> sqrt(var(y)) [1] 0.2544836
或者:
> sqrt(sum((y - mean(y))^2)/(length(y)-1)) [1] 0.2544836
> ## 总体方差 > sum((y - mean(y))^2)/length(y) [1] 0.0555102
或者:
> mean((y-mean(y))^2) [1] 0.0555102
> ## 总体标准差 > sqrt(sum((y - mean(y))^2)/length(y)) [1] 0.235606
或者:
> sqrt(mean((y-mean(y))^2)) [1] 0.235606
中心化和标准化意义一样,都是消除量纲的影响
「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'.
「参数解释:」
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
「公式计算:」
> ## 标准化: 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
「公式计算:」
> ## 标准化: 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
「公式计算:」
> ## 标准化: 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
如果有,那就多写几篇。
❝欢迎关注我的公众号:
❞育种数据分析之放飞自我
。主要分享R语言,Python,育种数据分析,生物统计,数量遗传学,混合线性模型,GWAS和GS相关的知识。
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-12-27 19:56
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社