suppressMessages(require(WeightedCluster))
set.seed(99)
# Load the MVAD data & aggregate the state sequences
data(mvad)
agg <- wcAggregateCases(mvad[,17:86], weights=mvad$weight)
# Create a state sequence object without the first two (summer) time points
states <- c("EM", "FE", "HE", "JL", "SC", "TR")
labels <- c("Employment", "Further Education", "Higher Education",
"Joblessness", "School", "Training")
mvad.seq <- seqdef(mvad[agg$aggIndex, 17:86],
states=states, labels=labels,
weights=agg$aggWeights)
# Run k-modes without the weights
resX <- wKModes(mvad.seq, 2)
# Run k-modes with the weights
resW <- wKModes(mvad.seq, 2, weights=agg$aggWeights)
# Examine the modal sequences of both solutions
seqformat(seqdef(resX$modes), from="STS", to="SPS", compress=TRUE)
seqformat(seqdef(resW$modes), from="STS", to="SPS", compress=TRUE)
# Using tot.withindiff to choose the number of clusters
# \donttest{
TWdiffs <- sapply(1:5, function(k) wKModes(mvad.seq, k, weights=agg$aggWeights)$tot.withindiff)
plot(TWdiffs, type="b", xlab="K")
# Use multiple random starts to account for inherent randomness
TWDiff <- sapply(1:5, function(k) min(replicate(10,
wKModes(mvad.seq, k, weights=agg$aggWeights)$tot.withindiff)))
plot(TWDiff, type="b", xlab="K")# }
Run the code above in your browser using DataLab