# ================================================================
# ANÁLISIS DE DATOS DE PANEL: EFECTOS FIJOS Y ALEATORIOS
# ================================================================

# 1. Configuración inicial
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)

# Paquetes necesarios
library(readxl)    # Lectura de Excel
## Warning: package 'readxl' was built under R version 4.4.3
library(dplyr)     # Manipulación de datos
## Warning: package 'dplyr' was built under R version 4.4.3
## 
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(plm)       # Modelos de datos de panel
## Warning: package 'plm' was built under R version 4.4.2
## 
## Adjuntando el paquete: 'plm'
## The following objects are masked from 'package:dplyr':
## 
##     between, lag, lead
library(lmtest)    # Pruebas estadísticas
## Warning: package 'lmtest' was built under R version 4.4.2
## Cargando paquete requerido: zoo
## Warning: package 'zoo' was built under R version 4.4.2
## 
## Adjuntando el paquete: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(sandwich)  # Errores robustos
## Warning: package 'sandwich' was built under R version 4.4.2
library(car)       # Multicolinealidad
## 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
library(tseries)   # Normalidad de residuos
## Warning: package 'tseries' was built under R version 4.4.3
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(ggplot2)   # Visualización
## Warning: package 'ggplot2' was built under R version 4.4.3
# ------------------------------------------------
# 2. Lectura y preparación de datos
# ------------------------------------------------
datos <- read_excel("Panel_datos.xlsx") %>%
  mutate(
    Año = as.numeric(Año),
    Pais = as.factor(Pais),
    ln_exports = log(Exportaciones),
    ln_tc_real = log(tc_real),
    ln_credito = log(credito),
    ln_pibpc = log(pibpc)
  )

# Declarar como datos de panel
panel_data <- pdata.frame(datos, index = c("Pais", "Año"))
# ------------------------------------------------
# 3. Estimación de modelos
# ------------------------------------------------
modelo_fe <- plm(
  ln_exports ~ ln_tc_real + ln_credito + ied + ln_pibpc,
  data = panel_data, model = "within"
)

modelo_re <- plm(
  ln_exports ~ ln_tc_real + ln_credito + ied + ln_pibpc,
  data = panel_data, model = "random"
)

cat("\n=== RESULTADOS EFECTOS FIJOS ===\n")
## 
## === RESULTADOS EFECTOS FIJOS ===
print(summary(modelo_fe))
## Oneway (individual) effect Within Model
## 
## Call:
## plm(formula = ln_exports ~ ln_tc_real + ln_credito + ied + ln_pibpc, 
##     data = panel_data, model = "within")
## 
## Balanced Panel: n = 6, T = 20, N = 120
## 
## Residuals:
##       Min.    1st Qu.     Median    3rd Qu.       Max. 
## -0.3513868 -0.1088991 -0.0076645  0.0885470  0.3527776 
## 
## Coefficients:
##             Estimate Std. Error t-value  Pr(>|t|)    
## ln_tc_real 0.3579758  0.1317055  2.7180  0.007632 ** 
## ln_credito 0.5229497  0.1128161  4.6354 9.854e-06 ***
## ied        0.0117813  0.0067906  1.7349  0.085553 .  
## ln_pibpc   1.9510590  0.1830115 10.6609 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    13.202
## Residual Sum of Squares: 2.8403
## R-Squared:      0.78486
## Adj. R-Squared: 0.76725
## F-statistic: 100.321 on 4 and 110 DF, p-value: < 2.22e-16
cat("\n=== RESULTADOS EFECTOS ALEATORIOS ===\n")
## 
## === RESULTADOS EFECTOS ALEATORIOS ===
print(summary(modelo_re))
## Oneway (individual) effect Random Effect Model 
##    (Swamy-Arora's transformation)
## 
## Call:
## plm(formula = ln_exports ~ ln_tc_real + ln_credito + ied + ln_pibpc, 
##     data = panel_data, model = "random")
## 
## Balanced Panel: n = 6, T = 20, N = 120
## 
## Effects:
##                   var std.dev share
## idiosyncratic 0.02582 0.16069 0.046
## individual    0.53777 0.73333 0.954
## theta: 0.9511
## 
## Residuals:
##       Min.    1st Qu.     Median    3rd Qu.       Max. 
## -0.3306956 -0.1338540 -0.0049222  0.1136873  0.4276227 
## 
## Coefficients:
##              Estimate Std. Error z-value  Pr(>|z|)    
## (Intercept) 2.3334213  1.5968571  1.4613   0.14394    
## ln_tc_real  0.3457925  0.1413622  2.4461   0.01444 *  
## ln_credito  0.5273136  0.1196138  4.4085 1.041e-05 ***
## ied         0.0105340  0.0073063  1.4418   0.14937    
## ln_pibpc    1.9145143  0.1950666  9.8147 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    13.651
## Residual Sum of Squares: 3.4453
## R-Squared:      0.74762
## Adj. R-Squared: 0.73885
## Chisq: 340.67 on 4 DF, p-value: < 2.22e-16
# ------------------------------------------------
# 4. Pruebas de especificación
# ------------------------------------------------
hausman_test <- phtest(modelo_fe, modelo_re)
bp_test <- bptest(modelo_re)                   # Heterocedasticidad
bg_test <- pbgtest(modelo_re)                  # Autocorrelación
vif_test <- vif(lm(ln_exports ~ ln_tc_real + ln_credito + ied + ln_pibpc, data = datos))
jb_test <- jarque.bera.test(residuals(modelo_re)) # Normalidad de residuos
reset_test <- resettest(lm(ln_exports ~ ln_tc_real + ln_credito + ied + ln_pibpc, data = datos))
plm_bp_test <- plmtest(modelo_re, type = "bp")  # Efectos significativos

cat("\n=== PRUEBA DE HAUSMAN ===\n")
## 
## === PRUEBA DE HAUSMAN ===
print(hausman_test)
## 
##  Hausman Test
## 
## data:  ln_exports ~ ln_tc_real + ln_credito + ied + ln_pibpc
## chisq = 0.64189, df = 4, p-value = 0.9583
## alternative hypothesis: one model is inconsistent
cat("\n=== PRUEBAS ADICIONALES ===\n")
## 
## === PRUEBAS ADICIONALES ===
list(
  BP_Heterocedasticidad = bp_test,
  BG_Autocorrelacion = bg_test,
  VIF = vif_test,
  Jarque_Bera = jb_test,
  RESET = reset_test,
  Breusch_Pagan_PL = plm_bp_test
)
## $BP_Heterocedasticidad
## 
##  studentized Breusch-Pagan test
## 
## data:  modelo_re
## BP = 36.48, df = 4, p-value = 2.305e-07
## 
## 
## $BG_Autocorrelacion
## 
##  Breusch-Godfrey/Wooldridge test for serial correlation in panel models
## 
## data:  ln_exports ~ ln_tc_real + ln_credito + ied + ln_pibpc
## chisq = 80.45, df = 20, p-value = 3.292e-09
## alternative hypothesis: serial correlation in idiosyncratic errors
## 
## 
## $VIF
## ln_tc_real ln_credito        ied   ln_pibpc 
##   1.536036   1.306944   1.085246   1.491416 
## 
## $Jarque_Bera
## 
##  Jarque Bera Test
## 
## data:  residuals(modelo_re)
## X-squared = 4.2218, df = 2, p-value = 0.1211
## 
## 
## $RESET
## 
##  RESET test
## 
## data:  lm(ln_exports ~ ln_tc_real + ln_credito + ied + ln_pibpc, data = datos)
## RESET = 2.5288, df1 = 2, df2 = 113, p-value = 0.08426
## 
## 
## $Breusch_Pagan_PL
## 
##  Lagrange Multiplier Test - (Breusch-Pagan)
## 
## data:  ln_exports ~ ln_tc_real + ln_credito + ied + ln_pibpc
## chisq = 611.59, df = 1, p-value < 2.2e-16
## alternative hypothesis: significant effects
# ------------------------------------------------
# 5. Errores robustos (Arellano, agrupados por país)
# ------------------------------------------------
robust_vcov <- vcovHC(modelo_re, method = "arellano", type = "HC1", cluster = "group")
robust_results <- coeftest(modelo_re, vcov = robust_vcov)
cat("\n=== COEFICIENTES CON ERRORES ROBUSTOS ===\n")
## 
## === COEFICIENTES CON ERRORES ROBUSTOS ===
print(robust_results)
## 
## t test of coefficients:
## 
##              Estimate Std. Error t value  Pr(>|t|)    
## (Intercept) 2.3334213  2.7958623  0.8346 0.4056744    
## ln_tc_real  0.3457925  0.3175605  1.0889 0.2784734    
## ln_credito  0.5273136  0.2585487  2.0395 0.0436906 *  
## ied         0.0105340  0.0030586  3.4440 0.0008004 ***
## ln_pibpc    1.9145143  0.3327688  5.7533 7.364e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# ------------------------------------------------
# 7. Visualizaciones
# ------------------------------------------------

# Evolución temporal de exportaciones
ggplot(datos, aes(x = Año, y = ln_exports, color = Pais)) +
  geom_line(linewidth = 1) +
  geom_point() +
  labs(title = "Evolución logarítmica de Exportaciones por país",
       x = "Año", y = "ln(Exportaciones)") +
  theme_minimal()

# Distribución del crédito
ggplot(datos, aes(x = Pais, y = ln_credito, fill = Pais)) +
  geom_boxplot(alpha = 0.7) +
  labs(title = "Distribución del Crédito Doméstico (% PIB) por país",
       y = "ln(Crédito)", x = "País") +
  theme_minimal()

# Relación Crédito vs Exportaciones
ggplot(datos, aes(x = ln_credito, y = ln_exports, color = Pais)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  labs(title = "Relación entre Crédito Doméstico y Exportaciones",
       x = "ln(Crédito)", y = "ln(Exportaciones)") +
  theme_minimal()

# Distribución de PIB per cápita
ggplot(datos, aes(x = ln_pibpc, fill = Pais)) +
  geom_density(alpha = 0.5) +
  labs(title = "Distribución de PIB per cápita (log) por país") +
  theme_minimal()