Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jstray
Created September 18, 2012 04:36
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 jstray/3741280 to your computer and use it in GitHub Desktop.
Save jstray/3741280 to your computer and use it in GitHub Desktop.
Distance function computation for UK House of Lords voting analysis
# -------------------------------- Compute distances ------------------------------
# distance function = 1 - fraction of votes where both voted, and both voted the same
votedist <- function(v1, v2) {
overlap = v1!=0 & v2!=0
numoverlap = sum(overlap)
match = overlap & v1==v2
nummatch = sum(match)
if (!numoverlap)
dist = 1
else
#dist = 1- ((nummatch/numoverlap) * log(numoverlap)/log(Nvotes))
dist = 1- (nummatch/numoverlap)
dist
}
# create distance matrix
d = dist(recentvotes, votedist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment