Introduction
The aim of this work is to analyze demographic and social factors
differentiating the voters of the two largest Polish political parties -
Law and Justice (PiS) and Civic Coalition (KO). Knowing the specificity
of these two parties, the selected variables should allow for the
verification of their policies. Beyond mere differentiation, this
analysis aims to uncover the core factors that set these parties apart
and illuminate the specific social demographics drawn to their political
approaches. For the parties, it may also be an indication of what they
can focus on in order to gain greater electoral support.
For this purpose, various unsupervised learning algorithms were used
in this study. The first focus was on social factors, performing
clustering using K-means and PAM and dimension reduction using MDS and
PCA. By initially concentrating on social factors, the study engages in
clustering and dimension reduction techniques, extending its examination
to Polish regions. This analysis seeks to discern the root causes that
contribute to their unique political landscapes.
Dataset description
This database was built by the author on the basis of data from the
Polish Central Statistical Office of Government Administration - GUS and from
the official source of 2023 election results - GOV.
The database consists of 16 observations and 15 variables.The
observation units are the regions of Poland’s administrative division.
In addition to the voting results for the two most important political
parties, other variables include various demographic factors that
influence citizens’ voting decisions. Apart from data about 2023
election, other variables are from year 2022 (last available on
GUS).
Variables
The data set consists of the following variables:
- Population density - population per 1 kilometer;
- Unemployment rate - percentage of people in the labour force who are
unemployed;
- Life expectancy(men) - number of years a person can expect to live
(among men);
- Beds in hospitals - beds in hospitals (per 10,000 people);
- Nurseries - percentage of children cared for in nurseries
- Natural increase rate - difference between the birth rate and the
death rate (per 1,000 people)
- Disposable income - amount of money that an individual has to spend
or save after income taxes in relation to the national average (Poland =
100)(percentage)
- Social benefits - people who were granted social assistance benefits
(per 10,000 people)
- Gross salary - average monthly gross wages in relation to the
national average (Poland = 100)(percentage)
- Pensioners - percentage of retirees and pensioners in the
population
- Urbanization - percentage of population living in urban areas
- Founds form EU - value of contracts for co-financing from EU funds
per capita (Poland = 100)
- Voter turnout - participation rate of Poland’s 2023 election
- Voting for PiS - percentage of votes cast for party “Law and
justice”
- Voting for KO - percentage of votes cast for party “Citizen
Coalition”
Libraries
library(clusterSim)
library(factoextra)
library(FeatureImpCluster)
library(flexclust)
library(dendextend)
library(psych)
library(smacof)
library(gridExtra)
library(hopkins)
library(sf)
library(ggplot2)
library(dplyr)
#devtools::install_github("o1iv3r/FeatureImpCluster")
Dataset preparation
DATA <- read.csv("Poland.csv", header=TRUE, sep=",", dec=".")
rownames(DATA)<-DATA[,2]
DATA <- DATA[,c(-1,-2,-3,-19,-20)]
#normalization of data
DATA.new <- data.Normalization(DATA, type="n1", normalization="column")
Analysis of social factors
Hopkins statistics
Veryfing how well the data can be clustered, can be conducted using
Hopkin’s statistic. Null hypothesis: dataset does not contain meaningful
clusters. Alternative hypothesis: dataset constains meaningful
clusters.
Calculated values 0-0.3 indicate regularly-spaced data. Values around
0.5 indicate random data. Values 0.7-1 indicate clustered data.
hopkins(t(DATA.new), m=nrow(DATA.new)/10)
## [1] 0.9941266
The value is close to 1, which means that data is clusterable.
Using another function we will verify clustering tendency. We again
have Hopkin’s statistic - if the value of is close to 1 (far above 0.5),
then we can conclude that the dataset is significantly clusterable.
get_clust_tendency(t(DATA.new), 10, graph=TRUE, gradient=list(low="brown1", mid="snow", high="royalblue3"), seed = 123)
## $hopkins_stat
## [1] 0.5832039
##
## $plot

These values indicate the possibility of clustering but not very
significant. On the plot we see two clearer patterns.
Optimal number of clusters
fviz_nbclust(t(DATA.new), FUNcluster=kmeans, method="silhouette")
fviz_nbclust(t(DATA.new), FUNcluster=kmeans, method="gap_stat")

We will only do 2 clusters, but in this case it may be even more useful
because we want to analyze the two most influential political
parties.
K-means and PAM clustering for social factors
km<-eclust(t(DATA.new), "kmeans", hc_metric="euclidean", k=2)
pam<-eclust(t(DATA.new), "pam", hc_metric="euclidean", k=2)
fviz_cluster(km, main="K-means / Euclidean", xlim=c(-5, 5), ylim=c(-5, 5), labelsize=10, repel=TRUE)
fviz_cluster(pam, main="PAM / Euclidean", xlim=c(-5, 5), ylim=c(-5, 5), labelsize=10, repel=TRUE)


What is the most important about these results is that we see two
very different clusters, each of which includes one political party.
These clusters define the attributes of their electorates.
The results are in line with intuition, as Party PiS adheres to
traditional views, and targets its policies to the elderly and those of
lower socioeconomic status. It is apparent that the party’s group has a
higher share of pensioners. PiS introduced a social benefit program, and
the people to whom the benefits went also seem more likely to be the
party’s voters in 2023. Another aspect is the unemployment rate - in
regions where it is higher, more people vote for PiS. This may be due to
the welfare state policies that the party pursue, as well as greater
support in villages and smaller urban centers, where the unemployment
rate may be higher.
People from regions with higher urbanization rates and higher
population densities, as well as those with higher economic status, are
more likely to vote for the Civic Coalition party. What is interesting
is that the goal of Party Law and Justice’s main social benefit program
was to increase the birth rate, and in regions where it is higher,
people were more likely to vote for KO.
It is also interesting to note that voter turnout was higher in
regions more likely to vote for KO. This may confirm that the motivation
to change power in these particular regions influenced the outcome of
the 2023 election and the removal of the ruling party from power.
We can examine the similarity between clustering using K-means and
PAM.
Rand Index and Jaccard similarity - comparing K-means and
PAM
km<-eclust(t(DATA.new), "kmeans", hc_metric="euclidean",k=2)
pam<-eclust(t(DATA.new), "pam", hc_metric="euclidean",k=2)
comPart(km$cluster, pam$cluster)
## ARI RI J FM
## 1 1 1 1
Overlap is 100% which means that both K-means and PAM give the same
results of clustering.
Now let’s see how well the data is clustered using Silhouette
plot.
fviz_silhouette(km)
## cluster size ave.sil.width
## 1 1 9 0.25
## 2 2 6 0.30
fviz_silhouette(pam)
## cluster size ave.sil.width
## 1 1 9 0.25
## 2 2 6 0.30


There are no negative silhouette values, which means the clustering
is correct.
Additionally we can examine which regions are the most important for
clustering.
Variable importance for K-mans
km<-kcca(t(DATA.new), k=2)
FeatImpKmeans<-FeatureImpCluster(km, as.data.table(t(DATA.new)))
plot(FeatImpKmeans)

Śląsk has a substantial impact on the formation of clusters within
this dataset. Let’s analyse the dispersion within clusters.
d1<-cclust(DATA.new, 2, dist="euclidean")
stripes(d1)

It is hard to decide which cluster is more dispersed, which could
give the information about whose electorate is more diverse.
Hierarchical clustering for social factors
dend.f<-hcut(t(DATA.new), k=5, stand=FALSE)
dend.f$cluster
## Population.density Unemployment.rate
## 1 2
## Life.expectancy.men. Beds.in.hospitals
## 3 4
## Nurseries Natural.increase
## 5 3
## Disposable.income Social.benefits
## 1 2
## Gross.salary Pensioners
## 1 4
## Urbanization Founds.from.EU
## 5 2
## Voter.turnout Voting.for.Law.and.Justice
## 1 4
## Voting.for.Civic.Coalition
## 5
fviz_dend(dend.f, rect=TRUE, horiz=TRUE, labels_track_height=10)

This dendrogram give us a clearer view of the clustering. We can
again conclude that the electorate of KO is most influenced by the
percentage of population living in urban areas and percentage of
children cared for in nurseries. Whereas electorate of PiS is most
influenced by the percentage of retirees and pensioners in the
population and access to health care (expressed in the availability of
hospital beds).
fviz_silhouette(dend.f)
## cluster size ave.sil.width
## 1 1 4 0.22
## 2 2 3 0.36
## 3 3 2 0.53
## 4 4 3 0.19
## 5 5 3 0.20
fviz_cluster(dend.f)


Even though this time instead of 2 clusters we have 5, there is no
negative value of silhouette.
MDS - Multidimensional scaling for social factors
dist1<-dist(t(DATA.new))
fit.data<-mds(dist1, ndim=2, type="ratio")
fit.data
##
## Call:
## mds(delta = dist1, ndim = 2, type = "ratio")
##
## Model: Symmetric SMACOF
## Number of objects: 15
## Stress-1 value: 0.224
## Number of iterations: 55
plot(fit.data, plot.type = "stressplot")
Stress point Stress points give the information about
goodness-of-fit in MDS, usually expressed as a percentage. It ranges
from 0% to 100%, where lower values indicate a better fit between the
observed dissimilarities and the distances in the MDS solution.
Values: 0.20 = poor, 0.10 = fair, 0.05 = good, 0.025
= excellent, 0.00 = perfect. In this data set most variables are between
10 and 4 % which indicates rather good quality of the model. There are
no clear outliers. Stress-1 value close = 0.224 suggests a relatively
decent fit for the MDS.
par(mar=c(5,5,4,2))
plot(fit.data, pch=21, cex=as.numeric(fit.data$spp), bg="lightsteelblue", main="MDS for selected variables", xlim=c(-1.2, 1.2), ylim=c(-0.8, 0.8))

With the help of the MDS, we can better see what factors are more
correlated with each other and what factors are far from each other. We
still see the division into two main clusters. An interesting
observation more visible on this perceptual map is that voting for
Citizen Coalition is highly correlated with the level of urbanization
and access to nurseries.
This data and chart also confirm the trend that people with higher
economic status are more likely to participate in elections. We can
deduce this by the fact that “Voter turnout” indicator is very close to
“Gross salary” on MDS perceptual map.
PCA - principal component analysis for social factors
pca.f<-principal(DATA.new, nfactors=4, rotate="varimax")
print(loadings(pca.f), digits=3, cutoff=0.4, sort=TRUE)
##
## Loadings:
## RC3 RC2 RC1 RC4
## Nurseries -0.637 0.426
## Urbanization -0.693 0.575
## Voting.for.Law.and.Justice 0.940
## Voting.for.Civic.Coalition -0.951
## Life.expectancy.men. 0.490 -0.741
## Beds.in.hospitals 0.834
## Natural.increase -0.913
## Pensioners 0.917
## Population.density 0.687 0.412
## Unemployment.rate 0.463 -0.684
## Social.benefits -0.895
## Founds.from.EU -0.824
## Disposable.income 0.883
## Gross.salary 0.476 0.728
## Voter.turnout 0.456 0.747
##
## RC3 RC2 RC1 RC4
## SS loadings 3.412 3.281 3.165 2.695
## Proportion Var 0.227 0.219 0.211 0.180
## Cumulative Var 0.227 0.446 0.657 0.837
SS loadings - indicate the strength and direction of
the relationship between the original variables and the principal
components. Larger SS loadings indicate that the principal component
captures more variance in the original data. We can see that RC3
captures the most variance.
Cumulative variance - gives a sense of how much of
the total variance is explained as we include more principal components.
In this case, the first two components (RC3 and RC2) together explain
45% of the variance, which seems a good score. Total variance explained
is 83%.
pca2<-princomp(DATA.new)
fviz_pca_var(pca2, col.var="lightcoral", repel=TRUE, ylim=c(-1,1), xlim=c(-1,1))

On this perceptual map positively correlated variables are grouped
together. Negatively correlated variables are positioned on opposite
sides of the plot origin. We can see that there is a contrary relation
between Voting for PiS and Urbanization rate or nurseries. Other
connections confirm that this method works well - for example Gross
salary and Unemployment rate, Natural increase and population of
Pensioners.. Interestingly there is a contrary relation between Voter
turnout and Unemployment rate or people granted with Social
benefits.
fviz_eig(pca2)

We can see the percentage of explained variance on y-axis of this
plot.
var<-get_pca_var(pca2)
a<-fviz_contrib(pca2, "var", axes=1, xtickslab.rt=90) # default angle=45°
b<-fviz_contrib(pca2, "var", axes=2, xtickslab.rt=90)
gridExtra::grid.arrange(a,b,top='Contribution to the first two Principal Components')

Here we can verify which variables contribute the most to our
principal components. For dimension 1 - unemployment rate, gross salary
and voter turnout. For dimension 2 - life expectancy, natural increase
and percentage of pensioners.
Analysis of regions
In this part, the same analysis was carried out, but this time the
goal was to examine the differences and similarities between the regions
of Poland.
Hopkin’s statistics
hopkins(DATA.new, m=nrow(DATA.new)/10)
## [1] 0.9997634
get_clust_tendency(DATA.new, 10, graph=TRUE, gradient=list(low="brown1", mid="snow", high="royalblue3"), seed = 123)
## $hopkins_stat
## [1] 0.5705516
##
## $plot

Again there is a possibility of clustering but not very significant.
On the plot we don’t see clear patterns.
Optimal number of clusters
fviz_nbclust(DATA.new, FUNcluster=kmeans, method="silhouette")
fviz_nbclust(DATA.new, FUNcluster=kmeans, method="gap_stat")


I decided to make 3 clusters instead of four. This decision was
promped by the occurrence of a negative silhouette value in 4
clusters.
K-means clustering for regions
km<-eclust(DATA.new, "kmeans", hc_metric="euclidean", k=3)
fviz_cluster(km, main="K-means / Euclidean", xlim=c(-5, 5), ylim=c(-5, 5), labelsize=10, repel=TRUE)
fviz_silhouette(km)
## cluster size ave.sil.width
## 1 1 4 0.23
## 2 2 6 0.15
## 3 3 6 0.24


For regions analysis I choose to do only K-means clustering. In 3
clusters there are no negative sillhouette values, so the clustering
seems well prepared.
Hierarchical clustering for regions
dend.f<-hcut(DATA.new, k=3, stand=FALSE)
dend.f$cluster
## DOLNOŚLĄSKIE KUJAWSKO-POMORSKIE ŁÓDZKIE LUBELSKIE
## 1 2 1 2
## LUBUSKIE MAŁOPOLSKIE MAZOWIECKIE OPOLSKIE
## 1 3 3 1
## PODKARPACKIE PODLASKIE POMORSKIE ŚLĄSKIE
## 2 2 3 1
## ŚWIĘTOKRZYSKIE WARMIŃSKO-MAZURSKIE WIELKOPOLSKIE ZACHODNIOPOMORSKIE
## 2 2 3 1
fviz_dend(dend.f, rect=TRUE, horiz=TRUE, labels_track_height=10, k_colors = c("dodgerblue3", "indianred3", "palegreen3"), # Change line colors
color_labels_by_k = TRUE)

Knowing the specifics of Polish regions, this clustering result seems
interesting and consistent with the frequent historically determined
division. Red cluster points to cities that are the largest urban
centers in terms of economy, industry and the scientific world. Blue
cluster presents the regions of Poland’s Eastern Bloc, which were under
Russian occupation for a long time in history. On the other hand, the
green cluster presents the regions of Poland’s Western Bloc, which was
under Prussian occupation. These regions differ in terms of types of
their economy and agriculture, which, as this analysis shows, also
differentiates these regions in terms of demographic and social factors.
This, in turn, means that historical conditions still have a major
impact on Polish societies and their distinctive cultural and economic
identity.
DATA2 <- read.csv("Poland.csv", header=TRUE, sep=",", dec=".")
DATA2 <- DATA2[,c(2,19,20)]
map_poland <- st_read("/Users/emiliaselwa/Desktop/PL/pol_adm_gov_v02_20220414_SHP/pol_admbnda_adm1_gov_v02_20220414.shp")
map_poland$ADM1_PL <- toupper(map_poland$ADM1_PL)
merged_data <- merge(map_poland, DATA2, by.x = "ADM1_PL", by.y = "Region")
ggplot() +
geom_sf(data = merged_data, aes(fill = Color), color = "white", size = 0.2) +
scale_fill_identity() +
theme_minimal()

The map above presents described relationships and division of
regions according to demographic and social factors. It can be noticed
that the agglomeration centers marked in red, separate two parts of the
country which have different demographic characteristics.
Variable importance for K-mans
km.f<-kcca(DATA.new, k=3)
FeatImpKmeans<-FeatureImpCluster(km.f, as.data.table(DATA.new))
plot(FeatImpKmeans)

This plot enables to recognize which variables had the biggest impact
on the formation of clusters of regions. We see that voter turnout and
voting for both PiS and KO, were important factors for the analysis.
Urbanization, unemployment rate, and disposable income, also had a
significant influence for the clusters.
MDS - Multidimensional scaling for regions
dist1<-dist(DATA.new)
fit.data<-mds(dist1, ndim=2, type="ratio")
fit.data
##
## Call:
## mds(delta = dist1, ndim = 2, type = "ratio")
##
## Model: Symmetric SMACOF
## Number of objects: 16
## Stress-1 value: 0.208
## Number of iterations: 47
plot(fit.data, plot.type = "stressplot")

Most regions vary between 10 and 4% of stress proportion, which
indicates rather good quality of the model. Two regions - Opolskie and
Śląskie seem to apper as outliers. Stress-1 value = 0.208 suggests a
relatively decent fit for the MDS.
par(mar=c(5,5,4,2))
plot(fit.data, pch=21, cex=as.numeric(fit.data$spp), bg="lightsteelblue", main="MDS for regions", xlim=c(-1.2, 1.2), ylim=c(-0.8, 0.8))

The significance of Śląskie region in variable clustering suggests
its unique importance in the broader regional context. On this
perceptual map we see that Śląskie is situated visibly further than
other regions. This region has been historically associated with heavy
industry, coal mining, and metallurgy. The industrialization of the
region may have shaped its economic landscape differently compared to
other parts of Poland. Economic activities and the presence of specific
industries can have a profound impact on the demographic composition and
social dynamics of a region.
Conclusions
This study focused on analyzing demographic and social factors that
differentiate voters of the two major Polish political parties - Law and
Justice (PiS) and Civic Coalition (KO). Utilizing unsupervised learning
algorithms such as K-means, PAM, MDS, and PCA, the research identified
distinct clusters corresponding to each political party.
The findings align with expectations, revealing that PiS attracts
predominantly elderly individuals and those with lower economic status.
The party’s social benefit program, targeted at this demographic, seems
to have influenced electoral support. On the other hand, KO garners
support from regions characterized by higher urbanization, population
density, and economic status. Notably, the higher voter turnout in
regions favoring Civic Coalition suggests a motivation for change,
influencing the outcome of the 2023 election and the removal of the
ruling party from power. These results provide valuable insights for
both political parties to understand their electorate and tailor their
strategies for greater electoral success.
The clustering results revealed intriguing patterns that align with
historical divisions in the country. Historical contexts contribute to
variations in economic types, agriculture, and, consequently,
demographic and social factors. The analysis underscores the enduring
impact of historical conditions on Polish society. Notably, voter
turnout, support for PiS and KO, urbanization, unemployment rate, and
disposable income emerged as crucial factors shaping the clusters.