K-means on a soccer field (part 2)
In the previous exercise, you successfully used the k-means algorithm to cluster the two teams from the lineup data frame. This time, let's explore what happens when you use a k of 3.
You will see that the algorithm will still run, but does it actually make sense in this context…
Diese Übung ist Teil des Kurses
<Kurs>Cluster Analysis in R</Kurs>Übungsanweisungen
- Build a k-means model called
model_km3for thelineupdata using thekmeans()function withcenters = 3. - Extract the vector of cluster assignments from the model
model_km3$clusterand store this in the variableclust_km3. - Append the cluster assignments as a column
clusterto thelineupdata frame and save the results to a new data frame calledlineup_km3. - Use ggplot to plot the positions of each player on the field and color them by their cluster.
Interaktive praktische Übung
Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.
# Build a kmeans model
model_km3 <- ___
# Extract the cluster assignment vector from the kmeans model
clust_km3 <- ___
# Create a new data frame appending the cluster assignment
lineup_km3 <- ___
# Plot the positions of the players and color them using their cluster
ggplot(___, aes(x = ___, y = ___, color = factor(___))) +
geom_point()