其實(shí)在上節(jié)課數(shù)量生態(tài)學(xué)筆記||數(shù)據(jù)探索中我們已經(jīng)簡單接觸了數(shù)據(jù)的轉(zhuǎn)化,我們?yōu)榱耸共煌瑒偭康臄?shù)據(jù)能在一起比較使用了標(biāo)準(zhǔn)化。在數(shù)據(jù)分析的過程中塔猾,經(jīng)常要根據(jù)分析的需要對數(shù)據(jù)進(jìn)行轉(zhuǎn)化环础,以符合模型的內(nèi)在需要。
要知道我們采集來的一手?jǐn)?shù)據(jù)有時會帶有我們的采集痕跡有時這種痕跡可能會掩蓋問題的實(shí)質(zhì)闻伶。我們常常出于以下幾方面的考慮需要對數(shù)據(jù)進(jìn)行轉(zhuǎn)化滨攻,轉(zhuǎn)化的方法也有很多,這里我們僅舉幾個例子虾攻。
- 使不同物理單位的數(shù)據(jù)具有可比性铡买,比如我們的環(huán)境因子數(shù)據(jù)。常采用歸一化霎箍,z-scores標(biāo)準(zhǔn)化奇钞。
- 使變量更符合正態(tài)分布和具有穩(wěn)定方差。常用平方根轉(zhuǎn)化漂坏,對數(shù)轉(zhuǎn)化等
- 改變對象變量的權(quán)重景埃,例如賦予所有對象向量相同的長度或者范數(shù)媒至。
- 將分類變量轉(zhuǎn)化為二元變量(1-0)或者對照碼。
對于我們的物種數(shù)據(jù)谷徙,通常具有相同的剛量拒啰,通常是正值和零,對這樣的數(shù)據(jù)幾種簡單的轉(zhuǎn)化函數(shù)完慧,可以降低極大值的影響:
- sqrt()平方根
- sqrt(sqrt())4次方根
- log1p()多度+ 1 的自然對數(shù)(保證0值轉(zhuǎn)化后仍為0)
- 某些特殊的情況需要把正值化為1 也就是有-無(1-0)
下面介紹vegan包的幾種轉(zhuǎn)化方式谋旦。
# 數(shù)據(jù)轉(zhuǎn)化和標(biāo)準(zhǔn)化
##################
#訪問decostand()幫助文件
?decostand
# 簡單轉(zhuǎn)化
# **********************
# 顯示原始數(shù)據(jù)某一部分(多度數(shù)據(jù))
spe[1:5, 2:4]
# 將多度數(shù)據(jù)轉(zhuǎn)化為有-無(1-0)數(shù)據(jù)
spe.pa <- decostand(spe, method="pa")
spe.pa[1:5, 2:4]
#物種水平:兩個方法;
#有-無數(shù)據(jù)或多度數(shù)據(jù)
# *******************
# 通過每個數(shù)值除以該物種最大值標(biāo)準(zhǔn)化多度
# 注意: 這里參數(shù)MARGIN=2 (默認(rèn)值)
spe.scal <- decostand(spe, "max")
spe.scal[1:5,2:4]
# 計算每列最大值
apply(spe.scal, 2, max)
#這些標(biāo)準(zhǔn)化過程是否正確運(yùn)行屈尼?最好利用繪圖函數(shù)或總結(jié)函數(shù)密切追蹤.册着。
這種追蹤是很有必要的,有時候我們懶得起名字脾歧,直接把處理前的名字賦給處理之后的數(shù)據(jù)甲捏,這樣我們再想重復(fù)上一步的操作就很困難。對于計算量大的數(shù)據(jù)更是如此鞭执,好不容易計算出來一個數(shù)據(jù)集司顿,重來的話又要計算好久。
#通過每個數(shù)值除以該物種總和標(biāo)準(zhǔn)化多度(每個物種的相對多度)
#注意: 這里需要設(shè)定參數(shù)MARGIN=2
spe.relsp <- decostand(spe, "total", MARGIN=2)
spe.relsp[1:5,2:4]
#計算標(biāo)準(zhǔn)化后數(shù)據(jù)每列總和
apply(spe.relsp, 2, sum)
# 樣方水平:3種方法兄纺;有-無數(shù)據(jù)或多度數(shù)據(jù)
# ***************************************
#通過每個數(shù)值除以該樣方總和標(biāo)準(zhǔn)化多度 (每個樣方相對多度或相對頻度)
#注意: 這里參數(shù)MARGIN=1 (默認(rèn)值)
spe.rel <- decostand(spe, "total") # 默認(rèn)MARGIN = 1
spe.rel[1:5,2:4]
#計算標(biāo)準(zhǔn)化后數(shù)據(jù)每列總和以檢驗標(biāo)準(zhǔn)化的過程是否正確
apply(spe.rel, 1, sum)
#賦予每個行向量長度(范數(shù))為1(即平方和為1).
spe.norm <- decostand(spe, "normalize")
spe.norm[1:5,2:4]
# 驗證每個行向量的范數(shù)
norm <- function(x) sqrt(x%*%x)
apply(spe.norm, 1, norm)
#這個轉(zhuǎn)化也稱為"弦轉(zhuǎn)化":如果用歐氏距離函數(shù)去計算弦轉(zhuǎn)化后的數(shù)據(jù)大溜,#將獲得弦距離矩陣(見第3章)。在PCA和RDA(見第5估脆、6章)及k-means
#聚類(見第4章)分析前通常需要對數(shù)據(jù)進(jìn)行弦轉(zhuǎn)化猎提。
# 計算相對頻度(樣方層面),然后取平方根
spe.hel <- decostand(spe, "hellinger")
spe.hel[1:5,2:4]
# 計算標(biāo)準(zhǔn)化后數(shù)據(jù)每行向量的范數(shù)
apply(spe.hel,1,norm)
#這個轉(zhuǎn)化也稱為Hellinger轉(zhuǎn)化旁蔼。如果用歐氏距離函數(shù)去計算Hellinger轉(zhuǎn)
#化后的數(shù)據(jù)锨苏,將獲得Hellinger距離矩陣(見第3章)。在PCA和RDA(見
#第5棺聊、6章)及k-means聚類(見第4章)分析前通常需要對數(shù)據(jù)進(jìn)行Hellinger
#轉(zhuǎn)化伞租。注意,Hellinger轉(zhuǎn)化等同于數(shù)據(jù)先平方根轉(zhuǎn)化后再進(jìn)行弦轉(zhuǎn)化限佩。
# 物種和樣方同時標(biāo)準(zhǔn)化
# ****************************
# 卡方轉(zhuǎn)化
spe.chi <- decostand(spe, "chi.square")
spe.chi[1:5,2:4]
# 請查看沒有物種的樣方8轉(zhuǎn)化后將會怎樣葵诈?
spe.chi[7:9,]
#如果用歐氏距離函數(shù)去計算卡方轉(zhuǎn)化后的數(shù)據(jù),將獲得卡方距離矩陣(見
#第3章)
# Wisconsin標(biāo)準(zhǔn)化:多度數(shù)據(jù)首先除以該物種最大值后再除以該樣方總和
spe.wis <- wisconsin(spe)
spe.wis[1:5,2:4]
# 常見種(石泥鰍 stone loach)轉(zhuǎn)化后的多度箱線圖
# *******************************************
par(mfrow=c(2,2))
boxplot(spe$LOC, sqrt(spe$LOC), log1p(spe$LOC),
las=1, main="簡單轉(zhuǎn)化",
names=c("原始數(shù)據(jù)", "sqrt", "log"), col="bisque")
boxplot(spe.scal$LOC, spe.relsp$LOC,
las=1, main="物種標(biāo)準(zhǔn)化",
names=c("max", "total"), col="lightgreen")
boxplot(spe.hel$LOC, spe.rel$LOC, spe.norm$LOC,
las=1, main="樣方標(biāo)準(zhǔn)化",
names=c("Hellinger", "total", "norm"), col="lightblue")
boxplot(spe.chi$LOC, spe.wis$LOC,
las=1, main="雙標(biāo)準(zhǔn)化",
names=c("Chi-square", "Wisconsin"), col="orange")
#比較多度數(shù)據(jù)轉(zhuǎn)化或標(biāo)準(zhǔn)化前后的數(shù)據(jù)分布范圍和分布情況祟同。
比較不同轉(zhuǎn)化對數(shù)據(jù)分布的影響作喘,理解物種水平的兩種方法,樣方水平的三種方法以及物種和樣方水平的兩種轉(zhuǎn)化方法晕城。
我們來看看物種數(shù)據(jù)轉(zhuǎn)化前后的沿河流變化情況泞坦。
#繪制物種從河流上游到下游分布圖
# ******************************
par(mfrow=c(2,2))
plot(env$das, spe$TRU, type="l", col=4, main="Raw data",
xlab="Distance from the source [km]", ylab="Raw abundance code")
lines(env$das, spe$OMB, col=3)
lines(env$das, spe$BAR, col="orange")
lines(env$das, spe$BCO, col=2)
lines(env$das, spe$LOC, col=1, lty="dotted")
plot(env$das, spe.scal$TRU, type="l", col=4, main="Species profiles (max)",
xlab="Distance from the source [km]", ylab="Standardized abundance")
lines(env$das, spe.scal$OMB, col=3)
lines(env$das, spe.scal$BAR, col="orange")
lines(env$das, spe.scal$BCO, col=2)
lines(env$das, spe.scal$LOC, col=1, lty="dotted")
plot(env$das, spe.hel$TRU, type="l", col=4,
main="Site profiles (Hellinger)",
xlab="Distance from the source [km]", ylab="Standardized abundance")
lines(env$das, spe.hel$OMB, col=3)
lines(env$das, spe.hel$BAR, col="orange")
lines(env$das, spe.hel$BCO, col=2)
lines(env$das, spe.hel$LOC, col=1, lty="dotted")
plot(env$das, spe.chi$TRU, type="l", col=4,
main="Double profiles (Chi-square)",
xlab="Distance from the source [km]", ylab="Standardized abundance")
lines(env$das, spe.chi$OMB, col=3)
lines(env$das, spe.chi$BAR, col="orange")
lines(env$das, spe.chi$BCO, col=2)
lines(env$das, spe.chi$LOC, col=1, lty="dotted")
legend("topright", c("Brown trout", "Grayling", "Barbel", "Common bream",
"Stone loach"), col=c(4,3,"orange",2,1), lty=c(rep(1,4),3))
#比較這些圖,并解釋它們的不同砖顷。