, , , , ,

[SOLVED] STAT 302 Winter 2025 Final Project

$25

File Name: STAT_302_Winter_2025_Final_Project.zip
File Size: 320.28 KB

5/5 - (1 vote)

STAT 302 Winter 2025 Final Project

The assignment should be completed in an R Markdown document.  Submit the assignment electronically on Gradescope by March 21, at 11:59 PM as a PDF file. No late submission will accepted.

Each answer has to be based on R code that shows how the result was obtained.  R code has to answer the question or solve the task.

NOTE: Please keep the following guidelines in mind::

•  Choose the most efficient code whenever possible.

•  Keep your code as short as possible.

•  Use only base ‘R‘, unless the instructions explicitly request you to use a specific library.

•  Use chunk options to suppress unnecessary output or messages in your code.

•  Ensure your code fits within the page and does not extend beyond the margins in the PDF.

•  Check the grading rubric for all the items which will be graded in this Assignment!

The total number of points of this assignment is 60 points.

Good luck!

K means clustering

In this project, you will implement an unsupervised learning technique called k-means clustering.  This method is used to partition a dataset into k distinct clusters based on the similarity of the data points.  The goal of k-means is to minimize the variance within each cluster, so that points within the same cluster are as similar as possible.

While there is a built-in function in base R that can perform k-means clustering, you are not allowed to use it in this project. Instead, you will manually implement the algorithm from scratch.

The k-means algorithm works as follows:

•  Choose k initial centroids (typically randomly selected points from the dataset).

•  Assign each data point to the closest centroid, forming KK clusters.

•  Recompute the centroids of the clusters by calculating the mean of all points assigned to each cluster.

•  Repeat the assignment and update steps until the centroids no longer change (or change very little), indicating convergence.

Your task is to replicate this process and implement the algorithm manually using base R functions, without relying on any built-in R functions  (such as the knn() function) and without relying on any specialized clustering libraries.

Consider one of the following datasets:

This Taylor Swift dataset from a previous homework,

urlRemote  <- ‘ https://raw.githubusercontent.com/ ‘

githubPath  <- ‘rfordatascience/tidytuesday/master/data/2023/2023-10-17/ ‘

fileName  <- ‘ taylor_album_songs .csv ‘

taylor_album_songs  <- read.csv (paste0(urlRemote,  githubPath,  fileName))

songs_reduced  <- taylor_album_songs[,c(“track_number” ,  “energy” ,  “valence”)]

songs_reduced_final  <- songs_reduced[rowSums (is.na (songs_reduced))==0,]

Or use the data set Mall_Customers .csv uploaded on Canvas in the Module named Final  Project:

mall_customers  <- read.csv ( “Mall_Customers.csv”)

1. In order to start exploring the dataset, use base R or ggplot to create some plots (at least 2).

In order to simplify your task, if you are working with the mall_customers data set, for parts 2-9 con- sider only the quantitative variables.   If you  are working with the Taylor  Swift dataset,  use the object songs_reduced_final as your dataset (it only contains quantitative variables).

2.  Randomly choose any three points from the dataset which will be your initial centroids.  These will define the centers of the first initial three clusters. This means that you are defining k = 3 clusters.

3.  Measure the distance between all the points in the dataset and each of the three initial clusters.  Assign the points to the nearest cluster. Do this in the most efficient way.

4.  After assigning all points to the closest centroid, recalculate the centroids.  This is achieved by com- puting the mean of all the variables of the points assigned to each cluster.  This will give you the new centroids.

5.  Repeat the process of assigning points to clusters and updating the centroids until the centroids no longer change.

6.  Calculate the Within-Cluster-Sum of Squared Errors (WSS) for different values of k (including the k = 3). The WSS is given by:

where:

is the i-th data point in cluster k, where d is the number of dimensions (features).

μk  = (µk1, µk2, . . . , µkd ) is the centroid (mean) of cluster k, calculated as the average of the points in the cluster.

The term represents the squared Euclidean distance between the point xi(k) and its corresponding centroid μk .

7.  Using either base R or ggplot2, plot WSS-versus-k.  Choose the k for which the effect of adding an additional cluster is no longer as effective in terms of WSS. In the plot, this is visible as an elbow.

8.  Since this procedure started with a random seed, consider 10 different random seeds for each value of k.

9.  For each data point, compute the proportion of times the data point ends up in the same cluster. Are there any points which are not always assigned to the same group?  If so, try to explain why you think that happens.

10.  Choose any of the seeds used above and consider your clustering as final.  Add a variable called cluster to the data set. Use the variables in the data set (both the ones used for clustering and the ones not used for clustering) to visually describe the clusters. Explain what differences you observe.

Shopping Cart
[SOLVED] STAT 302 Winter 2025 Final Project[SOLVED] STAT 302 Winter 2025 Final Project
$25