There’s an interesting blog post up on DataTarik about using random forests to model the outcomes of Malaysia’s 14th general election (GE14). While I have my reservations about their machine learning approach, I think it’s an interesting dataset, so I downloaded their data and code and played around with it in Python.
For those not familiar with Malaysia, some background:
- BN refers to the National Front (Barisan Nasional), the current largest opposition party and the ruling party since independence, and PH refers to Pact of Hope (Pakatan Harapan), the party currently in government. I sometimes refer to Gagasan Sejahtera as PAS (Malaysian Islamic Party), as they were the leading party in this electoral coalition.
- Melayu, Cina and India refer to ethnic Malays, Chinese and Indians respectively, whereas Bumiputera Sabah/Sarawak and Orang Asli are various indigenous tribes in Malaysia. Malays are also regarded as indigenous, and will be referred to as such.
- Geographically, Malaysia comprises of parts of the Malay Peninsula and Borneo island (West and East Malaysia respectively).
Variable names: agevars
refers to proportions of people in several bins of age groups, racevars
refers to proportions of people in each ethnic group, and features
is the union of agevars
and racevars
. target
is the outcome of the election.
Who won?
Thought I’d just do a quick waffle chart using the sweet pywaffle
package.
from pywaffle import Waffle
= ge14.target.value_counts()
data = plt.figure(
fig =Waffle,
FigureClass=10,
rows=data,
values=("red", "#232066", "green", "lightblue", 'grey', 'cyan'),
colors={'label': '14th Malaysian Parliament composition', 'loc': 'left'},
title=["{0}".format(k) for k, v in data.items()],
labels= 'user',
icons ={'loc': 'upper left', 'bbox_to_anchor': (1, 1)}
legend
)'#EEEEEE')
fig.gca().set_facecolor('#EEEEEE')
fig.set_facecolor(False)
fig.set_tight_layout( plt.show()
Overall distribution of demographics across constituencies
With the data we have, there are two stories to be told:
- Voting behaviour by ethnicity:
- Gagasan Sejahtera does well in constituencies with high Malay concentration, low Chinese and Indian concentration.
- We may want to look at geographical subgroups (Peninsula/Borneo), since the Borneo parties don’t contest in the Peninsula, and some of the ethnic groups covered are sparse in the Peninsula.
- For BN, the distribution of wins by ethnicity are more tightly distributed around low Indian and Chinese population constituencies.
- Both BN and PH have wins across a range of Malay-minority and Malay-majority constituencies. BN does not have any seats in Chinese-majority constituencies.
- Voting behaviour by age:
- Not very clear pattern for age - at least not with the boxplot
- PAS wins in ‘younger’ constituencies (higher pct 21-30, 31-40)
- PAS wins are very uniform in terms of age and ethnicity demographics
= plt.subplots(nrows =4, figsize = (6,20))
fg, ax =='BN'][features].boxplot(vert = False, ax = ax[0])
ge14[ge14.target 0].set_title("BN")
ax[=='PH'][features].boxplot(vert = False, ax = ax[1])
ge14[ge14.target1].set_title("PH")
ax[=='Gagasan Sejahtera'][features].boxplot(vert = False, ax = ax[2])
ge14[ge14.target2].set_title("Gagasan Sejahtera")
ax[~ge14.target.isin(['BN', 'PH', 'Gagasan Sejahtera'])][features].boxplot(vert = False, ax = ax[3])
ge14[3].set_title("Others")
ax[
plt.tight_layout()
In which seats do these parties win?
Ethnic diversity of constituencies
Using scipy
‘s entropy
function, I try to determine which parties win in ethnically ’diverse’ and ‘less-diverse’ constituencies. The boxplots below confirm what we already know from the boxplots above - that Gagasan Sejahtera appeals to a very homogenous group of voters.
- BN wins across diverse and non-diverse constituencies;
- PH also wins across diverse and non-diverse constituencies, but its median constituency is more diverse than BN’s median constituency.
- PAS wins in non-diverse constituencies.
The diversity index used here is the Shannon entropy index, given by $$H'=-\Sigma p_i \log{p_i}$$
The exponent of $H'$
has a nice interpretation. It is the effective number of species, i.e. how many species there would be if each species had an equal amount of observations. You can convince yourself that this is true by running the code exp(-(0.5*log(0.5)+0.5*log(0.5)))
(in R), which should return the result 2, meaning the diversity in the data is effectively two equally sized classes.
from scipy.stats import entropy
= np.array([entropy(ge14[racevars].iloc[i,:].tolist()) for i in range(len(ge14))])
entropy_const
# use effective number of species instead https://medium.com/@sam.weinger/how-diverse-are-names-in-america-f74b07e031bd
= np.exp(entropy_const)
effective_number
=='BN']), np.mean(effective_number[ge14.target=='PH']), np.mean(effective_number[ge14.target=='Gagasan Sejahtera']) np.mean(effective_number[ge14.target
(2.023986123800037, 2.4850578347860295, 1.2839901761045327)
= plt.subplots(nrows = 3, figsize = (5,5))
fg, ax =='BN'], ax = ax[0])
sns.boxplot(effective_number[ge14.target0].set_title("BN")
ax[
=='PH'], ax = ax[1], color = 'r')
sns.boxplot(effective_number[ge14.target1].set_title("PH")
ax[=='Gagasan Sejahtera'], ax = ax[2], color = 'g')
sns.boxplot(effective_number[ge14.target2].set_title("Gagasan Sejahtera")
ax[0,4)
plt.xlim( plt.tight_layout()
Peninsula-Borneo divide?
Since there are different parties in Peninsula and Borneo, I calculate age-ethnicity cross-tabulations for Peninsula and Borneo respectively. Age and ethnicity here refers to the demographic with the highest share in each constituency.
Peninsula
PH wins all predominantly ethnic Chinese seats and most predominantly ethnic Malay seats in the Peninsula.
= ge14[racevars].idxmax(axis = 1)
ethnicity = ge14[agevars].idxmax(axis = 1)
age
pd.crosstab(ge14[peninsula].target, ethnicity)
Cina (%) | Melayu (%) | |
---|---|---|
BN | 0 | 49 |
Gagasan Sejahtera | 0 | 18 |
PH | 33 | 65 |
pd.crosstab(ge14[peninsula].target, age)
21 - 30 (%) | 31 - 40 (%) | 41 - 50 (%) | 51 - 60 (%) | |
---|---|---|---|---|
BN | 2 | 47 | 0 | 0 |
Gagasan Sejahtera | 3 | 15 | 0 | 0 |
PH | 0 | 77 | 11 | 10 |
Borneo
BN is strongest in predominantly-indigenous seats, whereas PH is strongest in predominantly ethnic Chinese seats. PH made up for its weakness in indigenous seats with its partnership with WARISAN. Both BN and PH win the most seats where most people are aged between 31 and 40.
pd.crosstab(ge14[np.invert(peninsula)].target, ethnicity)
Bumiputera Sabah (%) | Bumiputera Sarawak (%) | Cina (%) | Melayu (%) | |
---|---|---|---|---|
BEBAS | 0 | 2 | 0 | 0 |
BN | 10 | 14 | 0 | 6 |
PH | 3 | 4 | 8 | 1 |
STAR | 1 | 0 | 0 | 0 |
WARISAN | 7 | 0 | 0 | 1 |
pd.crosstab(ge14[np.invert(peninsula)].target, age)
31 - 40 (%) | 41 - 50 (%) | 51 - 60 (%) | |
---|---|---|---|
BEBAS | 0 | 2 | 0 |
BN | 17 | 11 | 2 |
PH | 8 | 5 | 3 |
STAR | 1 | 0 | 0 |
WARISAN | 8 | 0 | 0 |