extrobe
2/1/2017 - 5:49 PM

Create multiple K-Means models & Plot them

Create multiple K-Means models & Plot them

# Set random seed. Don't remove this line.
set.seed(100)

# Explore the structure of your data
str(school_result)

# Initialise ratio_ss 
ratio_ss<-rep(0,7)

# Finish the for-loop. 
for (k in 1:7) {
  
  # Apply k-means to school_result: school_km
school_km<-kmeans(school_result,k,nstart=20)  
  
  # Save the ratio between of WSS to TSS in kth element of ratio_ss
  ratio_ss[k]<-school_km$tot.withinss/school_km$totss
  
}

# Make a scree plot with type "b" and xlab "k"
plot(ratio_ss,type="b",xlab="k")