R语言进行PCoA分析

Python013

R语言进行PCoA分析,第1张

#PCoA 分析在R语言中进行主要依赖于以下得包,进行这个分析得主要可以应用于形态学数据得相似与差异性分析。

library(ade4)

library(ggplot2)

library(RColorBrewer)

library(vegan)

这里我们使用R自带得数据iris

data(iris)

在R语言中通常都会使用这个数据进行案例分析

#iris

data(iris)

iris

data01<-iris[,-5]#数据预处理,去掉最后一列得数据标签

data01

dis01<-vegdist(data01,method = "euclidean")#这里是为了算矩阵距离,方法根据数据选择合适得方法

dis01

pcoa1<- dudi.pco(dis01, scan = FALSE,nf=3)#进行PCoA分析

pcoa1

pcoa1_eig<-pcoa1$eig[1:2]/sum(pcoa1$eig)#算一下前两列对整个数据得解释比例

pcoa1_eig

samplesite1<-data.frame({pcoa1$li})[1:2]#将前两列的数据分析结果放到sample_site1里面

sample_site1

sample_site1$names<-rownames(sample_site1)#设置名称

sample_site1$names

iris$Species

sample_site1$level<-factor(iris$Species,levels = c("setosa","versicolor","virginica"))#设置level的标签

sample_site1$level

names(sample_site1)[1:2]<-c("PCoA1","PCoA2")

p<-ggplot(sample_site1, mapping=aes(PCoA1, PCoA2,color=level))+theme_classic()

p<-p+geom_point()#绘制散点图

p

1、K最近邻(k-NearestNeighbor,KNN)分类算法,是一个理论上比较成熟的方法,也是最简单的机器学习算法之一。该方法的思路是:如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的样本中的大多数属于某一个类别,则该样本也属于这个类别。

2、KNN算法中,所选择的邻居都是已经正确分类的对象。该方法在定类决策上只依据最邻近的一个或者几个样本的类别来决定待分样本所属的类别。 KNN方法虽然从原理上也依赖于极限定理,但在类别决策时,只与极少量的相邻样本有关。由于KNN方法主要靠周围有限的邻近的样本,而不是靠判别类域的方法来确定所属类别的,因此对于类域的交叉或重叠较多的待分样本集来说,KNN方法较其他方法更为适合。

3、KNN算法不仅可以用于分类,还可以用于回归。通过找出一个样本的k个最近邻居,将这些邻居的属性的平均值赋给该样本,就可以得到该样本的属性。更有用的方法是将不同距离的邻居对该样本产生的影响给予不同的权值(weight),如权值与距离成正比。

简言之,就是将未标记的案例归类为与它们最近相似的、带有标记的案例所在的类 。

原理及举例

工作原理:我们知道样本集中每一个数据与所属分类的对应关系,输入没有标签的新数据后,将新数据与训练集的数据对应特征进行比较,找出“距离”最近的k(通常k<20)数据,选择这k个数据中出现最多的分类作为新数据的分类。

算法描述

1、计算已知数据集中的点与当前点的距离

2、按距离递增次序排序

3、选取与当前数据点距离最近的K个点

4、确定前K个点所在类别出现的频率

5、返回频率最高的类别作为当前类别的预测

距离计算方法有"euclidean"(欧氏距离),”minkowski”(明科夫斯基距离), "maximum"(切比雪夫距离), "manhattan"(绝对值距离),"canberra"(兰式距离), 或 "minkowski"(马氏距离)等

Usage

knn(train, test, cl, k = 1, l = 0, prob =FALSE, use.all = TRUE)

Arguments

train

matrix or data frame of training set cases.

test

matrix or data frame of test set cases. A vector will  be interpreted as a row vector for a single case.

cl

factor of true classifications of training set

k

number of neighbours considered.

l

minimum vote for definite decision, otherwisedoubt. (More precisely, less thank-ldissenting votes are allowed, even

ifkis  increased by ties.)

prob

If this is true, the proportion of the votes for the

winning class are returned as attributeprob.

use.all

controls handling of ties. If true, all distances equal

to thekth largest are

included. If false, a random selection of distances equal to thekth is chosen to use exactlykneighbours.

kknn(formula = formula(train), train, test, na.action = na.omit(), k = 7, distance = 2, kernel = "optimal", ykernel = NULL, scale=TRUE, contrasts = c('unordered' = "contr.dummy", ordered = "contr.ordinal"))

参数:

formula                            A formula object.

train                                 Matrix or data frame of training set cases.

test                                   Matrix or data frame of test set cases.

na.action                         A function which indicates what should happen when the data contain ’NA’s.

k                                       Number of neighbors considered.

distance                          Parameter of Minkowski distance.

kernel                              Kernel to use. Possible choices are "rectangular" (which is standard unweighted knn), "triangular", "epanechnikov" (or beta(2,2)), "biweight" (or beta(3,3)), "triweight" (or beta(4,4)), "cos", "inv", "gaussian", "rank" and "optimal".

ykernel                            Window width of an y-kernel, especially for prediction of ordinal classes.

scale                                Logical, scale variable to have equal sd.

contrasts                         A vector containing the ’unordered’ and ’ordered’ contrasts to use

kknn的返回值如下:

fitted.values              Vector of predictions.

CL                              Matrix of classes of the k nearest neighbors.

W                                Matrix of weights of the k nearest neighbors.

D                                 Matrix of distances of the k nearest neighbors.

C                                 Matrix of indices of the k nearest neighbors.

prob                            Matrix of predicted class probabilities.

response                   Type of response variable, one of continuous, nominal or ordinal.

distance                     Parameter of Minkowski distance.

call                              The matched call.

terms                          The ’terms’ object used.

iris%>%ggvis(~Length,~Sepal.Width,fill=~Species)

library(kknn)

data(iris)

dim(iris)

m<-(dim(iris))[1]

val<-sample(1:m,size=round(m/3),replace=FALSE,prob=rep(1/m,m))

建立训练数据集

data.train<-iris[-val,]

建立测试数据集

data.test<-iris[val,]

调用kknn  之前首先定义公式

formula : Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width

iris.kknn<-kknn(Species~.,iris.train,iris.test,distance=1,kernel="triangular")

summary(iris.kknn)

# 获取fitted.values

fit <- fitted(iris.kknn)

# 建立表格检验判类准确性

table(iris.valid$Species, fit)

# 绘画散点图,k-nearest neighbor用红色高亮显示

pcol <- as.character(as.numeric(iris.valid$Species))

pairs(iris.valid[1:4], pch = pcol, col = c("green3", "red")[(iris.valid$Species != fit)+1]

二、R语言knn算法

install.packages("class")

library(class)

对于新的测试样例基于距离相似度的法则,确定其K个最近的邻居,在K个邻居中少数服从多数

确定新测试样例的类别

1、获得数据

2、理解数据

对数据进行探索性分析,散点图

如上例

3、确定问题类型,分类数据分析

4、机器学习算法knn

5、数据处理,归一化数据处理

normalize <- function(x){

num <- x - min(x)

denom <- max(x) - min(x)

return(num/denom)

}

iris_norm <-as.data.frame(lapply(iris[,1:4], normalize))

summary(iris_norm)

6、训练集与测试集选取

一般按照3:1的比例选取

方法一、set.seed(1234)

ind <- sample(2,nrow(iris), replace=TRUE, prob=c(0.67, 0.33))

iris_train <-iris[ind==1, 1:4]

iris_test <-iris[ind==2, 1:4]

train_label <-iris[ind==1, 5]

test_label <-iris[ind==2, 5]

方法二、

ind<-sample(1:150,50)

iris_train<-iris[-ind,]

iris_test<-iris[ind,1:4]

iris_train<-iris[-ind,1:4]

train_label<-iris[-ind,5]

test_label<-iris[ind,5]

7、构建KNN模型

iris_pred<-knn(train=iris_train,test=iris_test,cl=train_label,k=3)

8、模型评价

交叉列联表法

table(test_label,iris_pred)

实例二

数据集

http://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data

导入数据

dir <-'http://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data'wdbc.data <-read.csv(dir,header = F)

names(wdbc.data) <- c('ID','Diagnosis','radius_mean','texture_mean','perimeter_mean','area_mean','smoothness_mean','compactness_mean','concavity_mean','concave points_mean','symmetry_mean','fractal dimension_mean','radius_sd','texture_sd','perimeter_sd','area_sd','smoothness_sd','compactness_sd','concavity_sd','concave points_sd','symmetry_sd','fractal dimension_sd','radius_max_mean','texture_max_mean','perimeter_max_mean','area_max_mean','smoothness_max_mean','compactness_max_mean','concavity_max_mean','concave points_max_mean','symmetry_max_mean','fractal dimension_max_mean')

table(wdbc.data$Diagnosis)## M = malignant, B = benign

wdbc.data$Diagnosis <- factor(wdbc.data$Diagnosis,levels =c('B','M'),labels = c(B ='benign',M ='malignant'))

一、层次聚类

1)距离和相似系数

r语言中使用dist(x, method = "euclidean",diag = FALSE, upper = FALSE, p = 2) 来计算距离。其中x是样本矩阵或者数据框。method表示计算哪种距离。method的取值有:

euclidean                欧几里德距离,就是平方再开方。

maximum                切比雪夫距离

manhattan 绝对值距离

canberra Lance 距离

minkowski            明科夫斯基距离,使用时要指定p值

binary                    定性变量距离.

定性变量距离: 记m个项目里面的 0:0配对数为m0 ,1:1配对数为m1,不能配对数为m2,距离=m1/(m1+m2)

diag 为TRUE的时候给出对角线上的距离。upper为TURE的时候给出上三角矩阵上的值。

r语言中使用scale(x, center = TRUE, scale = TRUE) 对数据矩阵做中心化和标准化变换。

如只中心化 scale(x,scale=F) ,

r语言中使用sweep(x, MARGIN, STATS, FUN="-", ...) 对矩阵进行运算。MARGIN为1,表示行的方向上进行运算,为2表示列的方向上运算。STATS是运算的参数。FUN为运算函数,默认是减法。下面利用sweep对矩阵x进行极差标准化变换

?

1

2

3

>center <-sweep(x, 2, apply(x, 2, mean)) #在列的方向上减去均值。

>R <-apply(x, 2, max) -apply(x,2,min)   #算出极差,即列上的最大值-最小值

>x_star <-sweep(center, 2, R, "/")        #把减去均值后的矩阵在列的方向上除以极差向量

?

1

2

3

>center <-sweep(x, 2, apply(x, 2, min)) #极差正规化变换

>R <-apply(x, 2, max) -apply(x,2,min)

>x_star <-sweep(center, 2, R, "/")

有时候我们不是对样本进行分类,而是对变量进行分类。这时候,我们不计算距离,而是计算变量间的相似系数。常用的有夹角和相关系数。

r语言计算两向量的夹角余弦:

?

1

2

y <-scale(x, center =F, scale =T)/sqrt(nrow(x)-1)

C <-t(y) %*%y

相关系数用cor函数

2)层次聚类法

层次聚类法。先计算样本之间的距离。每次将距离最近的点合并到同一个类。然后,再计算类与类之间的距离,将距离最近的类合并为一个大类。不停的合并,直到合成了一个类。其中类与类的距离的计算方法有:最短距离法,最长距离法,中间距离法,类平均法等。比如最短距离法,将类与类的距离定义为类与类之间样本的最段距离。。。

r语言中使用hclust(d, method = "complete", members=NULL) 来进行层次聚类。

其中d为距离矩阵。

method表示类的合并方法,有:

single            最短距离法

complete        最长距离法

median        中间距离法

mcquitty        相似法

average        类平均法

centroid        重心法

ward            离差平方和法

?

1

2

3

4

5

6

7

8

> x <-c(1,2,6,8,11)      #试用一下

> dim(x) <-c(5,1)

> d <-dist(x)

> hc1 <-hclust(d,"single")

> plot(hc1)

> plot(hc1,hang=-1,type="tirangle")             #hang小于0时,树将从底部画起。

#type = c("rectangle", "triangle"),默认树形图是方形的。另一个是三角形。

#horiz  TRUE 表示竖着放,FALSE表示横着放。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

> z <-scan()

1: 1.0000.8460.8050.8590.4730.3980.3010.382

9: 0.8461.0000.8810.8260.3760.3260.2770.277

17: 0.8050.8811.0000.8010.3800.3190.2370.345

25: 0.8590.8260.8011.0000.4360.3290.3270.365

33: 0.4730.3760.3800.4361.0000.7620.7300.629

41: 0.3980.3260.3190.3290.7621.0000.5830.577

49: 0.3010.2770.2370.3270.7300.5831.0000.539

57: 0.3820.4150.3450.3650.6290.5770.5391.000

65: 

Read 64items

> names

[1] "shengao""shoubi""shangzhi""xiazhi""tizhong"

[6] "jingwei""xiongwei""xiongkuang"

> r <-matrix(z,nrow=8,dimnames=list(names,names))

> d <-as.dist(1-r)

> hc <-hclust(d)

> plot(hc)

然后可以用rect.hclust(tree, k = NULL, which = NULL, x = NULL, h = NULL,border = 2, cluster = NULL)来确定类的个数。 tree就是求出来的对象。k为分类的个数,h为类间距离的阈值。border是画出来的颜色,用来分类的。

?

1

2

3

> plot(hc)

> rect.hclust(hc,k=2)

> rect.hclust(hc,h=0.5)

result=cutree(model,k=3) 该函数可以用来提取每个样本的所属类别

二、动态聚类k-means

层次聚类,在类形成之后就不再改变。而且数据比较大的时候更占内存。

动态聚类,先抽几个点,把周围的点聚集起来。然后算每个类的重心或平均值什么的,以算出来的结果为分类点,不断的重复。直到分类的结果收敛为止。r语言中主要使用kmeans(x, centers, iter.max = 10, nstart = 1, algorithm  =c("Hartigan-Wong", "Lloyd","Forgy", "MacQueen"))来进行聚类。centers是初始类的个数或者初始类的中心。iter.max是最大迭代次数。nstart是当centers是数字的时候,随机集合的个数。algorithm是算法,默认是第一个。

?

使用knn包进行Kmean聚类分析

将数据集进行备份,将列newiris$Species置为空,将此数据集作为测试数据集

>newiris <- iris

>newiris$Species <- NULL

在数据集newiris上运行Kmean聚类分析, 将聚类结果保存在kc中。在kmean函数中,将需要生成聚类数设置为3

>(kc <- kmeans(newiris, 3)) 

K-means clustering with 3 clusters of sizes 38, 50, 62: K-means算法产生了3个聚类,大小分别为38,50,62. 

Cluster means: 每个聚类中各个列值生成的最终平均值

  Sepal.Length Sepal.Width Petal.Length Petal.Width

1     5.006000    3.428000     1.462000    0.246000

2     5.901613    2.748387     4.393548    1.433871

3     6.850000    3.073684     5.742105    2.071053

Clustering vector: 每行记录所属的聚类(2代表属于第二个聚类,1代表属于第一个聚类,3代表属于第三个聚类)

  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

[37] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

[73] 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 3 3 3 3 2 3

[109] 3 3 3 3 3 2 2 3 3 3 3 2 3 2 3 2 3 3 2 2 3 3 3 3 3 2 3 3 3 3 2 3 3 3 2 3

[145] 3 3 2 3 3 2

Within cluster sum of squares by cluster: 每个聚类内部的距离平方和   

[1] 15.15100 39.82097 23.87947

(between_SS / total_SS =  88.4 %) 组间的距离平方和占了整体距离平方和的的88.4%,也就是说各个聚类间的距离做到了最大

Available components: 运行kmeans函数返回的对象所包含的各个组成部分

[1] "cluster"      "centers"      "totss"        "withinss"    

[5] "tot.withinss" "betweenss"    "size"  

("cluster"是一个整数向量,用于表示记录所属的聚类  

"centers"是一个矩阵,表示每聚类中各个变量的中心点

"totss"表示所生成聚类的总体距离平方和

"withinss"表示各个聚类组内的距离平方和

"tot.withinss"表示聚类组内的距离平方和总量

"betweenss"表示聚类组间的聚类平方和总量

"size"表示每个聚类组中成员的数量)

创建一个连续表,在三个聚类中分别统计各种花出现的次数

>table(iris$Species, kc$cluster)           

              1  2  3

  setosa      0 50  0

  versicolor  2  0 48

  virginica  36  0 14

根据最后的聚类结果画出散点图,数据为结果集中的列"Sepal.Length"和"Sepal.Width",颜色为用1,2,3表示的缺省颜色

>plot(newiris[c("Sepal.Length", "Sepal.Width")], col = kc$cluster)

在图上标出每个聚类的中心点

〉points(kc$centers[,c("Sepal.Length", "Sepal.Width")], col = 1:3, pch = 8, cex=2)

三、DBSCAN

动态聚类往往聚出来的类有点圆形或者椭圆形。基于密度扫描的算法能够解决这个问题。思路就是定一个距离半径,定最少有多少个点,然后把可以到达的点都连起来,判定为同类。在r中的实现

dbscan(data, eps, MinPts, scale, method, seeds, showplot, countmode)

其中eps是距离的半径,minpts是最少多少个点。 scale是否标准化(我猜) ,method 有三个值raw,dist,hybird,分别表示,数据是原始数据避免计算距离矩阵,数据就是距离矩阵,数据是原始数据但计算部分距离矩阵。showplot画不画图,0不画,1和2都画。countmode,可以填个向量,用来显示计算进度。用鸢尾花试一试

?

1

2

3

4

5

6

7

8

9

10

11

> install.packages("fpc", dependencies=T)

> library(fpc)

> newiris <-iris[1:4]

> model <-dbscan(newiris,1.5,5,scale=T,showplot=T,method="raw")# 画出来明显不对 把距离调小了一点

> model <-dbscan(newiris,0.5,5,scale=T,showplot=T,method="raw")

> model #还是不太理想……

dbscan Pts=150MinPts=5eps=0.5

        012

border 34518

seed    04053

total  344571