Final Project - Developing Data Products

John Theodore
August 23, 2019

Cluster Analysis on MTCARS

This shiny app produces a graphical output of K-Means cluster analysis on the mtcars data set.

Input values that the user generates include:

  • Number of clusters
  • x axis of plot
  • y axis of plot
  • number of “nstarts” (or starting coordinates of the cluster algorithm)
  • number of iterations (or, maxium iterations for the algorithm to run)

Output of Cluster Analysis

The graphical output generated by the user input includes:

  • a plot of n size clusters groups on a user defined x/y axis
  • the “total within sum of squares” statistic for the number of cluster groups plotted
  • a scree plot showing the “total within sum of squares” for a sequence of one to ten cluster assingments

ui Code

library(shiny)
shinyUI(pageWithSidebar(
  headerPanel("MTCARS Cluster Analysis"),
  sidebarPanel(
    sliderInput('k', h5("1. Choose number of clusters"), value = 2, 
                min = 1, max = 7, step =1),
        textInput("textx", h5("2. Type name of feature for x axis (e.g., mpg, hp, disp, wt, qsec, drat)"), value = "mpg"), 
      textInput("texty", h5("3. Type name of feature for y axis (e.g., mpg, hp, disp, wt, qsec, drat)"), value = "hp"),
      numericInput("nstart", h5("4. Type number of nstarts"),  value = 20),
      numericInput("itermax", h5("5. Type number of maximum iterations"),  value = 50)
  ),
  mainPanel(
    h3("K-Means Cluster Groups"),
    tableOutput(("wss")),
    plotOutput('newCluster'),
    plotOutput("screePlot")
  )
))

<!–html_preserve–>

MTCARS Cluster Analysis

K-Means Cluster Groups

<!–/html_preserve–>

Server Code

library(UsingR)
library(dplyr)
library(factoextra)
data(mtcars)
shinyServer(function(input, output) {
  output$newCluster <- renderPlot({
    km.out <- kmeans(mtcars, input$k, input$nstart, input$itermax)
    fviz_cluster(km.out, mtcars[, c(input$textx, input$texty)])
    }, height = 400, width = 700)
  output$screePlot <- renderPlot({
    fviz_nbclust(mtcars, kmeans, method = "wss")
  }, height = 200, width = 700)
  output$wss <- renderText({ 
    km.out <- kmeans(mtcars, input$k, input$nstart, input$itermax)
    paste(input$k, "Clusters", "with total within sum of squares of:", 
          round(km.out$tot.withinss))
  })
})