Skip to content

Instantly share code, notes, and snippets.

@randyzwitch
Created September 17, 2013 17:46
Show Gist options
  • Save randyzwitch/6597905 to your computer and use it in GitHub Desktop.
Save randyzwitch/6597905 to your computer and use it in GitHub Desktop.
elbow plot
#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)
Copy link

ghost commented Mar 13, 2015

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment