Created
November 6, 2013 09:09
-
-
Save mihi-tr/7333122 to your computer and use it in GitHub Desktop.
Gist of https://github.com/mihi-tr/nrw13/blob/gh-pages/MDS.R for embedding...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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