Author: Edi Oblak
How can we define states on segments based on the population, income,
illiteracy, life expectancy, murder rate, percentage of high-school
graduates and area?
We can perform clustering process which allows us to define clusters of
units using different numerical variables. We hope to get homogeneous
groups that differ among each other.
#data(package = .packages(all.available = TRUE))
mydata <- as.data.frame(force(state.x77)) # Importing data from data().
colnames(mydata)[4] ="LifeExp"
colnames(mydata)[6] ="HSGrad"
mydata$ID <- seq(1, nrow(mydata))# We add ID number for each unit
head(mydata)
## Population Income Illiteracy LifeExp Murder HSGrad Frost Area ID
## Alabama 3615 3624 2.1 69.05 15.1 41.3 20 50708 1
## Alaska 365 6315 1.5 69.31 11.3 66.7 152 566432 2
## Arizona 2212 4530 1.8 70.55 7.8 58.1 15 113417 3
## Arkansas 2110 3378 1.9 70.66 10.1 39.9 65 51945 4
## California 21198 5114 1.1 71.71 10.3 62.6 20 156361 5
## Colorado 2541 4884 0.7 72.06 6.8 63.9 166 103766 6
Dataset is representing US state facts and figures. For better understanding of data, we can read description of columns:
This data is appropriate for us because it uses numeric variables, has 50 rows and 8 variables. Our unit of observation is US state.
Source: U.S. Department of Commerce, Bureau of the Census (1977) Statistical Abstract of the United States.
We can perform some descriptive statistics very easily with function describe.
describe(mydata)
## mydata
##
## 9 Variables 50 Observations
## ------------------------------------------------------------------------------------------------------------------------
## Population
## n missing distinct Info Mean Gmd .05 .10 .25 .50 .75 .90 .95
## 50 0 50 1 4246 4364 520.2 632.3 1079.5 2838.5 4968.5 10781.2 12067.3
##
## lowest : 365 376 472 579 590, highest: 11197 11860 12237 18076 21198
## ------------------------------------------------------------------------------------------------------------------------
## Income
## n missing distinct Info Mean Gmd .05 .10 .25 .50 .75 .90 .95
## 50 0 50 1 4436 692.7 3570 3623 3993 4519 4814 5118 5271
##
## lowest : 3098 3378 3545 3601 3617, highest: 5149 5237 5299 5348 6315
## ------------------------------------------------------------------------------------------------------------------------
## Illiteracy
## n missing distinct Info Mean Gmd .05 .10 .25 .50 .75 .90 .95
## 50 0 20 0.989 1.17 0.674 0.545 0.600 0.625 0.950 1.575 2.110 2.255
##
## Value 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.8
## Frequency 3 10 5 3 4 1 5 2 3 1 1 1 2 2 1 1 2 1 1 1
## Proportion 0.06 0.20 0.10 0.06 0.08 0.02 0.10 0.04 0.06 0.02 0.02 0.02 0.04 0.04 0.02 0.02 0.04 0.02 0.02 0.02
##
## For the frequency table, variable is rounded to the nearest 0
## ------------------------------------------------------------------------------------------------------------------------
## LifeExp
## n missing distinct Info Mean Gmd .05 .10 .25 .50 .75 .90 .95
## 50 0 47 1 70.88 1.537 68.64 69.05 70.12 70.67 71.89 72.58 72.85
##
## lowest : 67.96 68.09 68.54 68.76 69.03, highest: 72.6 72.78 72.9 72.96 73.6
## ------------------------------------------------------------------------------------------------------------------------
## Murder
## n missing distinct Info Mean Gmd .05 .10 .25 .50 .75 .90 .95
## 50 0 44 1 7.378 4.277 2.30 2.67 4.35 6.85 10.67 11.66 12.88
##
## lowest : 1.4 1.7 2.3 2.4 2.7 , highest: 12.2 12.5 13.2 13.9 15.1
## ------------------------------------------------------------------------------------------------------------------------
## HSGrad
## n missing distinct Info Mean Gmd .05 .10 .25 .50 .75 .90 .95
## 50 0 47 1 53.11 9.24 39.13 40.96 48.05 53.25 59.15 62.96 64.61
##
## lowest : 37.8 38.5 39.9 40.6 41 , highest: 63.5 63.9 65.2 66.7 67.3
## ------------------------------------------------------------------------------------------------------------------------
## Frost
## n missing distinct Info Mean Gmd .05 .10 .25 .50 .75 .90 .95
## 50 0 43 1 104.5 59.79 13.35 20.00 66.25 114.50 139.75 168.40 173.55
##
## lowest : 0 11 12 15 20, highest: 172 173 174 186 188
## ------------------------------------------------------------------------------------------------------------------------
## Area
## n missing distinct Info Mean Gmd .05 .10 .25 .50 .75 .90 .95
## 50 0 50 1 70736 66622 5565 7796 36985 54277 81163 114217 151513
##
## lowest : 1049 1982 4862 6425 7521, highest: 121412 145587 156361 262134 566432
## ------------------------------------------------------------------------------------------------------------------------
## ID
## n missing distinct Info Mean Gmd .05 .10 .25 .50 .75 .90 .95
## 50 0 50 1 25.5 17 3.45 5.90 13.25 25.50 37.75 45.10 47.55
##
## lowest : 1 2 3 4 5, highest: 46 47 48 49 50
## ------------------------------------------------------------------------------------------------------------------------
With next function we can do some more descriptive statistic. We chose to check quantiles for murder and non-negligent manslaughter rate per 100,000 population (1976).
summary(mydata[ , 5])
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.400 4.350 6.850 7.378 10.675 15.100
Based on the data provided we can form next statements:
* 25 % of states had murder and non-negligent manslaughter rate in year
1976 smaller than 4.35 people per 100,000 residents.
* Median rate of murder and non-negligent manslaughter is 6.85 people
per 100,000 residents.
* 25 % of states had murder and non-negligent manslaughter rate in year
1976 bigger than 10.68 people per 100,000 residents.
We can also check the income per capita in 1974 more thouroughly.
summary(mydata[ , 2])
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 3098 3993 4519 4436 4814 6315
We can see that 75 % of states had income below 4814 dollars per capita in year 1974 and 25 % more than that. We can also see, that 25 % of states had income below 3993 dollars per capita. Average income was 4436 dollars.
mydata$Pop_z <- scale(mydata$Population)
mydata$Inc_z <- scale(mydata$Income)
mydata$Ill_z <- scale(mydata$Illiteracy)
mydata$Life_z <- scale(mydata$LifeExp)
mydata$Murd_z <- scale(mydata$Murder)
mydata$HSGrd_z <- scale(mydata$HSGrad)
mydata$Area_z <- scale(mydata$Area)
mydata$Frst_z <- scale(mydata$Frost)
rcorr(as.matrix(mydata[, c("Pop_z", "Inc_z", "Ill_z", "Life_z", "Murd_z", "HSGrd_z", "Area_z", "Frst_z")]),
type = "pearson")
## Pop_z Inc_z Ill_z Life_z Murd_z HSGrd_z Area_z Frst_z
## Pop_z 1.00 0.21 0.11 -0.07 0.34 -0.10 0.02 -0.33
## Inc_z 0.21 1.00 -0.44 0.34 -0.23 0.62 0.36 0.23
## Ill_z 0.11 -0.44 1.00 -0.59 0.70 -0.66 0.08 -0.67
## Life_z -0.07 0.34 -0.59 1.00 -0.78 0.58 -0.11 0.26
## Murd_z 0.34 -0.23 0.70 -0.78 1.00 -0.49 0.23 -0.54
## HSGrd_z -0.10 0.62 -0.66 0.58 -0.49 1.00 0.33 0.37
## Area_z 0.02 0.36 0.08 -0.11 0.23 0.33 1.00 0.06
## Frst_z -0.33 0.23 -0.67 0.26 -0.54 0.37 0.06 1.00
##
## n= 50
##
##
## P
## Pop_z Inc_z Ill_z Life_z Murd_z HSGrd_z Area_z Frst_z
## Pop_z 0.1467 0.4569 0.6387 0.0146 0.4962 0.8765 0.0184
## Inc_z 0.1467 0.0015 0.0156 0.1080 0.0000 0.0095 0.1141
## Ill_z 0.4569 0.0015 0.0000 0.0000 0.0000 0.5938 0.0000
## Life_z 0.6387 0.0156 0.0000 0.0000 0.0000 0.4581 0.0660
## Murd_z 0.0146 0.1080 0.0000 0.0000 0.0003 0.1106 0.0000
## HSGrd_z 0.4962 0.0000 0.0000 0.0000 0.0003 0.0179 0.0088
## Area_z 0.8765 0.0095 0.5938 0.4581 0.1106 0.0179 0.6828
## Frst_z 0.0184 0.1141 0.0000 0.0660 0.0000 0.0088 0.6828
From the correlation matrix we can see, that some of the variables are too strongly correlated. Limit value for correlation between variables when performing clustering is 0.3. Correlation between Murd_z and Ill_z is 0.7 and correlation between Murd_z and Life_z is 0.78. We will not use Murd_z when performing clustering. There is also too high correlation between Inc_z and some other variables. We will exclude this variable from clustering process too. The last one variable to exclude is Frst_z, as it is too strongly correlated with Ill_z. There is high correlation between Life_z and HSGrad_z, but we will keep both variables in clustering process, as we want to have five variables to perform analysis.
mydata$Dissimilarity <- sqrt(mydata$Pop_z^2 + mydata$Ill_z^2 + mydata$Life_z^2 + mydata$HSGrd_z^2 + mydata$Area_z^2) #Finding outliers
head(mydata[order(-mydata$Dissimilarity), ], 10) #10 units with the highest value of dissimilarity
## Population Income Illiteracy LifeExp Murder HSGrad Frost Area ID Pop_z Inc_z Ill_z
## Alaska 365 6315 1.5 69.31 11.3 66.7 152 566432 2 -0.86939802 3.0582456 0.541398
## California 21198 5114 1.1 71.71 10.3 62.6 20 156361 5 3.79697895 1.1037155 -0.114842
## South Carolina 2816 3635 2.3 67.96 11.6 37.8 65 30225 40 -0.32039932 -1.3032371 1.853878
## Texas 12237 4188 2.2 70.90 12.2 47.4 35 262134 43 1.78980744 -0.4032744 1.689818
## Louisiana 3806 3545 2.8 68.76 13.2 42.2 12 44930 18 -0.09864953 -1.4497048 2.674178
## Mississippi 2341 3098 2.4 68.09 12.5 41.0 50 47296 24 -0.42679441 -2.1771610 2.017938
## New York 18076 4903 1.4 70.55 10.9 52.7 82 47831 32 3.09768318 0.7603301 0.377338
## Hawaii 868 4963 1.9 73.60 6.2 61.9 0 6425 11 -0.75673121 0.8579753 1.197638
## Georgia 4931 4091 2.0 68.54 13.9 40.6 60 58073 10 0.15333885 -0.5611340 1.361698
## Utah 1203 4022 0.6 72.90 4.5 67.3 137 82096 44 -0.68169467 -0.6734259 -0.935142
## Life_z Murd_z HSGrd_z Area_z Frst_z Dissimilarity
## Alaska -1.16850978 1.0624293 1.68280347 5.8093497 0.9145676 6.244578
## California 0.61934147 0.7915396 1.17518912 1.0034903 -1.6248292 4.147515
## South Carolina -2.17417612 1.1436962 -1.89525865 -0.4747704 -0.7591257 3.476201
## Texas 0.01594167 1.3062300 -0.70669822 2.2431053 -1.3362614 3.404423
## Louisiana -1.57822570 1.5771197 -1.35050179 -0.3024340 -1.7787320 3.401041
## Mississippi -2.07733417 1.3874969 -1.49907184 -0.2747055 -1.0476936 3.300337
## New York -0.24478663 0.9540734 -0.05051382 -0.2684355 -0.4320822 3.142062
## Hawaii 2.02727434 -0.3191080 1.08852326 -0.7536964 -2.0095863 2.805309
## Georgia -1.74211206 1.7667425 -1.54859519 -0.1484036 -0.8553150 2.707925
## Utah 1.50581772 -0.7796205 1.75708850 0.1331358 0.6259998 2.590708
We found two units, that are highly dissimilar compared to others. These are Alaska and California. We will remove those two objects.
mydata <- mydata[c(-2, -5), ] #Removing Alaska and California
#Calculating Euclidean distances
distance <- get_dist(mydata[c("Pop_z", "Ill_z", "Life_z", "HSGrd_z", "Area_z")],
method = "euclidian")
distance2 <- distance^2
fviz_dist(distance2) #Showing dissimilarity matrix
We can see some groups forming from dissimilarity matrix. To be sure
that data is clusterable, we also perform Hopkins statistics.
get_clust_tendency(mydata[c("Pop_z", "Ill_z", "Life_z", "HSGrd_z", "Area_z")], #Hopkins statistics
n = nrow(mydata) - 1,
graph = FALSE)
## $hopkins_stat
## [1] 0.7162991
##
## $plot
## NULL
Based on results of Hopkins statistics, we are able to state that objects are suitable for classification.
WARD <- mydata[c("Pop_z", "Ill_z", "Life_z", "HSGrd_z", "Area_z")] %>%
#scale() %>%
get_dist(method = "euclidean") %>% #Selecting distance
hclust(method = "ward.D2") #Selecting algorithm
WARD
##
## Call:
## hclust(d = ., method = "ward.D2")
##
## Cluster method : ward.D2
## Distance : euclidean
## Number of objects: 48
fviz_dend(WARD,
k = 4,
cex = 0.5,
palette = "jama",
color_labels_by_k = TRUE,
rect = TRUE)
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as of ggplot2 3.3.4.
## ℹ The deprecated feature was likely used in the factoextra package.
## Please report the issue at <https://github.com/kassambara/factoextra/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
From dendrogram we can see that the most appropriate number of clusters
in our case is four.
mydata$ClusterWard <- cutree(WARD,
k = 4) #Cutting the dendrogram
head(mydata)
## Population Income Illiteracy LifeExp Murder HSGrad Frost Area ID Pop_z Inc_z Ill_z
## Alabama 3615 3624 2.1 69.05 15.1 41.3 20 50708 1 -0.1414316 -1.3211387 1.525758
## Arizona 2212 4530 1.8 70.55 7.8 58.1 15 113417 3 -0.4556891 0.1533029 1.033578
## Arkansas 2110 3378 1.9 70.66 10.1 39.9 65 51945 4 -0.4785360 -1.7214837 1.197638
## Colorado 2541 4884 0.7 72.06 6.8 63.9 166 103766 6 -0.3819965 0.7294092 -0.771082
## Connecticut 3100 5348 1.1 72.48 3.1 56.0 139 4862 7 -0.2567862 1.4845315 -0.114842
## Delaware 579 4809 0.9 70.06 6.2 54.6 103 1982 8 -0.8214642 0.6073527 -0.442962
## Life_z Murd_z HSGrd_z Area_z Frst_z Dissimilarity ClusterWard
## Alabama -1.3621937 2.0918101 -1.4619293 -0.2347183 -1.62482920 2.529000 1
## Arizona -0.2447866 0.1143154 0.6180514 0.5002047 -1.72101848 1.402872 2
## Arkansas -0.1628435 0.7373617 -1.6352611 -0.2202212 -0.75912574 2.100578 1
## Colorado 0.8800698 -0.1565742 1.3361400 0.3870991 1.18389757 1.857452 3
## Connecticut 1.1929438 -1.1588660 0.3580538 -0.7720141 0.66447550 1.492130 2
## Delaware -0.6098063 -0.3191080 0.1847221 -0.8057665 -0.02808727 1.387899 2
Also calculation of initial leaders is performed.
#Calculating positions of initial leaders
Initial_leaders <- aggregate(mydata[, c("Pop_z", "Ill_z", "Life_z", "HSGrd_z", "Area_z")],
by = list(mydata$ClusterWard),
FUN = mean)
Initial_leaders
## Group.1 Pop_z Ill_z Life_z HSGrd_z Area_z
## 1 1 -0.1802042 1.361698 -1.2534327 -1.5832615 -0.3168222
## 2 2 -0.2931433 -0.114842 0.2762779 0.0856754 -0.3746630
## 3 3 -0.4806553 -0.935142 0.7058689 0.9656673 0.1914524
## 4 4 1.7821171 0.049218 -0.2236801 -0.2011473 0.1641829
#Performing K-Means clustering - initial leaders are chosen as centroids of groups, found with hierarhical clustering
K_MEANS <- hkmeans(mydata[c("Pop_z", "Ill_z", "Life_z", "HSGrd_z", "Area_z")],
k = 4,
hc.metric = "euclidean",
hc.method = "ward.D2")
We dont’t show an output as it is not our final result yet.
fviz_cluster(K_MEANS,
palette = "jama",
repel = FALSE,
ggtheme = theme_classic())
We can see an outlier in the above graph. We have to remove Texas from
our data.
mydata <- mydata %>%
filter(!ID %in% c(43))
# SCALING
mydata_clu_std <- as.data.frame(scale(mydata[c("Pop_z", "Ill_z", "Life_z", "HSGrd_z", "Area_z")])) #We must not forget to do the standardization again.
WARD <- mydata[c("Pop_z", "Ill_z", "Life_z", "HSGrd_z", "Area_z")] %>%
#scale() %>%
get_dist(method = "euclidean") %>% #Selecting distance
hclust(method = "ward.D2") #Selecting algorithm
WARD
##
## Call:
## hclust(d = ., method = "ward.D2")
##
## Cluster method : ward.D2
## Distance : euclidean
## Number of objects: 47
kmeans_clu <- hkmeans(mydata_clu_std,
k = 4,
hc.metric = "euclidean",
hc.method = "ward.D2")
kmeans_clu
## Hierarchical K-means clustering with 4 clusters of sizes 11, 19, 11, 6
##
## Cluster means:
## Pop_z Ill_z Life_z HSGrd_z Area_z
## 1 -0.05989929 1.3209363 -1.1889339 -1.48277738 -0.3184743
## 2 -0.47623707 -0.5808877 0.5647282 0.71185989 0.9463193
## 3 -0.28621520 -0.2345290 0.3560276 0.27995556 -1.2490645
## 4 2.14262730 -0.1522689 -0.2613111 -0.04904964 -0.1228566
##
## Clustering vector:
## Alabama Arizona Arkansas Colorado Connecticut Delaware Florida Georgia
## 1 2 1 2 3 3 4 1
## Hawaii Idaho Illinois Indiana Iowa Kansas Kentucky Louisiana
## 3 2 4 3 2 2 1 1
## Maine Maryland Massachusetts Michigan Minnesota Mississippi Missouri Montana
## 3 3 3 4 2 1 2 2
## Nebraska Nevada New Hampshire New Jersey New Mexico New York North Carolina North Dakota
## 2 2 3 3 2 4 1 2
## Ohio Oklahoma Oregon Pennsylvania Rhode Island South Carolina South Dakota Tennessee
## 4 2 2 4 3 1 2 1
## Utah Vermont Virginia Washington West Virginia Wisconsin Wyoming
## 2 3 1 2 1 2 2
##
## Within cluster sum of squares by cluster:
## [1] 12.560157 40.708570 18.284935 5.817285
## (between_SS / total_SS = 66.4 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss" "betweenss" "size"
## [8] "iter" "ifault" "data" "hclust"
* From the above output we can inform ourselves about some facts that
define clusters. There are 11 states in first cluster, 19 states in
seccond cluster, 11 states in third cluster and 6 states in fourth
cluster.
The next text output can provide us with information about values of each variable mean, for each cluster separately.
We can also see which state is assigned to which cluster.
Ratio of between_SS and total_SS tells us how much units vary in each group between each other. Our result is 66.4 % which is quite high. The higher score, the better for our analysis. We can point out that the result is better since we removed one unit.
fviz_cluster(kmeans_clu,
palette = "jama",
repel = FALSE,
ggtheme = theme_classic())
On the above graph we can see final visualization of variables. There is
70.4 % (21.2 + 49.2) of information used for clustering shown on the
graph.
mydata$ClusterK_Means <- kmeans_clu$cluster
head(mydata[c("ID", "ClusterWard", "ClusterK_Means")])
## ID ClusterWard ClusterK_Means
## Alabama 1 1 1
## Arizona 3 2 2
## Arkansas 4 1 1
## Colorado 6 3 2
## Connecticut 7 2 3
## Delaware 8 2 3
table(mydata$ClusterWard) #Number of units in initial groups
##
## 1 2 3 4
## 10 19 13 5
table(mydata$ClusterK_Means) # Number of units in reclassified groups
##
## 1 2 3 4
## 11 19 11 6
There were some reclassifications during the process of K-means clustering.
table(mydata$ClusterWard, mydata$ClusterK_Means)
##
## 1 2 3 4
## 1 10 0 0 0
## 2 1 6 11 1
## 3 0 13 0 0
## 4 0 0 0 5
Centroids <- kmeans_clu$centers
Centroids
## Pop_z Ill_z Life_z HSGrd_z Area_z
## 1 -0.05989929 1.3209363 -1.1889339 -1.48277738 -0.3184743
## 2 -0.47623707 -0.5808877 0.5647282 0.71185989 0.9463193
## 3 -0.28621520 -0.2345290 0.3560276 0.27995556 -1.2490645
## 4 2.14262730 -0.1522689 -0.2613111 -0.04904964 -0.1228566
Figure <- as.data.frame(Centroids)
Figure$id <- 1:nrow(Figure)
Figure <- pivot_longer(Figure, cols = c("Pop_z", "Ill_z", "Life_z", "HSGrd_z", "Area_z"))
Figure$Groups <- factor(Figure$id,
levels = c(1, 2, 3, 4),
labels = c("1", "2", "3", "4"))
Figure$nameFactor <- factor(Figure$name,
levels = c("Pop_z", "Ill_z", "Life_z", "HSGrd_z", "Area_z"),
labels = c("Pop_z", "Ill_z", "Life_z", "HSGrd_z", "Area_z"))
ggplot(Figure, aes(x = nameFactor, y = value)) +
geom_hline(yintercept = 0) +
theme_bw() +
geom_point(aes(shape = Groups, col = Groups), size = 3) +
geom_line(aes(group = id), linewidth = 1) +
ylab("Averages") +
xlab("Cluster variables") +
ylim(-2.5, 2.5)
On the above graph we can see differences among different groups of
states. For example, we can see that states in group four have on
average much higher population than states in other groups. Those states
in group four reflect average illiteracy, life expectancy, percentage of
high-school graduates and land area. We can also see that state’s
percentage of high-school graduate students is the lowest for states
clustered in group one. In this group there are states with the lowest
percentage of people with high-school education. In the same group are
also states with the highest percentage of illiterate people. Group two
includes states that have on average the highest percentage of people
with high-school education. This group has also the highest life
expectancy.
#Are all the cluster variables successful at classifing units into groups? Performing ANOVAs.
fit <- aov(cbind(Pop_z, Ill_z, Life_z, HSGrd_z, Area_z) ~ as.factor(ClusterK_Means),
data = mydata)
summary(fit)
## Response 1 :
## Df Sum Sq Mean Sq F value Pr(>F)
## as.factor(ClusterK_Means) 3 21.4951 7.1650 35.597 1.007e-11 ***
## Residuals 43 8.6551 0.2013
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Response 2 :
## Df Sum Sq Mean Sq F value Pr(>F)
## as.factor(ClusterK_Means) 3 26.202 8.7339 19.219 4.685e-08 ***
## Residuals 43 19.541 0.4544
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Response 3 :
## Df Sum Sq Mean Sq F value Pr(>F)
## as.factor(ClusterK_Means) 3 24.046 8.0154 14.857 8.858e-07 ***
## Residuals 43 23.198 0.5395
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Response 4 :
## Df Sum Sq Mean Sq F value Pr(>F)
## as.factor(ClusterK_Means) 3 33.324 11.1080 43.961 3.702e-13 ***
## Residuals 43 10.865 0.2527
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Response 5 :
## Df Sum Sq Mean Sq F value Pr(>F)
## as.factor(ClusterK_Means) 3 5.7444 1.91479 47.768 9.591e-14 ***
## Residuals 43 1.7237 0.04009
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Based on the p-values we can reject H0 (H0 = groups have the same means for each variable) (p < 0.001). -> This holds for all of the used variables! There are statistically significant differences between clusters regarding the used clustering variables.
aggregate(mydata$Inc_z,
by = list(mydata$ClusterK_Means),
FUN = "mean")
## Group.1 V1
## 1 1 -1.13871981
## 2 2 0.07715679
## 3 3 0.37211673
## 4 4 0.53466136
fit <- aov(Inc_z ~ as.factor(ClusterK_Means),
data = mydata)
summary(fit)
## Df Sum Sq Mean Sq F value Pr(>F)
## as.factor(ClusterK_Means) 3 17.31 5.771 12.02 7.61e-06 ***
## Residuals 43 20.65 0.480
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
aggregate(mydata$Murd_z,
by = list(mydata$ClusterK_Means),
FUN = "mean")
## Group.1 V1
## 1 1 1.0870557
## 2 2 -0.5757404
## 3 3 -0.6811152
## 4 4 0.5522538
fit <- aov(Murd_z ~ as.factor(ClusterK_Means),
data = mydata)
summary(fit)
## Df Sum Sq Mean Sq F value Pr(>F)
## as.factor(ClusterK_Means) 3 26.02 8.672 19.31 4.41e-08 ***
## Residuals 43 19.31 0.449
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
aggregate(mydata$Frst_z,
by = list(mydata$ClusterK_Means),
FUN = "mean")
## Group.1 V1
## 1 1 -0.7818614
## 2 2 0.4265336
## 3 3 0.2867140
## 4 4 -0.1018324
fit <- aov(Frst_z ~ as.factor(ClusterK_Means),
data = mydata)
summary(fit)
## Df Sum Sq Mean Sq F value Pr(>F)
## as.factor(ClusterK_Means) 3 11.06 3.686 4.864 0.00532 **
## Residuals 43 32.59 0.758
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
We performed validation with three numerical variables. P values indicate, that we can validate our results with INC_z (p < 0.001), Murd_z (p < 0.001) and FRST_z (p = 0.006).
For hierarchical clustering Ward’s algorithm was used. Based on the analysis of the dendrogram we decided to classify the states in four groups. Classification was further optimized using the K-Means cluster. We succeeded with the process by dividing states into four groups which have significant differences among each others.