library(readxl)
datos_chirimoya <- read_excel("C:/2023/II UNAL Semestre II/Modelacion vegetal/clase 3/Chiri datos para R brotes veg.xlsx")
dc <- subset(datos_chirimoya, select = -c(Fecha, Tratamiento, Arbol))
hist(x = dc$bv4) #aca puedo ver la variacion de los brotes que elija (vegetativos o reproductivos) y la fecha (1, 2, 3, 4)
# Crear un gráfico en blanco con ejes etiquetados
plot(dc$bv1, dc$bv4, type = "n", xlab = "bv1", ylab = "bv4", main = "Diagrama de Dispersión de bv1 vs. bv4")
# Agregar puntos para bv1 con pch diferente (por ejemplo, 16 para círculos sólidos)
points(dc$bv1, pch = 1, col = "blue")
# Agregar puntos para bv4 con pch diferente (por ejemplo, 17 para triángulos)
points(dc$bv4, pch = 16, col = "red")
En el anterior grafico no entiendo 1 cosa: que significa que en el eje x
este la variable bv1 y en el y este la variable bv4?
library(cluster)
bv4_dcluster <- dc$bv4
num_clusters <- 3 #Falta determinar numero de cluster obtimo por metodo del codo o Silhouette
kmeans_result <- kmeans(scale(bv4_dcluster), centers = num_clusters) #se realiza clusterin con algoritmo de Kmeans
cluster_asignado_bv4 <- kmeans_result$cluster
cluster_centers <- kmeans_result$centers
print(cluster_asignado_bv4)
## [1] 3 2 1 2 3 2 2 3 1 3 2 2 2 2 2 3 2 2 1 1 2 2 2 1 2 3 2 3 3 2 1 1 2 3 1 1 3
## [38] 2 2 2 2 2 2 3 2 2 2 3 2 2 2 2 1 3 1 2 3 1 3 2 2 3 2 1 1 3 3 2 2 3 2 2 1 1
## [75] 2 2 1 1 1 3 3 1 3 3 2 1 3 2 3 2 3 1 3 2 3 1 3 1 1 2 2 1 3 2 3 2 3 3 1 3 1
## [112] 3 3 3 3 2 2 2 3 2 2 1 3 2 2 2 3 3 2 1 3 2 2 3 2 3 3 1 2 3 2 3 3 2 1 3 3 2
## [149] 3 3 1 1 1 2 3 3 3 3 3 3 1 3
bv1_bv4_dc <- cbind(dc$bv1, dc$bv4)
num_clusters <- 3 #nuevamente, no he determinado el numero de cluster por ninguna prueba
kmeans_result <- kmeans(scale(bv1_bv4_dc), centers = num_clusters)
cluster_asignado_bv1bv4 <- kmeans_result$cluster
print(cluster_asignado_bv1bv4)
## [1] 3 3 2 3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 1 1 3 3 3 1 3 3 3 1 3 3 1 1 3 3 1 1 3
## [38] 3 3 3 3 3 3 3 3 3 3 2 3 3 3 3 2 3 1 3 3 2 3 3 3 3 3 1 1 3 3 3 3 3 3 3 2 2
## [75] 3 3 1 1 1 3 3 1 3 3 3 1 3 3 3 3 3 1 3 3 3 1 3 1 1 3 3 1 2 3 2 3 2 2 1 3 1
## [112] 3 3 2 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3 1 3 3 3 3 3 3 3 1 3 3 3 3 3 3 1 3 3 3
## [149] 3 3 1 1 1 3 2 3 2 2 2 2 2 2
# Crear un gráfico de dispersión en 2D con colores basados en los clusters
plot(x = bv1_bv4_dc[,1], y = bv1_bv4_dc[,2],
col = cluster_asignado_bv1bv4, pch = 16,
main = "Clustering de brotes vegetativos (bv1 y bv4)",
xlab = "bv1", ylab = "bv4")
# Agregar leyenda para los colores de los clusters
legend("topright", legend = unique(cluster_asignado_bv1bv4),
col = 1:num_clusters, pch = 16, title = "Clusters")
7. Ahora procedo a comparar mis variables agrupadas en los 3 clusteres
para ver que tan diferentes son
dc$Cluster <- factor(cluster_asignado_bv1bv4) # Agregar la información de los clusters a tus datos
# Boxplot de bv4 por cluster
boxplot(dc$bv4 ~ dc$Cluster,
main = "Boxplot de bv4 por Cluster",
xlab = "Cluster",
ylab = "bv4")
# Boxplot de bv4 por cluster
boxplot(dc$bv4 ~ dc$Cluster,
main = "Boxplot de bv4 por Cluster",
xlab = "Cluster",
ylab = "bv4")
# Calcular el promedio de bv4
promedio_bv4 <- mean(dc$bv4)
# Agregar un punto rojo en el promedio de bv4
points(x = 1:3, y = tapply(dc$bv4, dc$Cluster, mean), pch = 16, col = "red")
# Agregar una línea horizontal en el promedio de bv4
abline(h = promedio_bv4, lwd = 2, col = "blue", lty = 2)
Necesito aprender mas del manejo de clusteres