R实战| PCA、tSNE、UMAP三种降维方法在R中的实现
生活随笔
收集整理的這篇文章主要介紹了
R实战| PCA、tSNE、UMAP三种降维方法在R中的实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
降維
木舟筆記矩陣
在組學分析中,一般通過降維算法得到低緯度如二維或三維的新坐標數據,再結合可視化技術去展示樣本的在新坐標的空間分布,接著加上統計檢驗結果證實整體組學水平上組間的差異性。降維算法有基于線性模型的PCA,也有基于非線性的tSNE和UMAP等方法。
示例數據和代碼領取
詳見:R實戰| PCA、tSNE、UMAP三種降維方法在R中的實現
PCA
主成分分析(Principal Component Analysis,PCA)是最常用的無監督學習方法。
rm(list?=?ls()) library(tidyverse) library(broom) library(palmerpenguins)#?示例數據 penguins?<-?penguins?%>%drop_na()?%>%select(-year) head(penguins)#?使用prcomp()進行PCA #?PCA前對數值型數據進行標準化 pca_fit?<-?penguins?%>%select(where(is.numeric))?%>%scale()?%>%prcomp() #?查看成分重要性 summary(pca_fit)#?可視化PC1和PC2 pca_fit?%>%augment(penguins)?%>%rename_at(vars(starts_with(".fitted")),list(~str_replace(.,".fitted","")))?%>%ggplot(aes(x=PC1,?y=PC2,color=species,shape=sex))+geom_point() PCA plot: PC1 vs PC2UMAP
數據預處理
##?UMAP rm(list?=?ls()) library(tidyverse) library(palmerpenguins) #install.packages("umap") library(umap) theme_set(theme_bw(18))penguins?<-?penguins?%>%?drop_na()?%>%select(-year)%>%mutate(ID=row_number())?penguins_meta?<-?penguins?%>%select(ID,?species,?island,?sex)使用umap包進行umap分析
set.seed(142) umap_fit?<-?penguins?%>%select(where(is.numeric))?%>%column_to_rownames("ID")?%>%scale()?%>%?umap()umap_df?<-?umap_fit$layout?%>%as.data.frame()%>%rename(UMAP1="V1",UMAP2="V2")?%>%mutate(ID=row_number())%>%inner_join(penguins_meta,?by="ID") umap_df?%>%?head()可視化
#?可視化 umap_df?%>%ggplot(aes(x?=?UMAP1,?y?=?UMAP2,?color?=?species,shape?=?sex))+geom_point()+labs(x?=?"UMAP1",y?=?"UMAP2",subtitle?=?"UMAP?plot") UMAP plot in R: Example 1 #?分面 umap_df?%>%ggplot(aes(x?=?UMAP1,?y?=?UMAP2,color?=?species))?+geom_point(size=3,?alpha=0.5)+facet_wrap(~island)+labs(x?=?"UMAP1",y?=?"UMAP2",subtitle="UMAP?plot")+theme(legend.position="bottom") UMAP plot in R: Example 2 #?圈出異常樣本 library(ggforce) umap_df?%>%ggplot(aes(x?=?UMAP1,y?=?UMAP2,?color?=?species,shape?=?sex))?+geom_point()?+labs(x?=?"UMAP1",y?=?"UMAP2",subtitle="UMAP?plot")?+geom_circle(aes(x0?=?-5,?y0?=?-3.8,?r?=?0.65),?color?=?"green",inherit.aes?=?FALSE) UMAP Plot to Identify Potential sample mix-upstSNE
數據預處理
##?tSNE rm(list?=?ls()) library(tidyverse) library(palmerpenguins) library(Rtsne) theme_set(theme_bw(18))penguins?<-?penguins?%>%?drop_na()?%>%select(-year)%>%mutate(ID=row_number())penguins_meta?<-?penguins?%>%select(ID,species,island,sex)使用Rtsne 包進行tSNE 分析
set.seed(142) tSNE_fit?<-?penguins?%>%select(where(is.numeric))?%>%column_to_rownames("ID")?%>%scale()?%>%?Rtsne()tSNE_df?<-?tSNE_fit$Y?%>%?as.data.frame()?%>%rename(tSNE1="V1",tSNE2="V2")?%>%mutate(ID=row_number())tSNE_df?<-?tSNE_df?%>%inner_join(penguins_meta,?by="ID")tSNE_df?%>%?head()可視化
tSNE_df?%>%ggplot(aes(x?=?tSNE1,?y?=?tSNE2,color?=?species,shape?=?sex))+geom_point()+theme(legend.position="bottom") tSNE Plot參考
How To Make tSNE plot in R - Data Viz with Python and R (datavizpyr.com)
How to make UMAP plot in R - Data Viz with Python and R (datavizpyr.com)
How To Make PCA Plot with R - Data Viz with Python and R (datavizpyr.com)
往期內容
CNS圖表復現|生信分析|R繪圖 資源分享&討論群!
組學生信| Front Immunol |基于血清蛋白質組早期診斷標志篩選的簡單套路
木舟筆記矩陣
總結
以上是生活随笔為你收集整理的R实战| PCA、tSNE、UMAP三种降维方法在R中的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 联想服务器TS130主板芯片组,【Thi
- 下一篇: Win7小工具“美女报时”,搜道版,v1