#— title: “ODS 9. Industria Innovación e Infraestructura: CALCULO DE INDICADOR SINTETICO” author: “Abel Peña” date: “2025-06-24” output: prettydoc::html_pretty: theme: architect highlight: github math: katex —

1. INSTALAR PAQUETES (si es necesario)

# install.packages(c("wbstats","dplyr","tidyverse","magrittr","ggplot2","kableExtra","writexl"))

2. CARGAR LIBRERÍAS

# (ya se hace en el chunk de setup)

3. OBTENCIÓN DE DATOS DESDE WORLD BANK (ODS 9)

# 3.1 Metadatos y ejemplo puntual
indicadores_disponibles <- wb_indicators(lang = "es")
datos_ejemplo <- wb_data(
  indicator  = "NV.IND.MANF.ZS",  # Manufactura % PIB (ODS 9)
  country    = "SLV",
  start_date = 2010,
  end_date   = 2024,
  lang       = "es"
)

# 3.2 Indicadores ODS 9
ods9_indicators <- c(
  "NV.IND.MANF.ZS",    # Manufactura (% del PIB)
  "EG.ELC.ACCS.ZS",    # Acceso a electricidad (% población)
  "IT.NET.USER.ZS",    # Usuarios de Internet (% población)
  "GB.XPD.RSDV.GD.ZS"   # Gasto en I+D (% del PIB)
)

# 3.3 Países por región + El Salvador
countries_region <- c(
  "CAN","USA","MEX",         # Norteamérica
  "BLZ","GTM","HND","NIC","CRI","PAN",  # Centroamérica
  "HTI","CUB","JAM","DOM",  # Caribe
  "COL","VEN","PER","CHL","BOL","ARG","BRA",  # Sudamérica
  "SLV"                           # El Salvador
)

# 3.4 Descargar datos ODS9 2010–2024
data_ods9 <- wb_data(
  indicator  = ods9_indicators,
  country    = countries_region,
  start_date = 2010,
  end_date   = 2024,
  lang       = "es"
)

# 3.5 Asignar región
data_ods9 <- data_ods9 %>%
  mutate(region = case_when(
    iso3c %in% c("CAN","USA","MEX")                   ~ "Norteamérica",
    iso3c %in% c("BLZ","GTM","HND","NIC","CRI","PAN") ~ "Centroamérica",
    iso3c %in% c("HTI","CUB","JAM","DOM")            ~ "Caribe",
    iso3c %in% c("COL","VEN","PER","CHL","BOL","ARG","BRA") ~ "Sudamérica",
    iso3c == "SLV"                                       ~ "El Salvador"
  ))

4. RESUMEN ESTADÍSTICO

# Estadísticas descriptivas por región
estad_ods9 <- data_ods9 %>%
  group_by(region) %>%
  summarize(across(all_of(ods9_indicators),
                   list(mean = ~mean(.x, na.rm=TRUE),
                        sd   = ~sd(.x, na.rm=TRUE),
                        min  = ~min(.x, na.rm=TRUE),
                        max  = ~max(.x, na.rm=TRUE)),
                   .names = "{col}_{fn}"))
estad_ods9 %>% kable(format = "html", caption = "Descriptivas ODS9 por región")
Descriptivas ODS9 por región
region NV.IND.MANF.ZS_mean NV.IND.MANF.ZS_sd NV.IND.MANF.ZS_min NV.IND.MANF.ZS_max EG.ELC.ACCS.ZS_mean EG.ELC.ACCS.ZS_sd EG.ELC.ACCS.ZS_min EG.ELC.ACCS.ZS_max IT.NET.USER.ZS_mean IT.NET.USER.ZS_sd IT.NET.USER.ZS_min IT.NET.USER.ZS_max GB.XPD.RSDV.GD.ZS_mean GB.XPD.RSDV.GD.ZS_sd GB.XPD.RSDV.GD.ZS_min GB.XPD.RSDV.GD.ZS_max
Caribe 13.42832 3.9334461 7.673248 24.47841 84.23214 24.2925960 37.2 100 48.14750 24.33476 8.37 85.2 0.4361631 0.0988444 0.27192 0.60767
Centroamérica 11.94282 4.2783391 4.982167 19.42673 91.82143 5.8626256 80.1 100 44.64524 20.41581 10.00 85.4 0.1765940 0.1658248 0.01497 0.55627
El Salvador 15.67616 0.7099447 14.003302 16.38749 96.17857 2.4826098 91.6 100 38.32143 18.51325 15.90 67.7 0.1217500 0.0533814 0.03397 0.18078
Norteamérica 13.95116 4.5087747 9.015477 21.37379 99.81667 0.3348947 99.0 100 76.32381 17.81400 31.10 94.6 1.6636193 1.0947784 0.25782 3.58623
Sudamérica 11.92949 1.9675176 8.525428 16.31750 97.72551 3.3307784 88.0 100 60.42065 17.31381 22.40 94.5 0.4791794 0.3604816 0.05530 1.37093
# Boxplot: Manufactura % PIB
ggplot(data_ods9, aes(x = region, y = NV.IND.MANF.ZS, fill = region)) +
  geom_boxplot() +
  labs(title = "Manufactura (% PIB) por región", y = "% PIB") +
  theme_minimal()

# Serie temporal: Gasto en I+D
ggplot(data_ods9, aes(x = as.integer(date), y = GB.XPD.RSDV.GD.ZS, color = region)) +
  geom_line() +
  labs(title = "Gasto en I+D (% PIB) 2010–2024", x = "Año", y = "% PIB") +
  theme_minimal()

5. CORRELACIONES TEÓRICAS Y EMPÍRICAS

# Empírica
data_corr <- data_ods9 %>%
  select(all_of(ods9_indicators)) %>%
  drop_na()
cor_emp_ods9 <- cor(data_corr)
cor_emp_ods9 %>% round(2) %>% kable(caption = "Correlación empírica ODS9")
Correlación empírica ODS9
NV.IND.MANF.ZS EG.ELC.ACCS.ZS IT.NET.USER.ZS GB.XPD.RSDV.GD.ZS
NV.IND.MANF.ZS 1.00 -0.08 -0.42 -0.25
EG.ELC.ACCS.ZS -0.08 1.00 0.67 0.42
IT.NET.USER.ZS -0.42 0.67 1.00 0.54
GB.XPD.RSDV.GD.ZS -0.25 0.42 0.54 1.00
# Teóricas (comentarios):
# - Manufactura ↔ Electricidad (+)
# - I+D ↔ Internet (+)
# - Electricidad ↔ Internet (+)

6. ANÁLISIS FACTORIAL PCA

# Escalar datos
pca_data_ods9 <- scale(data_corr)
# PCA
pca_ods9 <- prcomp(pca_data_ods9, center = FALSE, scale. = FALSE)

# Cargas PC1-PC2
df_cargas_ods9 <- as.data.frame(pca_ods9$rotation[,1:2])
colnames(df_cargas_ods9) <- c("PC1","PC2")
df_cargas_ods9 %>% kable(caption = "Cargas factoriales ODS9")
Cargas factoriales ODS9
PC1 PC2
NV.IND.MANF.ZS -0.3375451 0.8705741
EG.ELC.ACCS.ZS 0.5166514 0.4870955
IT.NET.USER.ZS 0.6055066 0.0129801
GB.XPD.RSDV.GD.ZS 0.5024902 0.0683393
# Scree Plot
plot(pca_ods9, type = "l", main = "Scree Plot PCA ODS9")

7. CREACIÓN DE ÍNDICE SINTÉTICO ODS9

# Normalización min-max
dat_norm_ods9 <- data_ods9 %>%
  mutate(across(all_of(ods9_indicators), ~(. - min(., na.rm=TRUE))/(max(., na.rm=TRUE)-min(., na.rm=TRUE)),
                .names = "min_{col}"))

# Pesos iguales
dat_norm_ods9 <- dat_norm_ods9 %>%
  rowwise() %>%
  mutate(IndexODS9_eq = mean(c_across(starts_with("min_")), na.rm=TRUE)) %>%
  ungroup()

# Pesos PCA
pesos_ods9 <- pca_ods9$rotation[,1]
pesos_ods9 <- pesos_ods9 / sum(pesos_ods9)

# Calcular índice con pesos PCA
dat_norm_ods9 <- dat_norm_ods9 %>%
  rowwise() %>%
  mutate(IndexODS9_pca = sum(pesos_ods9 * c_across(all_of(ods9_indicators)), na.rm=TRUE)) %>%
  ungroup()

# Gráfico de serie del índice PCA
ggplot(dat_norm_ods9, aes(x = as.integer(date), y = IndexODS9_pca, color = region)) +
  geom_line() +
  labs(title = "Índice Sintético ODS9 (PCA)", x = "Año", y = "Índice") +
  theme_minimal()

8. EXPORTACIÓN A EXCEL

# Preparar tablas
tabla_resumen_ods9 <- estad_ods9
tabla_indice_ods9  <- dat_norm_ods9 %>% select(iso3c, country, date, IndexODS9_eq, IndexODS9_pca)

write_xlsx(
  list(
    "Descriptivas_ODS9" = tabla_resumen_ods9,
    "Cargas_PCA_ODS9"   = df_cargas_ods9,
    "Indices_ODS9"      = tabla_indice_ods9
  ),
  path = "Resultados_ODS9.xlsx"
)