This file contains 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
plot2 <- function(..., col_index, draw.axis = TRUE) { | |
col_list <- c("#e33518", "#0068a2", "#2b6632", "#9f4a00", "#00a5cb", "#43a347") | |
plot(..., pch = 21, col = col_list[col_index], bg = "white", cex = 2.0, axes = FALSE, xlab = "", ylab = "") | |
if (draw.axis) axis(1, lwd = 0, lwd.ticks = 1, las = 1, cex.axis = 1.4) | |
if (draw.axis) axis(2, lwd = 0, lwd.ticks = 1, las = 1, cex.axis = 1.4) | |
if (draw.axis) box(bty = "l") | |
} | |
## pdf("file_name.pdf", width = 8, height = 8, useDingbats = FALSE) | |
## plot2(x) | |
## dev.off() |
This file contains 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
kmeansp2 <- function(x, k, iter.max = 10, nstart = 1, ...) { | |
n <- nrow(x) # number of data points | |
centers <- numeric(k) # IDs of centers | |
distances <- matrix(numeric(n * (k - 1)), ncol = k - 1) # distances[i, j]: The distance between x[i,] and x[centers[j],] | |
res.best <- list(tot.withinss = Inf) # the best result among <nstart> iterations | |
for (rep in 1:nstart) { | |
pr <- rep(1, n) # probability for sampling centers | |
for (i in 1:(k - 1)) { | |
centers[i] <- sample.int(n, 1, prob = pr) # Pick up the ith center | |
distances[, i] <- colSums((t(x) - x[centers[i], ])^2) # Compute (the square of) distances to the center |