install.packages(“GGally”) install.packages(“factoextra”)
install.packages(“cluster”) install.packages(“mclust”) library(ggplot2)
library(GGally)
library(gridExtra) library(ggdendro) library(factoextra)
library(cluster) library(mclust)
file_path <- “D:/Users/anaid.limon/OneDrive - Red de Universidades Anáhuac/Escritorio/Maestría/CNS/foxes.txt”
foxes <- read.table(file_path, header = TRUE)
head(foxes)
hist1 <- ggplot(foxes, aes(x = F1)) + geom_histogram(binwidth = 1, fill = “lightpink”, color = “#FF6EB4”) + labs(title = “Altura hasta los hombros (F1)”, x = “Altura (cm)”, y = “Frecuencia”) + geom_density(aes(y = ..count..), color = “magenta”)
hist2 <- ggplot(foxes, aes(x = F2)) + geom_histogram(binwidth = 1, fill = “lightpink”, color = “#FF6EB4”) + labs(title = “Longitud del cuerpo (F2)”, x = “Longitud (cm)”, y = “Frecuencia”) + geom_density(aes(y = ..count..), color = “magenta”)
hist3 <- ggplot(foxes, aes(x = F3)) + geom_histogram(binwidth = 0.2, fill = “lightpink”, color = “#FF6EB4”) + labs(title = “Peso (F3)”, x = “Peso (kg)”, y = “Frecuencia”) + geom_density(aes(y = ..count..), color = “magenta”)
grid.arrange(hist1, hist2, hist3, ncol = 1)
ggpairs(foxes[, c(“F1”, “F2”, “F3”)], title = “Matriz de Dispersión de Variables F1, F2 y F3”)
boxplot1 <- ggplot(foxes, aes(y = F1, x = ““)) + geom_boxplot(fill =”lightblue”) + labs(title = “Altura hasta los hombros (F1)”, y = “Altura (cm)”, x = ““)
boxplot2 <- ggplot(foxes, aes(y = F2, x = ““)) + geom_boxplot(fill =”lightblue”) + labs(title = “Longitud del cuerpo (F2)”, y = “Longitud (cm)”, x = ““)
boxplot3 <- ggplot(foxes, aes(y = F3, x = ““)) + geom_boxplot(fill =”lightblue”) + labs(title = “Peso (F3)”, y = “Peso (kg)”, x = ““)
grid.arrange(boxplot1, boxplot2, boxplot3, ncol = 3)
D <- dist(foxes, method = “euclidean”)
xs <- hclust(D, method = “single”) xa <- hclust(D, method = “average”) xc <- hclust(D, method = “complete”) xl <- list(xs, xa, xc) ml <- c(“Single”, “Average”, “Complete”) hl <- list(c(4.5), c(12, 9, 7.9), c(20,14,11))
par(mfrow = c(1, 3), las = 1) lapply(1:length(xl), function(x) { plot(xl[[x]], hang = -1, main = ml[x], labels = FALSE, sub = ““, xlab =”“) abline(h = hl[[x]], lty = 2, col = c(”red”, “blue”, “#32CD32”)) })
cor_matrix <- cor(foxes[, c(“F1”, “F2”, “F3”)]) print(cor_matrix)
if(!require(factoextra)) install.packages(“factoextra”) library(factoextra)
fviz_nbclust(foxes, kmeans, method = “wss”) + geom_vline(xintercept = 3, linetype = 2)
num_clusters <- 2
set.seed(123) # Para reproductibilidad kmeans_result <- kmeans(foxes, centers = num_clusters, nstart = 100)
print(kmeans_result)
foxes\(Cluster <- as.factor(kmeans_result\)cluster)
cat(“Número de observaciones en cada cluster:”) print(table(foxes$Cluster))
ggpairs(foxes[, c(“F1”, “F2”, “F3”, “Cluster”)], aes(color = Cluster, alpha = 0.5))
fviz_cluster(kmeans_result, data = foxes[, c(“F1”, “F2”, “F3”)], ellipse.type = “norm”, geom = “point”, stand = FALSE, main = “Grupos de zorros”)
num_clusters <- 3
kmeans_result <- kmeans(foxes, centers = num_clusters, nstart =100)
print(kmeans_result)
foxes\(Cluster <- as.factor(kmeans_result\)cluster)
cat(“Número de observaciones en cada cluster:”) print(table(foxes$Cluster))
ggpairs(foxes[, c(“F1”, “F2”, “F3”, “Cluster”)], aes(color = Cluster, alpha = 0.5))
fviz_cluster(kmeans_result, data = foxes[, c(“F1”, “F2”, “F3”)], ellipse.type = “norm”, geom = “point”, stand = FALSE, main = “Grupos de zorros”)
num_clusters <- 4
kmeans_result <- kmeans(foxes, centers = num_clusters, nstart = 100)
print(kmeans_result)
foxes\(Cluster <- as.factor(kmeans_result\)cluster)
cat(“Número de observaciones en cada cluster:”) print(table(foxes$Cluster))
ggpairs(foxes[, c(“F1”, “F2”, “F3”, “Cluster”)], aes(color = Cluster, alpha = 0.5))
fviz_cluster(kmeans_result, data = foxes[, c(“F1”, “F2”, “F3”)], ellipse.type = “norm”, geom = “point”, stand = FALSE, main = “Grupos de zorros”)
mclust_model <- Mclust(foxes, G = 3)
summary(mclust_model)
mclust_model\(parameters\)mean
mclust_model\(parameters\)pro
mclust_model\(parameters\)variance
model_name <- mclust_model$modelName cat(“Modelo seleccionado:”, model_name, “”)
plot(mclust_model)
1 2 3 4 0
cluster_assignment <- mclust_model$classification for (i in 1:3) { cat(“Cluster”, i, “consists of:”) print(names(cluster_assignment[cluster_assignment == i])) }
uncertainty <- mclust_model$uncertainty head(uncertainty)
features <- mclust_model$parameters features
fviz_cluster(mclust_model, data = foxes[, c(“F1”, “F2”, “F3”)], ellipse.type = “norm”, geom = “point”, stand = FALSE, main = “Grupos de zorros”)
#####Comparando los modelos###### # Agregar los resultados de k-means y GMM al dataframe foxes\(kmeans_cluster <- as.factor(kmeans_result\)cluster) foxes\(gmm_cluster <- as.factor(mclust_model\)classification)
foxes kmeans_result plot_kmeans <- fviz_cluster(kmeans_result, data = foxes[, c(“F1”, “F2”, “F3”)], ellipse.type = “norm”, geom = “point”, stand = FALSE, main = “Clusters usando k-medias”) + theme_minimal()
print(plot_kmeans)
plot_gmm <- fviz_mclust(mclust_model, what = “classification”, geom = “point”, palette = “jco”, ellipse.type = “norm”, main = “Clusters usando Modelo Mixtura”) + theme_minimal()
print(plot_gmm)
grid.arrange(plot_kmeans, plot_gmm, ncol = 2)
comparison_table <- table(foxes\(kmeans_cluster, foxes\)gmm_cluster)
print(comparison_table)
ggplot(as.data.frame(comparison_table), aes(x = Var1, y = Freq, fill = Var2)) + geom_bar(stat = “identity”, position = “dodge”) + labs(title = “Comparación de Clusters: K-means vs GMM”, x = “Clusters de K-means”, y = “Frecuencia”, fill = “Clusters de GMM”) + theme_minimal()
foxes\(gmm_cluster <- as.factor(mclust_model\)classification)
print(foxes)