[R] Minimum Spanning Tree

jpearl01 joshearl1 at hotmail.com
Tue Apr 7 20:00:59 CEST 2009


Hi all, I'm very new to R and read a few tutorials, however I'm having
difficulty trying to figure out how to plot a minimum spanning tree.  I have
a csv file that contains an n-by-n matrix of distances between strains of
bacteria called matrix.csv.

Looks like:
id,strain1, strain2,strain3
strain1,0,.2,.8
strain2,.3,0,.7
strain3,.4,.6,0

I've been messing around with some information I've found on the web that
prints out an mst, however I think it does it with random values, instead of
values provided in a dataset (like from my csv file).  Here's what I tried
looks like:
x <- runif(100)
y <- runif(100)
nearest_neighbour <- function (x, y, d=dist(cbind(x,y)), ...) {
  n <- length(x)
  stopifnot(length(x) == length(y))
  d <- as.matrix(d)
  stopifnot( dim(d)[1] == dim(d)[2] )
  stopifnot( length(x) == dim(d)[1] )
  i <- 1:n 
  j <- apply(d, 2, function (a) order(a)[2])
  segments(x[i], y[i], x[j], y[j], ...)
}
plot(x, y,
     main="Nearest neighbour graph",
     xlab = "", ylab = "")
nearest_neighbour(x, y)

This gets the Nearest Neighbors, and then:

plot(x, y,
     main = "Minimum spanning tree",
     xlab = "", ylab = "")
nearest_neighbour(x, y, lwd=10, col="grey")
points(x,y)
library(ape)
r <- mst(dist(cbind(x, y)))
i <- which(r==1, arr.ind=TRUE )
segments(x[i[,1]], y[i[,1]], x[i[,2]], y[i[,2]],
         lwd = 2, col = "blue")

This prints the mst.  This would be perfect for me!  However I don't know
how to make this use my dataset... Any help (including links to helpful
tutorials!) would be awesome,

Thanks!
~josh



-- 
View this message in context: http://www.nabble.com/Minimum-Spanning-Tree-tp22934813p22934813.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list