public
Created

elbow plot

  • Download Gist
elbow-plot.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#Calculate lm's for emphasis
lm(cost_df$cost[1:10] ~ cost_df$cluster[1:10])
lm(cost_df$cost[10:19] ~ cost_df$cluster[10:19])
lm(cost_df$cost[20:100] ~ cost_df$cluster[20:100])
 
cost_df$fitted <- ifelse(cost_df$cluster <10, (19019.9 - 550.9*cost_df$cluster),
ifelse(cost_df$cluster <20, (15251.5 - 116.5*cost_df$cluster),
(13246.1 - 35.9*cost_df$cluster)))
 
#Cost plot
ggplot(data=cost_df, aes(x=cluster, y=cost, group=1)) +
theme_bw(base_family="Garamond") +
geom_line(colour = "darkgreen") +
theme(text = element_text(size=20)) +
ggtitle("Reduction In Cost For Values of 'k'\n") +
xlab("\nClusters") +
ylab("Within-Cluster Sum of Squares\n") +
scale_x_continuous(breaks=seq(from=0, to=100, by= 10)) +
geom_line(aes(y= fitted), linetype=2)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.