[英]differences in heatmap/clustering defaults in R (heatplot versus heatmap.2)?
我比較R中樹狀圖,一個與創建熱圖的兩種方式made4
的heatplot
和一個與gplots
的heatmap.2
。 適當的結果取決於分析,但我試圖理解為什么默認值是如此不同,以及如何讓兩個函數給出相同的結果(或高度相似的結果),以便我理解所有'blackbox'參數進入這個。
這是示例數據和包:
require(gplots)
# made4 from bioconductor
require(made4)
data(khan)
data <- as.matrix(khan$train[1:30,])
使用heatmap.2對數據進行聚類得出:
heatmap.2(data, trace="none")
使用heatplot
給出:
heatplot(data)
最初的結果和縮放非常不同。 在這種情況下, heatplot
結果看起來更合理所以我想了解哪些參數可以提供給heatmap.2
以使它做同樣的事情,因為heatmap.2
具有我想要使用的其他優點/功能,因為我想要了解缺少的成分。
heatplot
使用與相關距離的平均鏈接,因此我們可以將其提供給heatmap.2
以確保使用類似的聚類(基於: https : heatmap.2
)
dist.pear <- function(x) as.dist(1-cor(t(x)))
hclust.ave <- function(x) hclust(x, method="average")
heatmap.2(data, trace="none", distfun=dist.pear, hclustfun=hclust.ave)
導致:
這使得行側樹狀圖看起來更相似但是列仍然不同,因此比例也是如此。 看起來, heatplot
按照某種方式按比例縮放列,默認情況下, heatmap.2
不會這樣做。 如果我向heatmap.2添加行縮放,我得到:
heatmap.2(data, trace="none", distfun=dist.pear, hclustfun=hclust.ave,scale="row")
它仍然不相同但更接近。 如何使用heatmap.2
重現heatplot
的結果? 有什么區別?
edit2 :似乎關鍵的區別在於, heatplot
使用以下行heatplot
調整數據:
if (dualScale) {
print(paste("Data (original) range: ", round(range(data),
2)[1], round(range(data), 2)[2]), sep = "")
data <- t(scale(t(data)))
print(paste("Data (scale) range: ", round(range(data),
2)[1], round(range(data), 2)[2]), sep = "")
data <- pmin(pmax(data, zlim[1]), zlim[2])
print(paste("Data scaled to range: ", round(range(data),
2)[1], round(range(data), 2)[2]), sep = "")
}
這是我試圖導入到我對heatmap.2
調用。 我喜歡它的原因是因為它使低值和高值之間的對比度更大,而只是將zlim
傳遞給heatmap.2
只是被忽略了。 如何在保留沿列的聚類的同時使用這種“雙重縮放”? 我想要的是增加對比度:
heatplot(..., dualScale=TRUE, scale="none")
與你得到的低對比度相比:
heatplot(..., dualScale=FALSE, scale="row")
有什么想法嗎?
heatmap.2
和heatplot
函數之間的主要區別如下:
heatmap.2,默認使用歐幾里德測度來獲得距離矩陣並完成聚類的聚集方法,而熱圖分別使用相關性和平均聚集方法。
heatmap.2計算距離矩陣並在縮放之前運行聚類算法,而熱圖(當dualScale=TRUE
)聚類已經縮放的數據。
heatmap.2重新排序基於行和列的平均值樹狀圖,描述在這里 。
通過提供自定義distfun
和hclustfun
參數,可以在heatmap.2中簡單地更改默認設置(第1頁)。 然而p。 在不更改源代碼的情況下,無法輕松解決2和3問題。 因此, heatplot
函數充當heatplot
的包裝器。 首先,它對數據進行必要的轉換,計算距離矩陣,對數據進行聚類,然后僅使用heatmap.2功能來繪制具有上述參數的熱圖。
heatplot函數中的dualScale=TRUE
參數僅適用於基於行的居中和縮放( 描述 )。 然后,它將縮放數據的極值( 描述 )重新分配給zlim
值:
z <- t(scale(t(data)))
zlim <- c(-3,3)
z <- pmin(pmax(z, zlim[1]), zlim[2])
為了匹配heatplot函數的輸出,我想提出兩個解決方案:
我 - 在源代碼中添加新功能-> heatmap.3
代碼可以在這里找到。 隨意瀏覽修訂版以查看對heatmap.2功能所做的更改。 總之,我介紹了以下選項:
scale=c("row","column")
zlim=c(-3,3)
reorder=FALSE
一個例子:
# require(gtools)
# require(RColorBrewer)
cols <- colorRampPalette(brewer.pal(10, "RdBu"))(256)
distCor <- function(x) as.dist(1-cor(t(x)))
hclustAvg <- function(x) hclust(x, method="average")
heatmap.3(data, trace="none", scale="row", zlim=c(-3,3), reorder=FALSE,
distfun=distCor, hclustfun=hclustAvg, col=rev(cols), symbreak=FALSE)
II - 定義一個為熱圖提供所有必需參數的heatmap.2
如果您更喜歡使用原始熱圖.2, zClust
函數(下面)將再現熱圖執行的所有步驟。 它提供(以列表格式)縮放數據矩陣,行和列樹形圖。 這些可以用作heatmap.2函數的輸入:
# depending on the analysis, the data can be centered and scaled by row or column.
# default parameters correspond to the ones in the heatplot function.
distCor <- function(x) as.dist(1-cor(x))
zClust <- function(x, scale="row", zlim=c(-3,3), method="average") {
if (scale=="row") z <- t(scale(t(x)))
if (scale=="col") z <- scale(x)
z <- pmin(pmax(z, zlim[1]), zlim[2])
hcl_row <- hclust(distCor(t(z)), method=method)
hcl_col <- hclust(distCor(z), method=method)
return(list(data=z, Rowv=as.dendrogram(hcl_row), Colv=as.dendrogram(hcl_col)))
}
z <- zClust(data)
# require(RColorBrewer)
cols <- colorRampPalette(brewer.pal(10, "RdBu"))(256)
heatmap.2(z$data, trace='none', col=rev(cols), Rowv=z$Rowv, Colv=z$Colv)
關於heatmap.2(3)
功能的其他評論很少:
symbreak=TRUE
。 它將調整色標,因此它在0左右突破。在當前示例中,負值=藍色,而正值=紅色。 col=bluered(256)
可以提供替代的着色解決方案,並且它不需要RColorBrewer庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.