R语言学习笔记之聚类分析

Python031

R语言学习笔记之聚类分析,第1张

R语言学习笔记之聚类分析

使用k-means聚类所需的包:

factoextra

cluster #加载包

library(factoextra)

library(cluster)l

#数据准备

使用内置的R数据集USArrests

#load the dataset

data("USArrests")

#remove any missing value (i.e, NA values for not available)

#That might be present in the data

USArrests <- na.omit(USArrests)#view the first 6 rows of the data

head(USArrests, n=6)

在此数据集中,列是变量,行是观测值

在聚类之前我们可以先进行一些必要的数据检查即数据描述性统计,如平均值、标准差等

desc_stats <- data.frame( Min=apply(USArrests, 2, min),#minimum

Med=apply(USArrests, 2, median),#median

Mean=apply(USArrests, 2, mean),#mean

SD=apply(USArrests, 2, sd),#Standard deviation

Max=apply(USArrests, 2, max)#maximum

)

desc_stats <- round(desc_stats, 1)#保留小数点后一位head(desc_stats)

变量有很大的方差及均值时需进行标准化

df <- scale(USArrests)

#数据集群性评估

使用get_clust_tendency()计算Hopkins统计量

res <- get_clust_tendency(df, 40, graph = TRUE)

res$hopkins_stat

## [1] 0.3440875

#Visualize the dissimilarity matrix

res$plot

Hopkins统计量的值<0.5,表明数据是高度可聚合的。另外,从图中也可以看出数据可聚合。

#估计聚合簇数

由于k均值聚类需要指定要生成的聚类数量,因此我们将使用函数clusGap()来计算用于估计最优聚类数。函数fviz_gap_stat()用于可视化。

set.seed(123)

## Compute the gap statistic

gap_stat <- clusGap(df, FUN = kmeans, nstart = 25, K.max = 10, B = 500)

# Plot the result

fviz_gap_stat(gap_stat)

图中显示最佳为聚成四类(k=4)

#进行聚类

set.seed(123)

km.res <- kmeans(df, 4, nstart = 25)

head(km.res$cluster, 20)

# Visualize clusters using factoextra

fviz_cluster(km.res, USArrests)

#检查cluster silhouette图

Recall that the silhouette measures (SiSi) how similar an object ii is to the the other objects in its own cluster versus those in the neighbor cluster. SiSi values range from 1 to - 1:

A value of SiSi close to 1 indicates that the object is well clustered. In the other words, the object ii is similar to the other objects in its group.

A value of SiSi close to -1 indicates that the object is poorly clustered, and that assignment to some other cluster would probably improve the overall results.

sil <- silhouette(km.res$cluster, dist(df))

rownames(sil) <- rownames(USArrests)

head(sil[, 1:3])

#Visualize

fviz_silhouette(sil)

图中可以看出有负值,可以通过函数silhouette()确定是哪个观测值

neg_sil_index <- which(sil[, "sil_width"] <0)

sil[neg_sil_index, , drop = FALSE]

##          cluster    neighbor     sil_width

## Missouri    3          2        -0.07318144

#eclust():增强的聚类分析

与其他聚类分析包相比,eclust()有以下优点:

简化了聚类分析的工作流程

可以用于计算层次聚类和分区聚类

eclust()自动计算最佳聚类簇数。

自动提供Silhouette plot

可以结合ggplot2绘制优美的图形

#使用eclust()的K均值聚类

# Compute k-means

res.km <- eclust(df, "kmeans")

# Gap statistic plot

fviz_gap_stat(res.km$gap_stat)

# Silhouette plotfviz_silhouette(res.km)

##    cluster size ave.sil.width

## 1     1     13      0.31

## 2     2     29      0.38

## 3     3      8      0.39

#使用eclust()的层次聚类

# Enhanced hierarchical clustering

res.hc <- eclust(df, "hclust") # compute hclust

fviz_dend(res.hc, rect = TRUE) # dendrogam

#下面的R代码生成Silhouette plot和分层聚类散点图。

fviz_silhouette(res.hc) # silhouette plot

##   cluster size ave.sil.width

## 1    1     19      0.26

## 2    2     19      0.28

## 3    3     12      0.43

fviz_cluster(res.hc) # scatter plot

#Infos

This analysis has been performed using R software (R version 3.3.2)

Fortran多princeado(站内联系TA)M-K检验是做趋势分析还是突变检验,我编了相关的程序,不是很长的,用R语言编的,不过DPS系统也可以做M-K检验,如果有你可以试一下.guoguo1983(站内联系TA)要是用MK做趋势检验,用那个软件呀princeado(站内联系TA)做趋势分析的话可以用R语言来做,有个包可以做,另外我编了一段的程序,不是很长,DPS系统也可以做,很方便。shzrzheng(站内联系TA)知道原理后,自己编编程序做shhongyuan6331(站内联系TA)DPS:tiger28:leehui789(站内联系TA)现代气候统计诊断与预测技术 气象出版社挺方便的gishuazi(站内联系TA)建议看看陈彦光的书wuyang2468(站内联系TA)用R或者Matlab吧,发文章的话,R的公信度还是比较高的。erqie(站内联系TA)借地问一句,R语言是什么呀?horseechen(站内联系TA)我不是搞地学水文研究的,不过处理离散数据序列时检测突变点也想用这个检验方法,发现网上多数是中国人写的文章,而且错误百出。不这我发现这个方法太受秩序列长度的限制了,对于同样一组数据,整体趋势分析的结果跟分段分析的结果,在突变点检测上几乎没有一致的精度。lijun2011(站内联系TA)R也是一个统计软件,代码公开,不收费的软件,国外用的人多,国内不怎么用geonuist(站内联系TA)EXCEL编个宏,可以看看陈彦光写的《基于EXCEL的地理数据分析》,里面有介绍儿时梦想尚未成(站内联系TA)最近刚刚发现有个EXCEL插件很好用,XLSTATE2013,网上可以下载:victory:geonuist(站内联系TA)EXCEL就可以做,详见本论坛中陈彦光编写的《基于EXCEL的地理数据分析》

r语言数据分析是查看数据的结构、类型,数据处理。根据查询相关资料信息显示:R语言是一个开源、跨平台的科学计算和统计分析软件包,具有丰富多样、强大的的统计功能和数据分析功能,数据可视化可以绘制直方图、箱型图、小提琴图等展示分数的分布情况可以通过散点图和线性拟合来展示分数和年龄之间的关系。