Important to know

Question 1 (10 points)

For this assignment, you will be working with 2015 November Election Results from TUIK. Click here to download the data as an Excel document. Also, click here to download the assignment as an R Markdown document. You have to complete that document and return it to your instructor.

Your first task is to load the data into R. You are NOT allowed to change the XLS file in any way. All grading tests will be run on the original XLS file. If you assume a changed XLS file, than you risk losing full grades for the whole assignment.

Your Answer

# Load the excel data into R object rawData
rawData <- read_excel("secim.xlsx", range = "C1:W259", col_names = TRUE)

# Read and process the city names from the Excel file
cities <- read_excel("secim.xlsx", range = "A2:A259", col_names = FALSE)$X__1
for (i in 1:length(cities))
  if (is.na(cities[i]))
  {
    if (!is.na(cities[i - 1]))
      cities[i] <- cities[i - 1]
    else
      cities[i] <- cities[i - 2]
  }

type <- rep(c("Toplam", "Yurt içi", "Yurt Dışı"), length.out = length(cities))
rawData <- cbind(rawData, Cevre = cities, Tip = type)
rawData <- rawData[, c(ncol(rawData) - 1, ncol(rawData), 1:(ncol(rawData) - 2))]

str(rawData)

Question 2 (20 points)

Plot the election results (vote distribution as percentages among parties) for all votes. Your plot must display the parties in descending sorted order.

Your Answer

vec <- as.matrix(rawData[1, 7:23]) / rawData$`Geçerli oy sayısı`[1] * 100
ord <- order(vec, decreasing = T)
vec <- vec[, ord]
barplot(vec, 
        cex.names = 0.5, 
        las = 2, 
        main = "Türkiye Geneli Seçim Sonuçları", 
        ylim = c(0, 60),
        ylab = "Yüzde Oy Oranı")

Question 3 (20 points)

This time plot the percentages for “In country” and “Off Country” votes. Place them on the same plot, side by side to be able to see how parties are affected.

Your Answer

vec <- as.matrix(rawData[2:3, 7:23]) / rawData$`Geçerli oy sayısı`[2:3] * 100
ord <- order(vec[1,], decreasing = T)
vec <- vec[, ord]
barplot(vec, 
        cex.names = 0.5, 
        las = 2, 
        main = "Yurt İçi / Yurt Dışı Oy Oranları", 
        ylim = c(0, 60),
        ylab = "Yüzde Oy Oranı", 
        beside = TRUE)

Question 4 (30 points)

Draw a plot of each party’s number of winnings, i.e. the number of regions where they got the most votes among all parties. Only consider the total votes’ rows.

Your Answer

# Prepare the data for this question
qData <- as.matrix(rawData[(1:85) * 3 + 1, 7:23])

# Determine the winner in each region
winner <- numeric()
for (i in 1:nrow(qData))
{
  winner[i] <- which.max(qData[i, ])
}

# Count the number of winnings for each party
winCounts <- numeric()
for (i in 1:ncol(qData))
{
  winCounts[i] <- sum(winner == i)
}
names(winCounts) <- colnames(qData)
winCounts <- sort(winCounts, decreasing = T)

# Do the plot: apply a barplot for best exposition
barplot(winCounts, ylim = c(0, 75), xlab = "Parti", ylab = "Kazanılan bölge", 
        main = "Partilerin kazandığı bölge sayıları", las = 2, cex.names = 0.5)

Question 5 (30 points)

In this question you will be comparing the performances of top 4 parties, head to head.

Your Answer

wdf <- rawData[rawData$Tip == "Toplam", c("AK PARTİ", "CHP", "MHP", "HDP")] / rawData[rawData$Tip == "Toplam", "Geçerli oy sayısı"] * 100
par(mfrow = c(1, 3))

for (i in 1:4)
  for (j in 1:4)
    if (i != j)
    {
      plot(wdf[[i]], wdf[[j]], 
           xlab = colnames(wdf)[i], ylab = colnames(wdf)[j])
      abline(a = 0, b = 1, col = "blue", lwd = 2)
      abline(a = 0, b = .5, col = "green", lwd = 2)
      abline(a = 0, b = 2, col = "red", lwd = 2)
      abline(a = 20, b = -1, col = "orange", lwd = 2)
    }