Este trabajo analiza la temperatura en los 32 estados de México entre 1985 y 2025, utilizando datos de temperatura mínima, máxima y media.
Mediante técnicas de clustering, los estados se agrupan en categorías climáticas (fríos, templados, cálidos) para identificar patrones regionales y comparar su diversidad térmica.
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:data.table':
##
## between, first, last
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
## PERIOD STATE_ID STATE MIN_C
## Length:16071 Min. : 0 Length:16071 Min. :-2.20
## Class :character 1st Qu.: 8 Class :character 1st Qu.: 9.50
## Mode :character Median :16 Mode :character Median :14.10
## Mean :16 Mean :13.96
## 3rd Qu.:24 3rd Qu.:18.60
## Max. :32 Max. :26.50
## MEAN_C MAX_C PRECIPITATION_MM MIN_F
## Min. : 7.50 Min. :15.30 Min. : 0.00 Min. :28.04
## 1st Qu.:17.60 1st Qu.:25.60 1st Qu.: 7.70 1st Qu.:49.10
## Median :21.70 Median :29.10 Median : 35.70 Median :57.38
## Mean :21.39 Mean :28.85 Mean : 73.86 Mean :57.12
## 3rd Qu.:25.20 3rd Qu.:32.20 3rd Qu.:111.75 3rd Qu.:65.48
## Max. :33.40 Max. :40.40 Max. :851.00 Max. :79.70
## MEAN_F MAX_F PRECIPITATION_IN
## Min. :45.50 Min. : 59.54 Min. : 0.0000
## 1st Qu.:63.68 1st Qu.: 78.08 1st Qu.: 0.3031
## Median :71.06 Median : 84.38 Median : 1.4055
## Mean :70.51 Mean : 83.93 Mean : 2.9080
## 3rd Qu.:77.36 3rd Qu.: 89.96 3rd Qu.: 4.3996
## Max. :92.12 Max. :104.72 Max. :33.5039
## 'data.frame': 16071 obs. of 11 variables:
## $ PERIOD : chr "1985-01-01" "1985-01-01" "1985-01-01" "1985-01-01" ...
## $ STATE_ID : int 0 1 2 3 4 5 6 7 8 9 ...
## $ STATE : chr "Mexico" "Aguascalientes" "Baja California" "Baja California Sur" ...
## $ MIN_C : num 7.8 3.1 5.6 9.2 15.7 2.9 14.4 14.1 -1.4 4.1 ...
## $ MEAN_C : num 15.9 12.2 12.9 17.1 22.7 10.3 22.7 20.9 7.5 12.5 ...
## $ MAX_C : num 23.9 21.3 20.2 25 29.7 17.7 30.9 27.8 16.5 20.9 ...
## $ PRECIPITATION_MM: num 36 4.9 12.2 30.3 20.9 ...
## $ MIN_F : num 46 37.6 42.1 48.6 60.3 ...
## $ MEAN_F : num 60.6 54 55.2 62.8 72.9 ...
## $ MAX_F : num 75 70.3 68.4 77 85.5 ...
## $ PRECIPITATION_IN: num 1.417 0.193 0.48 1.193 0.823 ...
clima$PERIOD <- as.character(clima$PERIOD)
x <- as.Date(clima$PERIOD) # funciona si viene AAAA-MM-DD
bad <- is.na(x)
if (any(bad)) {
x[bad] <- as.Date(paste0(clima$PERIOD[bad], "-01")) # para AAAA-MM
}
clima$PERIOD <- x
clima <- subset(clima, STATE_ID != 0)
clima <- subset(clima, PERIOD >= as.Date("1985-01-01") & PERIOD <= as.Date("2025-07-01"))features <- clima %>%
group_by(STATE) %>%
summarise(
min_c_monthly_mean = mean(MIN_C, na.rm = TRUE),
max_c_monthly_mean = mean(MAX_C, na.rm = TRUE),
mean_c_monthly_mean = mean(MEAN_C, na.rm = TRUE)
) %>%
mutate(thermal_amplitude = max_c_monthly_mean - min_c_monthly_mean) %>%
ungroup()
head(features)## # A tibble: 6 × 5
## STATE min_c_monthly_mean max_c_monthly_mean mean_c_monthly_mean
## <chr> <dbl> <dbl> <dbl>
## 1 Aguascalientes 8.91 26.4 17.6
## 2 Baja California 12.0 26.8 19.4
## 3 Baja California Sur 15.5 30.3 22.9
## 4 Campeche 20.3 33.1 26.7
## 5 Chiapas 18.2 30.7 24.5
## 6 Chihuahua 9.21 26.8 18.0
## # ℹ 1 more variable: thermal_amplitude <dbl>
X <- features %>% select(min_c_monthly_mean,
max_c_monthly_mean,
mean_c_monthly_mean,
thermal_amplitude)
X_scaled <- scale(X)
# Codo
fviz_nbclust(as.data.frame(X_scaled), kmeans, method = "wss") +
ggtitle("Elección de K - Codo")# Silueta
fviz_nbclust(as.data.frame(X_scaled), kmeans, method = "silhouette") +
ggtitle("Elección de K - Silueta")set.seed(42)
km4 <- kmeans(X_scaled, centers = 4, nstart = 50)
features$cluster_k4 <- factor(km4$cluster)
# Vista rápida del cluster plot
fviz_cluster(km4, data = as.data.frame(X_scaled), geom = "point",
ellipse.type = "norm",
main = "Clusters de temperatura por estado (K = 4)")ord <- features %>%
group_by(cluster_k4) %>%
summarise(mean_temp = mean(mean_c_monthly_mean), .groups = "drop") %>%
arrange(mean_temp) %>%
mutate(label = c("Frío","Templado","Cálido","Muy cálido")) # si cambias K, ajusta
features <- features %>%
left_join(ord %>% select(cluster_k4, label), by = "cluster_k4")tabla_final <- features %>%
dplyr::arrange(mean_c_monthly_mean) %>%
dplyr::select(STATE,
min_c_monthly_mean,
max_c_monthly_mean,
mean_c_monthly_mean,
thermal_amplitude,
cluster_k4,
categoria = label)
tabla_final## # A tibble: 32 × 7
## STATE min_c_monthly_mean max_c_monthly_mean mean_c_monthly_mean
## <chr> <dbl> <dbl> <dbl>
## 1 Tlaxcala 6.18 23.2 14.7
## 2 Estado de México 6.99 22.7 14.8
## 3 Ciudad de México 9.65 24.0 16.8
## 4 Zacatecas 8.63 26.2 17.4
## 5 Hidalgo 9.81 25.2 17.5
## 6 Aguascalientes 8.91 26.4 17.6
## 7 Durango 8.89 26.8 17.8
## 8 Puebla 10.5 25.4 17.9
## 9 Chihuahua 9.21 26.8 18.0
## 10 Guanajuato 10.5 27.1 18.8
## # ℹ 22 more rows
## # ℹ 3 more variables: thermal_amplitude <dbl>, cluster_k4 <fct>,
## # categoria <chr>
ggplot(tabla_final,
aes(x = reorder(STATE, mean_c_monthly_mean),
y = mean_c_monthly_mean, fill = categoria)) +
geom_col() +
coord_flip() +
labs(x = "Estado", y = "Temp. media (°C, 1985–2025)",
title = "Temperatura media por estado y categoría (K=4)") +
theme_minimal()