NA值處理
數(shù)據(jù)NA值查看
# 查看是否有NA值
is.na(data)
na.fail(DF)
# 查看有多少NA值
length(data[is.na(data)])
# 顯示出NA數(shù)超過某百分比值的行,如20%
manyNAs(data , 0.2)
NA值處理
- 直接刪掉含NA的那行
na.omit(data)
- 用某值將其替換掉,如0
data[is.na(data)] <- 0
- 通過探索相關(guān)性將NA值填補(bǔ)上
cor(data , use = "complete.obs")
- the k-nearest neighbours
# 通過means, median, weighAvg
library(DMwR)
cleanData <- knnImputation(data, k = 10, scale = T,
meth = "weighAvg", distData = NULL)
# 用沒有缺失的值得中位數(shù)插入
cleanData <- centralImputation(data)
- impute any missing values in the training set using proximities
library(randomForest)
data.imputed <- rfImpute(variable1 ~ ., data = data, iter=6)
- missMDA :Impute the missing values of a dataset with the Principal Components Analysis model.
數(shù)據(jù)標(biāo)準(zhǔn)化
數(shù)據(jù)的標(biāo)準(zhǔn)化(normalization)是將數(shù)據(jù)按比例縮放,使之落入一個(gè)特定區(qū)間;目的是去除數(shù)據(jù)的單位限制,將其轉(zhuǎn)化為無量綱的純數(shù)值,便于不同單位或量級(jí)的指標(biāo)能夠進(jìn)行比較和加權(quán),在某些比較和評(píng)價(jià)的指標(biāo)處理中經(jīng)常會(huì)用到。其中最典型的就是數(shù)據(jù)的歸一化處理,即將數(shù)據(jù)統(tǒng)一映射到[0,1]區(qū)間上。
目前數(shù)據(jù)標(biāo)準(zhǔn)化方法有多種,歸結(jié)起來可以分為直線型方法(如極值法、標(biāo)準(zhǔn)差法)、折線型方法(如三折線法)、曲線型方法(如半正態(tài)性分布)。不同的標(biāo)準(zhǔn)化方法,對(duì)系統(tǒng)的評(píng)價(jià)結(jié)果會(huì)產(chǎn)生不同的影響,但在數(shù)據(jù)標(biāo)準(zhǔn)化方法的選擇上并沒有通用的法則。
數(shù)據(jù)標(biāo)準(zhǔn)化是不會(huì)改變?cè)瓟?shù)據(jù)的分布狀況的,否則相當(dāng)于原始數(shù)據(jù)發(fā)生了改變,那就修改了源數(shù)據(jù)了。(并不)
# 生成均勻分布數(shù)據(jù)
set.seed(13)
data <- runif(1000)
scale <- scale(data)
norm <- rnorm(1000)
ggplot() + geom_density(aes(x = data))
# scale(){base} 標(biāo)準(zhǔn)化
scale <- scale(data)
ggplot() + geom_density(aes(x = scale))
# min-max
min_max = (data-min(data))/(max(data)-min(data))
# qqplot 檢測(cè)分布變化
qqplot(data, min_max)
log <- log10(data)/log10(max(data))
qqplot(norm, log)
雖然分布改變了,但是也沒有完全正態(tài)化。
常用方法
atan函數(shù)轉(zhuǎn)換
-
min-max標(biāo)準(zhǔn)化(Min-Max Normalization)
也稱為離差標(biāo)準(zhǔn)化,是對(duì)原始數(shù)據(jù)的線性變換,使結(jié)果值映射到[0 - 1]之間。
X=(x-min)/(max-min)
其中max為樣本數(shù)據(jù)的最大值,min為樣本數(shù)據(jù)的最小值。
優(yōu)點(diǎn):所有的值都會(huì)是正值,會(huì)更加反映數(shù)據(jù)的意義。
缺陷:當(dāng)有新數(shù)據(jù)加入時(shí),可能導(dǎo)致max和min的變化,需要重新定義。
-
log 轉(zhuǎn)化
log2
log10
-
scale(){base}
x是數(shù)值矩陣,當(dāng)center=TRUE時(shí),為數(shù)據(jù)對(duì)象x按列進(jìn)行中心化;當(dāng)scale=TRUE時(shí),為數(shù)據(jù)對(duì)象x按列進(jìn)行標(biāo)準(zhǔn)化。
- 中心化是把每個(gè)數(shù)據(jù)減去均值;
- 標(biāo)準(zhǔn)化是在中心化后的數(shù)據(jù)基礎(chǔ)上再除以數(shù)據(jù)的標(biāo)準(zhǔn)差;
vegan 中數(shù)據(jù)標(biāo)準(zhǔn)化方法
-
decostand() {vegan}(x, method, MARGIN, range.global, na.rm = FALSE)
- total: 除以行和或列和 (default MARGIN = 1);
- max:除以行或列的最大值(default MARGIN = 2);
- freq:除以行或列的最大值,并乘以非零值的個(gè)數(shù)(default MARGIN=2);
- normalize:使行或列的平方和等于1 (default MARGIN = 1);
- range: 標(biāo)準(zhǔn)化使行或列的值在0 ... 1 (default MARGIN = 2).
- standardize:標(biāo)準(zhǔn)化使行或列的和為1且方差為1(default MARGIN = 2);
- pa: 將數(shù)據(jù)轉(zhuǎn)換為0、1數(shù)據(jù);
- chi.square: 除以行和及列和的平方根;
- hellinger: 采用total標(biāo)準(zhǔn)化以后再取平方根;
wisconsin() {vegan}:除以列最大值,再除以行和。
FactoMineR 內(nèi)置標(biāo)準(zhǔn)化參數(shù)
PCA(x, scale.unit = TRUE)