美文网首页
PCA & tSNE

PCA & tSNE

作者: 茶馆先生的马褂 | 来源:发表于2020-04-25 20:15 被阅读0次

R语言中set.seed(),该命令的作用是设定生成随机数的种子,种子是为了让结果具有重复性。如果不设定种子,生成的随机数无法重现。

还是我济的镜像🐂嘛

更新R版本
更新R包

getwd()
set.seed(123456789)
library(ggfortify)
library(pheatmap)
library(Rtsne)
library(mvtnorm)
ng=500
nc=20
a1=rnorm(ng*nc);dim(a1)=c(ng,nc)
View(a1)
dim(a1)
a2=rnorm(ng*nc);dim(a2)=c(ng,nc)
a3=cbind(a1,a2)
paste0('cell_01_',1:nc)
paste0('cell_02_',1:nc)
colnames(a3)=c(paste0('cell_01_',1:nc),paste0('cell_02_',1:nc))
rownames(a3)=paste('gene_',1:ng,sep = '')
head(a3)
pheatmap(a3)
a3=t(a3);dim(a3)
pca_dat = prcomp(a3, scale. = TRUE) #prcomp主成分分析
p=autoplot(pca_dat) + theme_classic()+ggtitle('PCA plot')
plot(p)

set.seed(42)
tsne_out = Rtsne(a3, pca = F, perplexity = 10, theta = 0.0)
tsne=tsne_out$Y
colnames(tsne) = c('tSNE1','tSNE2')
ggplot(tsne, aes(x=tSNE1, y=tSNE2))+geom_point(aes)

R语言中根据hclust进行分群
详见http://wap.sciencenet.cn/blog-3334560-1150586.html
生成测试数据
产生0-1之间均匀分布Uniform Distribution的数值
x = runif(10)
y = runif(10)
得到2维的数组:按列合并
S = cbind(x,y)
赋予名称,便于识别分类:生成Name1-Name10的系列名赋予数组行名
rownames(S) = paste("Name",1:10,"")

数值计算距离

out.dist=dist(S,method="euclidean")
注释:在聚类中求两点的距离有:
1,绝对距离:manhattan
2,欧氏距离:euclidean 默认
3,闵科夫斯基距离:minkowski
4,切比雪夫距离:chebyshev
5,马氏距离:mahalanobis
6,蓝氏距离:canberra

根据距离聚类

out.hclust=hclust(out.dist,method="complete")
注释:聚类中集合之间的距离:
1,类平均法:average
2,重心法:centroid
3,中间距离法:median
4,最长距离法:complete 默认
5,最短距离法:single
6,离差平方和法:ward
7,密度估计法:density

聚类结果绘图

plclust(out.hclust)
添加聚类分类矩形,如分为3类
rect.hclust(out.hclust, k=3)

得到分为3类的数值
out.id = cutree(out.hclust, k=3)
out.id

以矩阵的方式分辨名称对应的类
table(out.id,paste("Name",1:10,""))

问题:

clusters were defined density-based spatial clustering of applications with noise (DBSCAN) clustering algorithm in R with an eps = 3.1

相关文章

网友评论

      本文标题:PCA & tSNE

      本文链接:https://www.haomeiwen.com/subject/qvjjwhtx.html