Skip to content

Instantly share code, notes, and snippets.

@mihi-tr
Created November 6, 2013 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mihi-tr/7333122 to your computer and use it in GitHub Desktop.
Save mihi-tr/7333122 to your computer and use it in GitHub Desktop.
# Formulas for Z scores
zscore <- function (d) {
return ((d-mean(d))/sd(d))
}
# Read in data
d <- read.csv("Cleaned-20131016-1525.csv")
# The parties we want to use
parties<-colnames(d)[c(6,8,9,10,11,12,13,14,15,16)]
# Create the "Other party column"
parties<-c(parties,"Sonstige")
# The parties in Other Parties
sonstige<-colnames(d)[c(17:21)]
# Calculate votes for "Other Parties" - trouble due to NA's in the data...
d[["Sonstige"]]<-Reduce(function(x,y) { i=d[[y]];
i[is.na(i)]<-0 ;
return (x+i) }, sonstige, 0);
# Normalize votes using Z-Scores of the percentage of votes
normalized<-sapply(parties,function(x) { return (zscore(d[[x]]/d[["Abgegebene"]])) })
# This is where linear algebra magic happens:
# Perform Multidimensional Scaling to get 2 dimensional coordinates
c<-cmdscale(dist(normalized))
# Build the return object...
r<-d[,c(1:16,23)]
# Add the coordinates in two columns...
r[["x"]]<-c[,1]
r[["y"]]<-c[,2]
# Save the file!
write.csv(r,"mds.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment