Hierachical Clustering in mtcars using ShinyApp

Enrique Ruix
10 of April 2016

Introduction

This app is part of the Developing Data Products Peer Assignment within the Coursera Data Science Specialisation. This app aims to be used as a tool for exploring the mtcars dataset in R. You could use the app for:

  • To select and plot two difference measures.
  • To perform hierarchical clusters.
  • To identify cars belonging to clusters.
  • To calculate average in variables by groups.

What are the inputs?

You will be asked to select two variables as well as the number of clusters to create:

  • Variable 1: select one of the columns from the mtcars dataset. This variable will be placed on the y axis.
  • Variable 2: select one of the columns from the mtcars dataset. This variable will be placed in the x axis.
  • Number of cluster: select a number between 1 and 5 to group the measures in clusters.

What are the outputs?

Variable 1 vs. Variable 2 plot
The next code performs a hieralchical clustering for hp and mpg with 3 different groups and calculate the average hp and mpg for the different clusters. The output will be shown in the next slide.

clusters<-kmeans(mtcars[, c("hp", "mpg")], 3)
data<-cbind(data.frame(Group=c(clusters$cluster)),mtcars[,c(1,2)])
avg_var1<-with(data,aggregate(mtcars[,"hp"],by=list(Group),FUN=mean))
avg_var2<-with(data,aggregate(mtcars[,"mpg"],by=list(Group),FUN=mean))
names(avg_var1) <- c("Group", paste("Average", "hp"))
names(avg_var2) <- c("Group", paste("Average", "mpg"))
data <- merge(avg_var1, avg_var2)

What are the outputs?

.
Those two outputs will help you to:

  • Explore in the next chart relationships between different variables in the mtcars dataset.
  • Create clusters to group different measures together and check the average values on the table.

plot of chunk unnamed-chunk-2

.
.

  Group Average hp Average mpg
1     1   93.52941    24.22353
2     2  263.80000    14.62000
3     3  178.50000    15.80000

Links

Find below useful links to find the app, code and this presentation: