R语言数据降维

今天是2020年的最后一天,这一年多灾多难,让我们唏嘘不已。不论2020如何,生活仍要继续,希望即将到来的2021年,可以‘牛’转乾坤。
这也是2020年的最后一篇博客了,在这里给大家介绍一下。目前R语言的几种降维方式。
首先需要配置数据

data<-matrix(rnorm(3000),ncol=6)
colnames(data)=paste0('gene',1:6)
rownames(data)=c(paste0(rep('C',nrow(data)/2),1:(nrow(data)/2)),
                 paste0(rep('P',nrow(data)/2),1:(nrow(data)/2)))
head(data)

在这里插入图片描述1、PCA降维

data.pca <- prcomp(t(data), scale. = TRUE)
head(data.pca$rotation)

在这里插入图片描述

pca_data=data.pca$rotation[,1:2]
pca_data=data.frame(sample=rownames(pca_data),
                    pca_data,
                    group=c(rep('C',nrow(data)/2),rep('P',nrow(data)/2)))
head(pca_data)

在这里插入图片描述

#绘图
library(ggplot2)
ggplot(data=pca_data,aes(x=PC1,y=PC2))+
  geom_point(aes(colour=group,shape=group),size=2)+
  theme(text=element_text(size=10),legend.title=element_blank(),
        panel.background = element_rect(fill = "white", colour = "black",size = 0.2), 
        legend.key = element_rect(fill = "white", colour = "white"),
        legend.background = (element_rect(colour= "white",fill = "white")))

在这里插入图片描述2、非度量MDS降维

dis_data = dist(data,'euclidean',p=2)
mds_x = cmdscale(dis_data,k=2)
mds_x = data.frame(mds_x)
colnames(mds_x)=c('MDS.1','MDS.2')
head(mds_x)

在这里插入图片描述

mds_data=data.frame(sample=rownames(mds_x),
                    mds_x,
                    group=c(rep('C',nrow(data)/2),rep('P',nrow(data)/2)))
head(mds_data)

在这里插入图片描述

library(ggplot2)
ggplot(data=mds_data,aes(x=MDS.1,y=MDS.2))+
  geom_point(aes(colour=group,shape=group),size=2)+
  theme(text=element_text(size=10),legend.title=element_blank(),
        panel.background = element_rect(fill = "white", colour = "black",size = 0.2), 
        legend.key = element_rect(fill = "white", colour = "white"),
        legend.background = (element_rect(colour= "white",fill = "white")))

在这里插入图片描述3、umap降维

library(umap)
data.umap = umap(data)

umap_data=data.umap$layout
colnames(umap_data)=c('umap_1','umap_2')
head(umap_data)

在这里插入图片描述

umap_data=data.frame(sample=rownames(umap_data),
                    umap_data,
                    group=c(rep('C',nrow(data)/2),rep('P',nrow(data)/2)))
head(umap_data)

在这里插入图片描述

library(ggplot2)
ggplot(data=umap_data,aes(x=umap_1,y=umap_2))+
  geom_point(aes(colour=group,shape=group),size=2)+
  theme(text=element_text(size=10),legend.title=element_blank(),
        panel.background = element_rect(fill = "white", colour = "black",size = 0.2), 
        legend.key = element_rect(fill = "white", colour = "white"),
        legend.background = (element_rect(colour= "white",fill = "white")))

在这里插入图片描述4、tsne降维
#迭代次数 max_iter
#困惑度perplexity
#权衡速度与准确度,越小越精确,越大速度越快 theta

library(Rtsne)
#设置随机种子
set.seed(1500)
tsne_result <- Rtsne(data,
                     initial_dims = ncol(data),
                     pca = FALSE,
                     dims = 2,
                     check_duplicates = FALSE,
                     perplexity=30,
                     max_iter=2500,
                     theta=0.5)$Y
colnames(tsne_result)=c('tsne_1','tsne_2')
head(tsne_result)

在这里插入图片描述

tsne_data=data.frame(sample=rownames(data),
                     tsne_result,
                     group=c(rep('C',nrow(data)/2),rep('P',nrow(data)/2)))
head(tsne_data)

在这里插入图片描述

library(ggplot2)
ggplot(data=tsne_data,aes(x=tsne_1,y=tsne_2))+
  geom_point(aes(colour=group,shape=group),size=2)+
  theme(text=element_text(size=10),legend.title=element_blank(),
        panel.background = element_rect(fill = "white", colour = "black",size = 0.2), 
        legend.key = element_rect(fill = "white", colour = "white"),
        legend.background = (element_rect(colour= "white",fill = "white")))

在这里插入图片描述

  • 6
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值