forked from mlandauer/vote-preferencing
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mds.R
45 lines (39 loc) · 1.26 KB
/
mds.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Run this within R with:
# source("mds.R")
library("maptools")
library("MASS")
# Graph and write to file as svg
graph <- function(fit, d2, label, filename) {
svg(filename, width=7, height=7)
x <- fit$points[,1]
y <- fit$points[,2]
plot(x, y, col="red", main=label, xlab="", ylab="", xaxt='n', yaxt='n', frame.plot=FALSE)
pointLabel(x, y, labels = row.names(d2), cex=1, xpd=TRUE)
dev.off()
}
calculate <- function(d) {
# Make the matrix symmetric. Simplistic - puts equal weight on preferencing in both directions
# Also make matrix from list
return(isoMDS(do.call(rbind, d + t(d)), k=2))
}
process <- function(state, label) {
d = read.table(sprintf("output/distance_%s.dat", state), header=TRUE)
fit <- calculate(d)
graph(fit, d, label, sprintf("output/%s.svg", state))
write.csv(fit$points, sprintf("output/%s-coords.csv", state))
}
# process("act", "ACT")
# process("nsw", "NSW")
# process("nt", "NT")
# process("qld", "QLD")
# process("sa", "SA")
# process("tas", "TAS")
# process("vic", "VIC")
process("wa", "WA")
# # Process the example data
# d = read.table("example.dat", header=TRUE)
# fit <- calculate(d)
# graph(fit, d, "", "output/example.svg")
# write.csv(fit$points, "output/example-coords.csv")
# # Recalculate distances
# d2 = dist(fit$points)