R语言之数据标准化方法大全

作者: Oodelay | 来源:发表于2019-04-21 12:54 被阅读53次

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. 除以和,转化后,加和为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))
    1. 除以最大值
decostand(dat,'max') %>% summary()
decostand(dat,'max',1) %>% summary()

    1. 均值为1
decostand(dat,'frequency') %>% colMeans()
decostand(dat,'frequency',1) %>% rowMeans()

    1. 平方和为1
decostand(dat,'normalize') %>% apply(1,function(x) sum(x^2))
decostand(dat,'normalize',2) %>% apply(2,function(x) sum(x^2))

'normalize'
  • 归一化为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()
'log'
  • 以排序序号替代具体数值
decostand(dat,'rank',2)
  • 转化为二进制0/1
decostand(dat,'pa') %>% summary()

相关文章

  • R语言之数据标准化方法大全

    decostand 是群落生态学中常用的工具包,提供了很多主流且高效的数据标准化方法。 基本语法 标准化,和转化相...

  • 2018-01-08

    "R语言学习笔记之数据标准化" 数据标准化/归一化 数据标准化是将数据按比例缩放,使之落入一个小的特定区间。在某些...

  • 《学习小组Day5笔记--寒鹤》

    R语言之数据结构 今天的学习内容是R语言的数据结构。R语言的数据结构主要有向量(vector),矩阵(matrix...

  • PCA分析

    1.数据标准化 为了统一数据的量纲并对数据进行中心化,在主成分分析之前往往需要对原始数据进行标准化。下面以R语言自...

  • Day2-R

    sthda R作图代码大全生信人20个R语言练习题一份精美答案 提纲: R数据结构 tidyverse, tid...

  • 数据标准化处理

    一、标准化的方法 1、Min-max 标准化 min-max标准化方法是对原始数据进行线性变换。设minA和max...

  • Day6:逻辑回归案例

    数据下载 一、数据预处理 导入库 导入数据 分割数据集 数据标准化 二、建立逻辑回归模型 R值(准确率): 0.8...

  • 数据分析:数据预处理--标准化初解释(一)

    数据归一化和标准化都是scaling,常用Normalization或Standardization表示。记录下R...

  • Mysql语法大全

    mysql操作手册大全 DDL操作数据库,表 一. 操作数据库:CRUD 1.c(create):创建 2.r(r...

  • 数据标准化方法:该如何选择?

    原文链接: 数据标准化方法:该如何选择? 什么是数据标准化? 在微生物组学数据分析之前,我们常常需要根据数据量纲的...

网友评论

    本文标题:R语言之数据标准化方法大全

    本文链接:https://www.haomeiwen.com/subject/jsndgqtx.html