####聚类分析#### ####计算各种距离#dist()函数 dist(x, method = "euclidean", diag = FALSE, upper = FALSE, p = 2) ####数据中心化或标准化#scale()函数 scale(x, center = TRUE, scale = TRUE) ####极差标准化变换#sweep()函数 #变化过程 center <- sweep(x, 2, apply(x, 2, mean)) R <- apply(x, 2, max) - apply(x,2,min) x_star <- sweep(center, 2, R, "/") ####相似系数-两向量夹角余弦 y <- scale(x, center = F, scale = T)/sqrt(nrow(x)-1) C <- t(y) %*% y ####相关系数 C<-cor(x) ####系统聚类#hclust()函数 hclust(d, method = "complete", members=NULL) single——最短距离法 complete——最长距离法 median——中间距离法 mcquitty——相似法 average——类平均法 centroid——重心法 ward——类平均法 ####谱系图#plot() plot(x, labels = NULL, hang = 0.1, #x为hclust()生成的对象,hang表示所在位置 axes = TRUE, frame.plot = FALSE, ann = TRUE, main = "Cluster Dendrogram", sub = NULL, xlab = NULL, ylab = "Height", ...) #plclust()函数 plclust(tree, hang=0.1, unit=FALSE, level=FALSE, hmin=0, square=TRUE, labels=NULL, plot. = TRUE, axes = TRUE, frame.plot = FALSE, ann = TRUE, main = "", sub = NULL, xlab=NULL, ylab="Height") #####例8.6 x<-c(1,2,6,8,11);dim<-c(5,1);d<-dist(x) #输入数据,生成距离结构 hc1<-hclust(d,"single");hc2<-hclust(d,"complete") #生成系统聚类 hc3<-hclust(d,"median");hc4<-hclust(d,"mcquitty") opar<-par(mfrow=c(2,2)) #绘出所有树形结构图 plot(hc1,hang=-1);plot(hc2,hang=-1) plot(hc3,hang=-1);plot(hc4,hang=-1) par(opar) ##as.dendrogram()函数 #将系统聚类得到的对象强制为谱系图 as.dendrogram(object, hang = -1, ...) #图8.4 dend1<-as.dendrogram(hc1) opar <- par(mfrow = c(2, 2),mar = c(4,3,1,2)) plot(dend1) plot(dend1, nodePar=list(pch = c(1,NA), cex=0.8, lab.cex=0.8), type = "t", center=TRUE) plot(dend1, edgePar=list(col = 1:2, lty = 2:3), dLeaf=1, edge.root = TRUE) plot(dend1, nodePar=list(pch = 2:1, cex=.4*2:1, col=2:3), horiz=TRUE) par(opar) #例8.7 x<-c(1.000, 0.846, 0.805, 0.859, 0.473, 0.398, 0.301, 0.382, 0.846, 1.000, 0.881, 0.826, 0.376, 0.326, 0.277, 0.277, 0.805, 0.881, 1.000, 0.801, 0.380, 0.319, 0.237, 0.345, 0.859, 0.826, 0.801, 1.000, 0.436, 0.329, 0.327, 0.365, 0.473, 0.376, 0.380, 0.436, 1.000, 0.762, 0.730, 0.629, 0.398, 0.326, 0.319, 0.329, 0.762, 1.000, 0.583, 0.577, 0.301, 0.277, 0.237, 0.327, 0.730, 0.583, 1.000, 0.539, 0.382, 0.415, 0.345, 0.365, 0.629, 0.577, 0.539, 1.000) names<-c("身高 x1", "手臂长 x2", "上肢长 x3", "下肢长 x4", "体重 x5", "颈围 x6", "胸围 x7", "胸宽 x8") r<-matrix(x, nrow=8, dimnames=list(names, names)) ## 作系统聚类分析, ## as.dist()的作用是将普通矩阵转化为聚类分析用的距离结构. d<-as.dist(1-r);hc<-hclust(d);dend<-as.dendrogram(hc) ## 写一段小程序, 其目的是在绘图命令中调用它, 使谱系图更好看. nP<-list(col=3:2, cex=c(2.0, 0.75), pch= 21:22, bg= c("light blue", "pink"), lab.cex = 1.0, lab.col = "tomato") addE <- function(n){ if(!is.leaf(n)) { attr(n, "edgePar") <- list(p.col="plum") attr(n, "edgetext") <- paste(attr(n,"members"),"members") } n } ## 画出谱系图. op<-par(mfrow=c(1,1), mar=c(4,3,0.5,0)) de <- dendrapply(dend, addE); plot(de, nodePar= nP) par(op) ####类个数的确定#rect.hclust()函数 rect.hclust(tree, k = NULL, which = NULL, x = NULL, h = NULL, border = 2, cluster = NULL) #tree为hclust生成的结构;k为类的个数;h是谱系图的阀值;border表明数据框的颜色 plclust(hc,hang=-1);re<-rect.hclust(hc,k=3) ####例8.8 X<-data.frame( x1=c(2959.19, 2459.77, 1495.63, 1046.33, 1303.97, 1730.84, 1561.86, 1410.11, 3712.31, 2207.58, 2629.16, 1844.78, 2709.46, 1563.78, 1675.75, 1427.65, 1783.43, 1942.23, 3055.17, 2033.87, 2057.86, 2303.29, 1974.28, 1673.82, 2194.25, 2646.61, 1472.95, 1525.57, 1654.69, 1375.46, 1608.82), x2=c(730.79, 495.47, 515.90, 477.77, 524.29, 553.90, 492.42, 510.71, 550.74, 449.37, 557.32, 430.29, 428.11, 303.65, 613.32, 431.79, 511.88, 512.27, 353.23, 300.82, 186.44, 589.99, 507.76, 437.75, 537.01, 839.70, 390.89, 472.98, 437.77, 480.99, 536.05), x3=c(749.41, 697.33, 362.37, 290.15, 254.83, 246.91, 200.49, 211.88, 893.37, 572.40, 689.73, 271.28, 334.12, 233.81, 550.71, 288.55, 282.84, 401.39, 564.56, 338.65, 202.72, 516.21, 344.79, 461.61, 369.07, 204.44, 447.95, 328.90, 258.78, 273.84, 432.46), x4=c(513.34, 302.87, 285.32, 208.57, 192.17, 279.81, 218.36, 277.11, 346.93, 211.92, 435.69, 126.33, 160.77, 107.90, 219.79, 208.14, 201.01, 206.06, 356.27, 157.78, 171.79, 236.55, 203.21, 153.32, 249.54, 209.11, 259.51, 219.86, 303.00, 317.32, 235.82), x5=c(467.87, 284.19, 272.95, 201.50, 249.81, 239.18, 220.69, 224.65, 527.00, 302.09, 514.66, 250.56, 405.14, 209.70, 272.59, 217.00, 237.60, 321.29, 811.88, 329.06, 329.65, 403.92, 240.24, 254.66, 290.84, 379.30, 230.61, 206.65, 244.93, 251.08, 250.28), x6=c(1141.82, 735.97, 540.58, 414.72, 463.09, 445.20, 459.62, 376.82, 1034.98, 585.23, 795.87, 513.18, 461.67, 393.99, 599.43, 337.76, 617.74, 697.22, 873.06, 621.74, 477.17, 730.05, 575.10, 445.59, 561.91, 371.04, 490.90, 449.69, 479.53, 424.75, 541.30), x7=c(478.42, 570.84, 364.91, 281.84, 287.87, 330.24, 360.48, 317.61, 720.33, 429.77, 575.76, 314.00, 535.13, 509.39, 371.62, 421.31, 523.52, 492.60, 1082.82, 587.02, 312.93, 438.41, 430.36, 346.11, 407.70, 269.59, 469.10, 249.66, 288.56, 228.73, 344.85), x8=c(457.64, 305.08, 188.63, 212.10, 192.96, 163.86, 147.76, 152.85, 462.03, 252.54, 323.36, 151.39, 232.29, 160.12, 211.84, 165.32, 182.52, 226.45, 420.81, 218.27, 279.19, 225.80, 223.46, 191.48, 330.95, 389.33, 191.34, 228.19, 236.51, 195.93, 214.40), row.names = c("北京", "天津", "河北", "山西", "内蒙古", "辽宁", "吉林", "黑龙江", "上海", "江苏", "浙江", "安徽", "福建", "江西", "山东", "河南", "湖北", "湖南", "广东", "广西", "海南", "重庆", "四川", "贵州", "云南", "西藏", "陕西", "甘肃", "青海", "宁夏", "新疆") ) ##生成距离结构,做系统聚类 d<-dist(scale(X)) hc1<-hclust(d); hc2<-hclust(d, "average") hc3<-hclust(d, "centroid");hc4<-hclust(d, "ward") ##绘出谱系图和聚类情况 opar<-par(mfrow=c(2,1),mar=c(5.2,4,0,0)) plclust(hc1,hang=-1);re1<-rect.hclust(hc1,k=5,border="red") plclust(hc2,hang=-1);re2<-rect.hclust(hc2,k=5,border="red") par(opar) opar<-par(mfrow=c(2,1), mar=c(5.2,4,0,0)) plclust(hc3,hang=-1);re3<-rect.hclust(hc3,k=5,border="red") plclust(hc4,hang=-1);re4<-rect.hclust(hc4,k=5,border="red") par(opar) #####动态聚类法#K均值 ##kmeans()函数 kmeans(x, centers, iter.max = 10, nstart = 1, algorithm = c("Hartigan-Wong", "Lloyd", "Forgy", "MacQueen")) ##例8.9 km<-kmeans(scale(X),centers=5,nstart=20) km |
|