Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
DataAnalytics2025_MICHAEL_GIANNATTASIO/Lab3.Rmd
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
145 lines (102 sloc)
3.5 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
output: | |
pdf_document: default | |
html_document: default | |
--- | |
```{r, include=FALSE} | |
if (!require("tidyverse")) { | |
install.packages("tidyverse") | |
library(tidyverse) | |
} | |
if (!require("caret")) { | |
install.packages("caret") | |
library(caret) | |
} | |
``` | |
```{r} | |
################### | |
##### Abalone ##### | |
################### | |
# read dataset | |
abalone <- read.csv("~/DataStore-DataAnalytics/abalone_dataset.csv") | |
dataset <- abalone | |
## add new column age.group with 3 values based on the number of rings | |
dataset$age.group <- cut(dataset$rings, br=c(0,8,11,35), labels = c("young", 'adult', 'old')) | |
## alternative way of setting age.group | |
dataset$age.group[dataset$rings<=8] <- "young" | |
dataset$age.group[dataset$rings>8 & dataset$rings<=11] <- "adult" | |
dataset$age.group[dataset$rings>11 & dataset$rings<=35] <- "old" | |
dataset <- na.omit(dataset) | |
``` | |
```{r} | |
# Create dummy variables | |
dummies <- dummyVars(~ sex, data = dataset) | |
# Apply transformation | |
sex_dummies <- predict(dummies, newdata = dataset) | |
# Convert to dataframe and bind with original dataset (excluding 'sex' column) | |
dataset <- cbind(dataset[, !names(dataset) %in% "sex"], as.data.frame(sex_dummies)) | |
dataset <- dataset %>% select(-rings, -sexM) | |
subset1 <- dataset[,1:8] | |
subset2 <- cbind(dataset[,9:10], dataset[,2:8]) | |
``` | |
```{r} | |
# sample create list of 105 (70% of 150) numbers randomly sampled from 1-150 | |
set.seed(123) | |
n = 150 | |
s1 <- sample(n,n*.8) | |
s2 <- sample(n,n*.8) | |
## create train & test sets based on sampled indexes | |
s1train <- subset1[s1,] | |
s1test <- subset1[-s1,] | |
s2train <- subset2[s2,] | |
s2test <- subset2[-s2,] | |
``` | |
# kNN Models: | |
```{r} | |
## train and evaluate multiple knn models to find optimal k | |
knn.model <- train(s1train[,1:7], s1train$age.group, method = "knn", tuneLength = 10, trControl = trainControl(method = "cv")) | |
# print model outputs | |
print(knn.model) | |
knn.predicted <- predict(knn.model, newdata = s1test[,1:7]) | |
# create contingency table/ confusion matrix | |
contingency.table <- table(knn.predicted, s1test$age.group, dnn=list('predicted','actual')) | |
contingency.table | |
# calculate classification accuracy | |
sum(diag(contingency.table))/length(s1test$age.group) | |
``` | |
```{r} | |
## train and evaluate multiple knn models to find optimal k | |
knn.model2 <- train(s2train[,1:8], s2train$age.group, method = "knn", tuneLength = 10, trControl = trainControl(method = "cv")) | |
# print model outputs | |
print(knn.model2) | |
knn.predicted2 <- predict(knn.model2, newdata = s2test[,1:8]) | |
# create contingency table/ confusion matrix | |
contingency.table2 <- table(knn.predicted2, s2test$age.group, dnn=list('predicted','actual')) | |
contingency.table2 | |
# calculate classification accuracy | |
sum(diag(contingency.table2))/length(s2test$age.group) | |
``` | |
# K-Means Clustering | |
Subset 1 had the better results, so we proceed with doing K-means clustering on that dataset | |
```{r} | |
## run tests with multiple k values and plot WCSS | |
k.list <- c(2,3,4,5,6,7,8) | |
wcss.list <- c() | |
for (k in k.list) { | |
km <- kmeans(subset1[,1:7], centers = k) | |
wcss <- km$tot.withinss | |
wcss.list <- c(wcss.list,wcss) | |
## get and plot clustering output | |
assigned.clusters <- as.factor(km$cluster) | |
ggplot(subset1, aes(x = length, y = height, colour = assigned.clusters)) + | |
geom_point() | |
} | |
plot(k.list,wcss.list,type = "b") | |
km <- kmeans(subset1[,1:7], centers = 4) | |
wcss <- km$tot.withinss | |
wcss.list <- c(wcss.list,wcss) | |
## get and plot clustering output | |
assigned.clusters <- as.factor(km$cluster) | |
ggplot(subset1, aes(x = length, y = height, colour = assigned.clusters)) + | |
geom_point() | |
``` |