Skip to content

Instantly share code, notes, and snippets.

@amy17519
Created August 22, 2016 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amy17519/4f929aaa5939e8ad5a1b34a5fdba35fc to your computer and use it in GitHub Desktop.
Save amy17519/4f929aaa5939e8ad5a1b34a5fdba35fc to your computer and use it in GitHub Desktop.
wss <- (nrow(mug[,7:9])-1)*sum(apply(mug[,7:9],2,var))
for (i in 2:10) wss[i] <- sum(kmeans(mug[,7:9],
centers=i)$withinss)
plot(1:10, wss, type="b", xlab="Number of Clusters",
ylab="Within groups sum of squares")
#determines k=5
set.seed(123)
mugCluster <- kmeans(mug[, 7:9], 5, nstart = 100)
#observe patterns in each cluster, rename clusters by their characteristics, then #use it as a new variable: Difficulty
mugCluster$centers
mugCluster$cluster <- factor(mugCluster$cluster,levels=c(2,1,5,4,3))
levels(mugCluster$cluster) <- c("Inconclusive","Easy to Find Mugs",'Medium Difficulty',
"Hard to Get Mugs","Very Hard to Get Mugs")
mug$Difficulty <- mugCluster$cluster
output$kmeans <- renderPlotly({
plot_ly(data = mug, x = Owner, y = Seeker, mode = "markers",
text= paste(Name, "<br>Edition: ", Edition, "<br>Country: ", Country,
"<br>City: ",City, "<br>Owner: ",Owner,"<br>Seeker: ",
Seeker,"<br>Trader",Trader, "<br>Difficulty: ",Difficulty),
color = mugCluster$cluster,
colors =c('olivedrab','navyblue','indianred2','darkgoldenrod1','magenta4')) %>%
layout(title='K-means Clustering: # of Seekers vs. # of Owners')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment