Today, I'm excited to introduce you to my first R package: the D'Hondt simulator. It is a powerful new tool for political scientists, statisticians, and anyone interested in electoral systems. This user-friendly package allows users to simulate elections using the D'Hondt method, a highest averages method for allocating seats in party-list proportional representation.
View on GitHub
onurgitmez/dhondt
Installation
The dhondt package is available directly from GitHub and can be installed using the devtools package in R:
devtools::install_github("onurgitmez/dhondt")
Usage
Start by loading the package:
library(dhondt)
Simulating an Election
To simulate an election, you need a dataframe (data) that includes the electoral district name (district), the number of seats available in that district (seats), and the number of votes each party received (party1, party2, etc.).
Assuming the dataset is complete, you can simulate an election like this:
election_results <- simulate_election(
data,
"district",
"seats",
c("party1", "party2"),
threshold = 0.1
)
The threshold argument specifies the national threshold required for a party to be eligible for seats. If desired, the resulting dataframe can be assigned to a global environment variable, election_results for convenient access.
Function Details
When you run the simulation using the simulate_election() function, the calculations are performed in the background using the D'Hondt method. The function accepts the following arguments:
- df: Your dataframe with election data.
- district_col: The name of the column with district names.
- seats_col: The name of the column with the number of seats in each district.
- parties: A vector of party names, each matching a column in your dataframe holding the votes.
- threshold: (Optional) A vote share threshold that parties must exceed to be eligible. Default is 0.
Output
The function returns a list with two components:
- Total Seats Vector: A named vector displaying total seats won by each party (e.g.,
partyA = 15, partyB = 10). - District Breakdown: A dataframe showing the seats each party won in each district, including a summary row at the bottom.
This output provides both a bird's eye view and a detailed analysis of the election results, offering a comprehensive understanding of the electoral landscape.
For more information and a sample dataset shipped with the package, please visit the GitHub page linked above.