rm(list = ls())

# Instalar y cargar paquetes
if (!require(tidyverse)) install.packages("tidyverse")
## Cargando paquete requerido: tidyverse
## Warning: package 'ggplot2' was built under R version 4.4.2
## Warning: package 'readr' was built under R version 4.4.2
## Warning: package 'dplyr' was built under R version 4.4.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
if (!require(ggplot2)) install.packages("ggplot2")
if (!require(cluster)) install.packages("cluster")
## Cargando paquete requerido: cluster
## Warning: package 'cluster' was built under R version 4.4.2
if (!require(factoextra)) install.packages("factoextra")
## Cargando paquete requerido: factoextra
## Warning: package 'factoextra' was built under R version 4.4.2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
if (!require(corrplot)) install.packages("corrplot")
## Cargando paquete requerido: corrplot
## Warning: package 'corrplot' was built under R version 4.4.2
## corrplot 0.95 loaded
if (!require(car)) install.packages("car")
## Cargando paquete requerido: car
## Warning: package 'car' was built under R version 4.4.2
## Cargando paquete requerido: carData
## Warning: package 'carData' was built under R version 4.4.2
## 
## Adjuntando el paquete: 'car'
## 
## The following object is masked from 'package:dplyr':
## 
##     recode
## 
## The following object is masked from 'package:purrr':
## 
##     some
if (!require(broom)) install.packages("broom")
## Cargando paquete requerido: broom
if (!require(ggrepel)) install.packages("ggrepel")
## Cargando paquete requerido: ggrepel
## Warning: package 'ggrepel' was built under R version 4.4.2
if (!require(shiny)) install.packages("shiny")
## Cargando paquete requerido: shiny
## Warning: package 'shiny' was built under R version 4.4.2
if (!require(shinydashboard)) install.packages("shinydashboard")
## Cargando paquete requerido: shinydashboard
## Warning: package 'shinydashboard' was built under R version 4.4.2
## 
## Adjuntando el paquete: 'shinydashboard'
## 
## The following object is masked from 'package:graphics':
## 
##     box
if (!require(readxl)) install.packages("readxl")
## Cargando paquete requerido: readxl
## Warning: package 'readxl' was built under R version 4.4.2
library(tidyverse)
library(ggplot2)
library(cluster)
library(factoextra)
library(corrplot)
library(car)
library(broom)
library(ggrepel)
library(shiny)
library(shinydashboard)
library(readxl)

# Cargar datos
library(readxl)

# URL de la base de datos en Google Drive
file_url <- "https://docs.google.com/uc?export=download&id=1y-Tdgx_kDH3rdElP7-nHLNktUZxzRn_J"

# Descargar el archivo temporalmente
temp_file <- tempfile(fileext = ".xlsx")
download.file(file_url, temp_file, mode = "wb")

# Leer el archivo Excel
df <- read_excel(temp_file)

# Eliminar el archivo temporal
unlink(temp_file)

# Validación y limpieza básica de datos
df <- df %>% 
  filter(complete.cases(.)) %>%
  mutate(across(where(is.numeric), scale)) # Escalar variables numéricas


# --- Análisis Descriptivo ---
# Histograma de Democracia
histograma_democracia <- function() {
  ggplot(df, aes(x = democracia)) +
    geom_histogram(binwidth = 0.5, fill = "blue", color = "black") +
    theme_minimal() +
    labs(title = "Distribución del Índice de Democracia", x = "Democracia", y = "Frecuencia")
}

# Matriz de correlación
correlación <- function() {
  cor_matrix <- cor(df %>% select(-pais), use = "complete.obs")
  corrplot::corrplot(cor_matrix, method = "circle", type = "upper", tl.cex = 0.8)
}

# Tabla Resumen
tabla_resumen <- function() {
  resumen <- df %>%
    summarise(across(where(is.numeric), 
                     list(promedio = mean, mediana = median, sd = sd, min = min, max = max), 
                     .names = "{.col}_{.fn}"))
  return(resumen)
}

# Gráfica de Densidad
densidad_democracia <- function() {
  ggplot(df, aes(x = democracia)) +
    geom_density(fill = "blue", alpha = 0.5) +
    theme_minimal() +
    labs(title = "Densidad del Índice de Democracia", x = "Democracia", y = "Densidad")
}

# Boxplot
boxplot_democracia <- function() {
  ggplot(df, aes(y = democracia)) +
    geom_boxplot(fill = "cyan", color = "darkblue") +
    theme_minimal() +
    labs(title = "Boxplot del Índice de Democracia", y = "Democracia")
}

# --- Modelos de Regresión ---
# Regresión Lineal Multivariada
regresion_lineal <- function() {
  # Ajustar el modelo
  modelo <- lm(democracia ~ estabilidad + propiedad + pib + inversion + idh + patentes, data = df)
  
  # Agregar valores ajustados y residuales al dataframe
  df <- df %>%
    mutate(prediccion = predict(modelo), residual = democracia - prediccion)
  
  # Gráfico de valores reales vs ajustados
  ggplot(df, aes(x = prediccion, y = democracia)) +
    geom_point(color = "blue", alpha = 0.7) +
    geom_abline(slope = 1, intercept = 0, color = "red", linetype = "dashed") +
    theme_minimal() +
    labs(
      title = "Regresión",
      x = "valores de institucion inclusiva",
      y = "Democracia (Valores Reales)"
    )
}

# --- Análisis de Clusterización ---
# AGNES (Jerárquico)
agnes_cluster <- function() {
  modelo <- cluster::agnes(df %>% select(estabilidad, propiedad, pib, inversion, idh, patentes), 
                            metric = "euclidean", method = "average")
  fviz_dend(modelo, main = "Dendrograma (AGNES)", rect = TRUE, show_labels = FALSE)
}

# DIANA (Jerárquico)
diana_cluster <- function() {
  modelo <- cluster::diana(df %>% select(estabilidad, propiedad, pib, inversion, idh, patentes), 
                            metric = "euclidean")
  fviz_dend(modelo, main = "Dendrograma (DIANA)", rect = TRUE, show_labels = FALSE)
}

# PAM (Partición basada en niveles de Democracia)
pam_cluster <- function() {
  modelo <- cluster::pam(df %>% select(democracia, estabilidad, propiedad, pib, inversion, idh, patentes), k = 3)
  df$cluster_pam <- modelo$clustering
  ggplot(df, aes(x = democracia, y = propiedad, color = factor(cluster_pam))) +
    geom_point(size = 3) +
    labs(title = "Clusterización PAM (Basado en Democracia)", x = "Democracia", y = "Propiedad") +
    theme_minimal()
}

# GAP Statistic (para determinar el número óptimo de clusters)

gap_stat <- function() {
  gap_model <- cluster::clusGap(df %>% select(estabilidad, propiedad, pib, inversion, idh, patentes), 
                                FUN = kmeans, K.max = 5, B = 50)
  fviz_gap_stat(gap_model)
}

# --- Dashboard ---
ui <- dashboardPage(
  dashboardHeader(title = "Dashboard de Análisis"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Análisis Descriptivo", tabName = "descriptivo", icon = icon("chart-bar")),
      menuItem("Modelos de Regresión", tabName = "regresion", icon = icon("chart-line")),
      menuItem("Clusterización", tabName = "cluster", icon = icon("object-group"))
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "descriptivo",
              fluidRow(
                box(plotOutput("hist_democracia"), width = 6, title = "Distribución de Democracia"),
                box(plotOutput("matriz_correlacion"), width = 6, title = "Matriz de Correlación"),
                box(plotOutput("densidad_democracia"), width = 6, title = "Densidad de Democracia"),
                box(plotOutput("boxplot_democracia"), width = 6, title = "Boxplot de Democracia"),
                box(tableOutput("tabla_resumen"), width = 12, title = "Tabla Resumen Estadístico")
              )),
      tabItem(tabName = "regresion",
              fluidRow(
                box(plotOutput("regresion_lineal"), width = 12, title = "Modelo de Regresión Lineal")
              )),
      tabItem(tabName = "cluster",
              fluidRow(
                box(plotOutput("agnes"), width = 6, title = "Clusterización AGNES"),
                box(plotOutput("diana"), width = 6, title = "Clusterización DIANA"),
                box(plotOutput("pam"), width = 6, title = "Clusterización PAM"),
                box(plotOutput("gap"), width = 6, title = "Estadística GAP")
              ))
    )
  )
)

server <- function(input, output) {
  # Análisis descriptivo
  output$hist_democracia <- renderPlot({ histograma_democracia() })
  output$matriz_correlacion <- renderPlot({ correlación() })
  output$densidad_democracia <- renderPlot({ densidad_democracia() })
  output$boxplot_democracia <- renderPlot({ boxplot_democracia() })
  output$tabla_resumen <- renderTable({ tabla_resumen() })
  
  # Modelos de regresión
  output$regresion_lineal <- renderPlot({ regresion_lineal() })
  
  # Clusterización
  output$agnes <- renderPlot({ agnes_cluster() })
  output$diana <- renderPlot({ diana_cluster() })
  output$pam <- renderPlot({ pam_cluster() })
  output$gap <- renderPlot({ gap_stat() })
}

shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents