#We give a trivial example for a block diagonal matrix perturbed by
#noise, for adapting clustering objectives of spectral clustering,
#and for balanced clustering.
# \donttest{
### TOY EXAMPLE
A <- matrix(c(2,1,1,3), 2, 2) # 2x2 block
B <- matrix(c(5,4,4,6), 2, 2) # 2x2 block
# Create a 5x5 zero matrix and insert blocks at right positions.
M <- matrix(0, 4, 4)
M[1:2, 1:2] <- A
M[3:4, 3:4] <- B
M.tilde <- M + matrix(rnorm(4^2, 0, 0.2), 4, 4)
#Construct a similaritiy matrix with same block structure
S <- cov2cor(t(M.tilde) %*% M.tilde)
bd <- bd.approx(S)
#Block diagonal approximation:
bd$B
#We can also permute the block diagonal shape:
S2 <- S[c(1, 3, 2, 4), c(1, 3, 2, 4)]
bd2 <- bd.approx(S2)
#bd2$B gives us again the block diagonal approximation
bd2$B
#And bd2$BD gives us the block diagonal approximation permuted to
#block diagonal shape
bd2$BD
### ADAPTING CLUSTERING OBJECTIVES
#We will use the USArrests example (see ?hcsvd).
data("USArrests")
USArrests["Maryland", "UrbanPop"] <- 76.6
D <- as.matrix(dist(USArrests))
S <- 1 - D / max(D)
#We compute k = 2 clusters adapting the objective of spectral clustering
#with the ratio cut.
bd.approx(S, linkage = "rcut")
### BALANCED CLUSTERING
#We can also enforce balanced clustering, such as two clusters of equal
#size (50:50). We will do this for the USArrests example from above.
bd.approx(S, linkage = "rcut", balance = 0.5)
# }
Run the code above in your browser using DataLab