decostand
是群落生态学中常用的工具包,提供了很多主流且高效的数据标准化方法。
基本语法
decostand (x,method, MARGIN, range.global,logbase = 2, na.rm = FALSE,...)
标准化,和转化相反,是求相对值,旨在降低数据之间因量级、单位等差异而带来的数据异质性。
示例
(dat = matrix(sample(seq(100)),nrow = 20,dimnames = list(paste0('OTU_',seq(20)),paste0('smp',seq(5)))))
library('vegan')
library('dplyr')
- 除以和,转化后,加和为1
decostand(dat,'total') %>% rowSums()
decostand(dat,'total',2) %>% colSums()
- 1.1 其他方法可实现对列除以和,并使列和为1
t(t(df)/colSums(df))
dat/matrix(rep(colSums(dat),nrow(dat)), nrow = nrow(dat), byrow = T)
sweep(dat,2,colSums(dat),`/`)
scale(dat, center=FALSE, scale=colSums(dat))
- 除以最大值
decostand(dat,'max') %>% summary()
decostand(dat,'max',1) %>% summary()
- 均值为1
decostand(dat,'frequency') %>% colMeans()
decostand(dat,'frequency',1) %>% rowMeans()
- 平方和为1
decostand(dat,'normalize') %>% apply(1,function(x) sum(x^2))
decostand(dat,'normalize',2) %>% apply(2,function(x) sum(x^2))
- 归一化为0~1
decostand(dat,'range') %>% summary() #apply(dat, 2, function (x) (max(x)-x)/(max(x)-min(x)))
decostand(dat,'range',1) %>% summary() #apply(dat, 1, function (x) (max(x)-x)/(max(x)-min(x)))
- z-score转化,均值为0,方差为1
decostand(dat, 'standardize') %>% summary()
decostand(dat, 'standardize',1) %>% summary()
- chi.square 卡方,先每行差异行和,再每列除以列和平方根,最后除以矩阵和的平方根
decostand(dat,'chi.square')
# (dat / rowSums(dat)) %*% diag(1/sqrt(colSums(dat))) * sqrt(sum(dat))
- log
decostand(dat,'log') %>% summary()
- 以排序序号替代具体数值
decostand(dat,'rank',2)
- 转化为二进制0/1
decostand(dat,'pa') %>% summary()