1 #various util functions for the main program
3 #preliminary: replace NA's by averaging over each serie's values
4 #TODO: find a better way to handle missing values
5 replaceNAs = function(M)
12 avg = mean(M[i,1:(m-2)] [!is.na(M[i,1:(m-2)])])
13 res[i,1:(m-2)] [is.na(M[i,1:(m-2)])] = avg
18 #standardize matrix M (remove mean, divide by standard deviation)
19 standardize = function(M)
21 avgM = colMeans(M, na.rm = TRUE)
22 stdevs = sqrt( unlist( apply(M, 2, var, na.rm=TRUE) ) )
25 return (list("M"=res,"avg"=avgM,"stv"=stdevs))
28 #opposite of the previous function: get back M from standardized form
29 destandardize = function(std)
37 #remap neighbors into some connex component
38 remapNeighbors = function(NI, indices)
40 revIndices = rep(NA, length(NI))
43 revIndices[ indices[ii] ] = ii
44 locNI = list("ix"=as.list(rep(NA,nc)), "ds"=as.list(rep(NA,nc)))
47 locNI$ix[[ii]] = revIndices[ NI$ix[[ indices[ii] ]] ]
48 locNI$ds[[ii]] = NI$ds[[ indices[ii] ]]
53 #check graph connexity
54 getConnectedComponents = function(NIix)
56 return (.Call("getConnectedComponents", NIix));
59 #auxiliary function to display clustering information
60 promptForMapDisplay = function(stage, coordsM, NIix=NULL, clusters=NULL)
62 if (is.null(clusters))
63 clusters = rep(1, nrow(coordsM))
66 if (stage == "interm")
67 shouldDisplay = readline(">>> show intermediate map of neighborhoods ? (y/n)\n")
68 else if (stage == "final")
70 shouldDisplay = readline(
71 ">>> show final map of clusters ? (y/n) \
72 NOTE: can be plotted later, see '? drawMapWithSites'\n")
75 if (shouldDisplay == "y")
77 drawMapWithSites(coordsM, clusters)
79 drawNeighborhoodGraph(coordsM,NIix)
80 print("Please press 'enter' to continue")
82 if (!is.null(dev.list()))