-
Notifications
You must be signed in to change notification settings - Fork 2
/
Clustering.R
93 lines (66 loc) · 2.31 KB
/
Clustering.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
dataset <- read.csv('usa_final.csv', sep=',')
rownames(dataset)<-dataset[,1]
datanames <- dataset
datanames$cl <- cut(datanames$aqi, breaks = c(50,100,150,200),
labels = c('yellow', 'orange', 'red'))
datanames$cl <- as.factor(datanames$cl)
dataset <- dataset[,2:19]
ds<-dist(scale(dataset))
agglo <- function(hc){
data.frame(row.names=paste0("Cluster",seq_along(hc$height)),
height=hc$height,
compoments=ifelse(hc$merge<0,
hc$labels[abs(hc$merge)], paste0("Cluster",hc$merge))
)}
opar <- par(mfrow = c(1, 3))
par(mfrow=c(1,3))
#first clustering: average linkage
h1 <- hclust(ds,method="average")
agglo(h1)
plot(h1, main="Average linkage")
#add robustness to the analysis trying other methods
h2 <- hclust(ds,method="complete")
agglo(h2)
plot(h2, main="Complete linkage")
#Ward method can help us in reducing distances of outliers such as California
h3 <- hclust(ds,method="ward.D2")
agglo(h3)
plot(h3, main="Ward linkage")
#choice of number of clusters - elbow method
set.seed(123)
#compute and plot wss
wss <- sapply(1:12,
function(k){kmeans(ds, k, nstart=50,iter.max = 12 )$tot.withinss})
wss
plot(1:12, wss,
type="b", pch = 19, frame = FALSE,
xlab="Number of clusters K",
ylab="Total within-clusters sum of squares")
#the plot suggests the number k of clusters
cut_av <- cutree(h1, k = 3)
cut_compl <- cutree(h2, k = 3)
cut_ward <- cutree(h3, k = 3)
#confront the different clustering methods
table(cut_av,cut_compl)
table(cut_av,cut_ward)
plot(h3, main="Clusters with Ward linkage method")
rect.hclust(h3,3)
# Lets compare the share of polluted groups within each of the 3 clusters
cut_ward <- as.data.frame(cut_ward)
cut_ward <- cbind(rownames(cut_ward), cut_ward)
names(cut_ward) <- c("state","cluster")
cut_ward <- merge(cut_ward, datanames[,c(1,22)], by = "state")
library(dplyr)
group_by(cut_ward, cluster, cl) %>%
summarise(
count = n(),
share = n()/51*100
)
install.packages("sjPlot")
library(sjPlot)
sjPlot::tab_xtab(cut_ward$cluster, cut_ward$cl, show.row.prc = TRUE)
#Correlation tests
cut_ward$cl2 <- as.numeric(cut_ward$cl)
cor(cut_ward$cluster, cut_ward$cl2, method = "pearson")
cor.test(cut_ward$cluster, cut_ward$cl2, method = "pearson")
# There is no relationship between both indexes (cluster and pollution category)