Professional Documents
Culture Documents
Ek125 Final Project
Ek125 Final Project
Since we're focusing on just the data that will allow us to predict the future presidential
elections, we don’t need all of the data. So to make the data table easier to work with, we are
going to write some code that will remove the unnecessary data. We were able to accomplish this
through the use of for loops and setting the unnecessary columns of data to [ ]. The data after
running the code can be seen in the below table.
Then we wrote a code to change the ‘Libertarian’ party to ‘Other’, this step simplified
our table and got rid of the non-popular parties that have no chance of winning the election.
Code:
Then we made a copy of the data we imported into a new variable so we could do some
different scrubbing to it. The next step was to make the code easier to work with, so we
converted the arrays in the party names column to strings. This was done by several simple for
loops that set the values equal to different ones. Then in the new data variable we created we
deleted all of the “other” parties since they never won an election once and were thus negligible.
Now we go to the meat of the code where the first step was to use the menu() function to
create a menu that allows the user to select from three options, 2024 Election Predictor,
Comparison, and State Data. After the menu was set up, we had to start setting up the code for
each option. The first step to code was when the value input was 0, this occurs when the user
clicks the “x” button to leave the menu before selecting an option. To just make the program a bit
more user friendly we decided on using the fprintf() function to send a message to the user
saying the program had been successfully closed.
The first big chunk of code is for the first option, the 2024 Presidential Election. To start
we needed to initialize two variables to act as counters that would be used to track the number of
states each party won. By setting them equal to 0 they would then be initialized. Next is a for
loop that goes from 1 to 616 in increments of 2 and is assigned to the variable “qq”. Initially we
tried doing it in increments of 1, but we needed to compare two races in each state and this didn’t
allow for it. So to solve the issue whenever the for loop went up one value, another variable
would be created that would have 1 added to it, this variable is “jj”. This then allowed us to have
new values of the two races in each state every time. Within the for loop there are a couple of
loops. The first one checks if the the number of votes for the party at “qq” is less than the
number of votes at “jj”. After this is another if loop that determines what party gets the point. We
initially messed it up by giving the point to the party with the lower total votes which resulted in
the wrong party getting the points. We solved this by switching it so that the losing party didn’t
get the points. The points were then assigned into the counter by simply just adding 1 to them.
Next we made another if loop to print the different results. If the Republican party won
then an fprintf function is used to print “Republicans are predicted to win the next election”.
Then if the Democrat party won, an fprintf function is used to print “ Democrats are predicted to
win the next election.
Now we're onto the next option provided by the menu function, Comparisons. This one is
significantly more complex than the predictor. Starting off we used several input functions and
fprintf functions to tell the user what they are using and ask them what data they would like to
compare. The user can enter a state's full name and then two separate dates and the code will
access the data and then compare the two years to determine the percent change, whether the
change was positive or negative, and what the votes were at the two points in time. We had quite
a bit of trouble here getting the code to access the correct data. It wasn’t so much an issue with
our code, but matlab was being finicky about copying and pasting. So we had to retype out
everything line by line and it ended up working. How the code does work is by initiating a for
loop through the whole data set, in each loop it first looks at the state name in the data of that
row and compares it to the user input state name. If the name is the same it then moves onto
another if loop that looks at the year for that row, if the year is the same as the user input year
then it moves onto the final if loop which determines what party it is. If the party is Republican
than the data for the number of votes that party got is assigned to a variable, if the party is
democrat it assigns the data to a different variable for Democrat data. After all of this the exact
same loop is initiated again but for the second date. Everything is assigned to different variables.
A simple calculation is run to get the percent change and the values are assigned to variables.
Then we used an if function that would print three different things depending on if the percent
change was positive, negative, or nothing.
The final option we decided on was to let users access the raw data of the dataset. Using
several input and fprintf functions the user is asked which state’s data they wish to access and
during which year. Then they are asked to confirm if the data they input is correct or not. If they
answer yes then a for loop and an several if loops like in the previous option are used to find the
correct data row. Then that row’s data is displayed by simply using the disp() function and
indexing into it. If the user enters no into the prompt asking them if to confirm their input then
the code loops back to the beginning and asks them for the data again, this will happen until the
user input “yes” and then the data will be displayed.
Project deliverables
Works Cited
James E. Campbell, “Forecasting the presidential election P»LTA,” presidential forecast, 2020.
[Online]. Available: https://unz.com/PDF/PERIODICAL/BrookingsRev-1996q4/28-34/.
[Accessed: 10-Dec-2022].
K. N. Jordan, “Examining long-term trends in politics and culture through ... - PNAS,” PNAS,
2018. [Online]. Available: https://www.pnas.org/doi/10.1073/pnas.1811987116. [Accessed:
10-Dec-2022].
R. S. Erikson, “Electoral College bias and the 2020 presidential election | PNAS,” PNAS, 2020.
[Online]. Available: https://www.pnas.org/doi/10.1073/pnas.2013581117. [Accessed:
10-Dec-2022].
UC Barbara, “Pool reports of November 29, 2022,” Pool Reports of November 29, 2022 | The
American Presidency Project, 29-Nov-2022. [Online]. Available:
https://www.presidency.ucsb.edu/documents/pool-reports-november-29-2022. [Accessed:
10-Dec-2022].
Graphs
Figure 1 - 13
Figure 1. Representation of total democratic and republican votes each year, republican in red,
democrat in blue
Figure 2. Line graphs representing the total votes in each state in each given year
Figure 3. Democrat and republican votes each year, from each state