# Read raw data from population growth rdata
# with countries over million inhabitants
data("popgrowthmillion")
# \dontshow{
popgrowthmillion <- popgrowthmillion[1:10, 1:50]
# }
# Create centred data
m <- apply(popgrowthmillion, 2, mean) # Country-wise means
cpopgrowthmillion <- popgrowthmillion
for(i in 1:dim(popgrowthmillion)[1]) {
cpopgrowthmillion[i,] <- popgrowthmillion[i,] - m
}
# Create scaled data
t2 <- function(v) { sqrt(sum(v^2)) }
s <- apply(cpopgrowthmillion, 2, t2)
spopgrowthmillion <- popgrowthmillion
for(i in 1:dim(popgrowthmillion)[1]) {
spopgrowthmillion[i,] <- cpopgrowthmillion[i,]/s
}
# Create curve sets
r <- 1951:2015
# \dontshow{
r <- r[1:10]
# }
cset1 <- curve_set(r = r, obs = popgrowthmillion)
cset2 <- curve_set(r = r, obs = spopgrowthmillion)
csets <- list(Raw = cset1, Shape = cset2)
# Functional clustering with respect to joined "st" difference measure
# and "joined" central regions of each group
res <- fclustering(csets, k=3, type="area")
p <- plot(res, plotstyle = "marginal", coverage = 0.5)
p[[1]] # Central functions
p[[2]] # Groups: central functions and regions
# To collect the two figures into one use, e.g., patchwork:
if(require("patchwork", quietly=TRUE)) {
p[[1]] + p[[2]] + plot_layout(widths = c(1, res$k))
}
# Silhouette plot of pam
plot(res$pam)
Run the code above in your browser using DataLab