library(mFilter)
library(forecast)
## Warning: package 'forecast' was built under R version 4.3.3
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(lmtest)
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.3.3
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
library(tseries)
## Warning: package 'tseries' was built under R version 4.3.3
library(stats)
library(fBasics)
## Warning: package 'fBasics' was built under R version 4.3.3
library(grid)
library(openxlsx)
## Warning: package 'openxlsx' was built under R version 4.3.3
library(dynlm)
library(urca)
## Warning: package 'urca' was built under R version 4.3.3
library(FinTS)
## Warning: package 'FinTS' was built under R version 4.3.2
## 
## Attaching package: 'FinTS'
## The following object is masked from 'package:forecast':
## 
##     Acf
library(MTS)
## 
## Attaching package: 'MTS'
## The following object is masked from 'package:FinTS':
## 
##     apca
library(ggrepel)
## Warning: package 'ggrepel' was built under R version 4.3.3
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
library(readxl)
library(readxl)
Fondos_de_Renta_Fija_1 <- read_excel("Fondos de Renta Fija_1.xlsx")
summary(Fondos_de_Renta_Fija_1)
##      Dates                        CAMDCTA CB Equity RLVALBC CB Equity
##  Min.   :2016-02-29 00:00:00.00   Min.   :10009     Min.   :11978    
##  1st Qu.:2018-08-15 12:00:00.00   1st Qu.:11539     1st Qu.:13812    
##  Median :2021-01-31 00:00:00.00   Median :15472     Median :15010    
##  Mean   :2021-01-29 09:40:50.42   Mean   :19640     Mean   :15683    
##  3rd Qu.:2023-07-15 12:00:00.00   3rd Qu.:28061     3rd Qu.:17331    
##  Max.   :2025-12-31 00:00:00.00   Max.   :32023     Max.   :21690    
##                                                                      
##  FCABBPU CB Equity FGLDEEM CB Equity ALICXCA CB Equity CRCCFEC CB Equity
##  Min.   : 8949     Min.   :10881     Min.   :12493     Min.   :10819    
##  1st Qu.: 9562     1st Qu.:13203     1st Qu.:15067     1st Qu.:12424    
##  Median :10343     Median :14011     Median :17024     Median :14257    
##  Mean   :10750     Mean   :14246     Mean   :17614     Mean   :14180    
##  3rd Qu.:12042     3rd Qu.:15109     3rd Qu.:19894     3rd Qu.:15974    
##  Max.   :13104     Max.   :18118     Max.   :24900     Max.   :17447    
##  NA's   :57                                            NA's   :62       
##  ALIGODB CB Equity ITCOPTC CB Equity PENSIO4 CB Equity ALT120A CB Equity
##  Min.   :10007     Min.   :   9906   Min.   :12113     Min.   :10002    
##  1st Qu.:10635     1st Qu.:  10337   1st Qu.:14507     1st Qu.:10656    
##  Median :11176     Median :  12421   Median :16392     Median :11448    
##  Mean   :12124     Mean   :1174547   Mean   :17410     Mean   :12070    
##  3rd Qu.:13848     3rd Qu.:3624557   3rd Qu.:20180     3rd Qu.:13765    
##  Max.   :15811     Max.   :4137827   Max.   :24727     Max.   :15429    
##  NA's   :37                          NA's   :13        NA's   :57       
##  FAAPPM1 CB Equity BBVEFEA CB Equity CONSOLI CB Equity FIDUCTA CB Equity
##  Min.   :119964    Min.   :2043      Min.   :18862     Min.   :24699    
##  1st Qu.:134834    1st Qu.:2376      1st Qu.:20640     1st Qu.:28452    
##  Median :142736    Median :2631      Median :21759     Median :30827    
##  Mean   :148256    Mean   :2766      Mean   :23265     Mean   :32398    
##  3rd Qu.:159611    3rd Qu.:3120      3rd Qu.:25836     3rd Qu.:35812    
##  Max.   :193764    Max.   :3938      Max.   :31023     Max.   :45008    
##                                      NA's   :13                         
##  DEREC26 CB Equity LIQBYRB CB Equity OMEFECF CB Equity FINTERES CB Equity
##  Min.   :10025     Min.   :15493     Min.   :2448      Min.   :15199     
##  1st Qu.:11405     1st Qu.:17901     1st Qu.:2558      1st Qu.:17611     
##  Median :12766     Median :19420     Median :2558      Median :19297     
##  Mean   :13664     Mean   :20324     Mean   :2935      Mean   :19933     
##  3rd Qu.:16016     3rd Qu.:22420     3rd Qu.:3349      3rd Qu.:21926     
##  Max.   :18941     Max.   :28181     Max.   :4041      Max.   :27045     
##  NA's   :37                          NA's   :15                          
##  ABIERCA CB Equity ITMMKTC CB Equity CAMFVG2 CB Equity CCAPAIS CB Equity
##  Min.   :14955     Min.   :1984      Min.   :14055     Min.   :12892    
##  1st Qu.:16471     1st Qu.:2277      1st Qu.:14454     1st Qu.:14925    
##  Median :17551     Median :2508      Median :16764     Median :16278    
##  Mean   :18697     Mean   :2634      Mean   :16851     Mean   :16994    
##  3rd Qu.:20817     3rd Qu.:2937      3rd Qu.:18857     3rd Qu.:18838    
##  Max.   :25220     Max.   :3725      Max.   :20491     Max.   :23470    
##  NA's   :12                          NA's   :64                         
##  PARF901 CB Equity FIDUEXC CB Equity RENTFIP CB Equity COVPLT1 CB Equity
##  Min.   :13396     Min.   :13088     Min.   :10282     Min.   :32844    
##  1st Qu.:14778     1st Qu.:15058     1st Qu.:12248     1st Qu.:35905    
##  Median :15663     Median :16369     Median :14073     Median :37854    
##  Mean   :16503     Mean   :17180     Mean   :13937     Mean   :40191    
##  3rd Qu.:18134     3rd Qu.:19019     3rd Qu.:15685     3rd Qu.:44275    
##  Max.   :21537     Max.   :23918     Max.   :17954     Max.   :53303    
##  NA's   :11                                            NA's   :9        
##  ACCIVAL CB Equity BBVFAMA CB Equity   GT10 Govt         COLTES      
##  Min.   :5337      Min.   :2006      Min.   :0.529   Min.   : 83.75  
##  1st Qu.:6096      1st Qu.:2329      1st Qu.:1.714   1st Qu.: 96.48  
##  Median :6628      Median :2553      Median :2.503   Median :101.39  
##  Mean   :6904      Mean   :2648      Mean   :2.688   Mean   :101.47  
##  3rd Qu.:7561      3rd Qu.:2927      3rd Qu.:3.892   3rd Qu.:107.07  
##  Max.   :9464      Max.   :3632      Max.   :4.932   Max.   :116.28  
##                                                                      
##  Dividend Yield of Market
##  Min.   : 1.925          
##  1st Qu.: 2.767          
##  Median : 3.852          
##  Mean   : 5.255          
##  3rd Qu.: 7.533          
##  Max.   :11.752          
## 

PUNTO 1

cols_excluir <- c("Dates", "Dividend Yield of Market", "COLTES", "GT10 Govt")
cols_fondos <- setdiff(names(Fondos_de_Renta_Fija_1), cols_excluir)
fondos_Rp <- Fondos_de_Renta_Fija_1 %>%
  arrange(Dates) %>%
  mutate(across(all_of(cols_fondos), ~ . / lag(.) - 1, .names = "Rp_{.col}")) %>%
  select(Dates, starts_with("Rp_")) %>%
  slice(-1)
fondos_Rlog <- Fondos_de_Renta_Fija_1 %>%
  arrange(Dates) %>%
  mutate(across(all_of(cols_fondos), ~ log(. / lag(.)), .names = "Rlog_{.col}")) %>%
  select(Dates, starts_with("Rlog_")) %>%
  slice(-1)
retornosprecios_long <- fondos_Rp %>%
  select(Dates, starts_with("Rp_")) %>%
  pivot_longer(cols = -Dates, 
               names_to = "fondo", 
               values_to = "retorno")

retornosprecios_long$Dates <- as.Date(retornosprecios_long$Dates, format = "%Y-%m-%d")

ggplot(retornosprecios_long, aes(x = Dates, y = retorno, color = fondo)) +
  geom_line(linewidth = 0.5) +
  facet_wrap(~ fondo, scales = "free_y", ncol = 4) +
  
  # Limitar el eje Y para eliminar el efecto de outliers
  coord_cartesian(ylim = c(-0.05, 0.05)) +  # ajusta según tus datos
  
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
  scale_x_date(date_breaks = "3 years", date_labels = "%Y") +
  
  theme_minimal(base_size = 9) +
  theme(
    legend.position = "none",
    strip.text = element_text(size = 6.5, face = "bold"),
    axis.text.x = element_text(angle = 45, hjust = 1, size = 6),
    axis.text.y = element_text(size = 6),
    panel.spacing = unit(0.5, "lines"),
    plot.title = element_text(hjust = 0.5, size = 14)
  ) +
  labs(title = "Retornos de precio mensuales por fondo",
       x = "Fecha", y = "Retorno")
## Warning: Removed 387 rows containing missing values or values outside the scale range
## (`geom_line()`).

retornoslog_long <- fondos_Rlog %>%
  select(Dates, starts_with("Rlog_")) %>%
  pivot_longer(cols = -Dates, 
               names_to = "fondo", 
               values_to = "retorno")

retornoslog_long$Dates <- as.Date(retornoslog_long$Dates, format = "%Y-%m-%d")


ggplot(retornoslog_long, aes(x = Dates, y = retorno, color = fondo)) +
  geom_line(linewidth = 0.5) +
  facet_wrap(~ fondo, scales = "free_y", ncol = 4) +
  
  coord_cartesian(ylim = c(-0.05, 0.05)) +  
  
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
  scale_x_date(date_breaks = "3 years", date_labels = "%Y") +
  
  theme_minimal(base_size = 9) +
  theme(
    legend.position = "none",
    strip.text = element_text(size = 6.5, face = "bold"),
    axis.text.x = element_text(angle = 45, hjust = 1, size = 6),
    axis.text.y = element_text(size = 6),
    panel.spacing = unit(0.5, "lines"),
    plot.title = element_text(hjust = 0.5, size = 14)
  ) +
  labs(title = "Retornos logaritmicos mensuales por fondo",
       x = "Fecha", y = "Retorno")
## Warning: Removed 387 rows containing missing values or values outside the scale range
## (`geom_line()`).

#RESPUESTA

#PUNTO 2

cols_fondos_log <- setdiff(names(fondos_Rlog), "Dates")

resultados_adf <- lapply(cols_fondos_log, function(col) {
  serie <- na.omit(fondos_Rlog[[col]])  # eliminar NA
  
  if (length(serie) > 10) {  # verificar que haya suficientes datos
    test <- adf.test(serie)
    data.frame(
      Fondo     = col,
      Statistic = round(test$statistic, 4),
      P_value   = round(test$p.value, 4),
      Estacionaria = ifelse(test$p.value < 0.05, "Sí", "No")
    )
  }
})
## Warning in adf.test(serie): p-value smaller than printed p-value
## Warning in adf.test(serie): p-value greater than printed p-value
## Warning in adf.test(serie): p-value smaller than printed p-value

## Warning in adf.test(serie): p-value smaller than printed p-value

## Warning in adf.test(serie): p-value smaller than printed p-value
resultados_adf <- do.call(rbind, resultados_adf)
print(resultados_adf)
##                                   Fondo Statistic P_value Estacionaria
## Dickey-Fuller    Rlog_CAMDCTA CB Equity   -4.7260  0.0100           Sí
## Dickey-Fuller1   Rlog_RLVALBC CB Equity    0.4907  0.9900           No
## Dickey-Fuller2   Rlog_FCABBPU CB Equity   -0.9388  0.9402           No
## Dickey-Fuller3   Rlog_FGLDEEM CB Equity   -4.2609  0.0100           Sí
## Dickey-Fuller4   Rlog_ALICXCA CB Equity   -1.6396  0.7262           No
## Dickey-Fuller5   Rlog_CRCCFEC CB Equity   -3.7582  0.0279           Sí
## Dickey-Fuller6   Rlog_ALIGODB CB Equity   -1.6683  0.7116           No
## Dickey-Fuller7   Rlog_ITCOPTC CB Equity   -4.8165  0.0100           Sí
## Dickey-Fuller8   Rlog_PENSIO4 CB Equity   -1.8628  0.6335           No
## Dickey-Fuller9   Rlog_ALT120A CB Equity   -3.5549  0.0445           Sí
## Dickey-Fuller10  Rlog_FAAPPM1 CB Equity   -2.0369  0.5613           No
## Dickey-Fuller11  Rlog_BBVEFEA CB Equity   -1.9017  0.6174           No
## Dickey-Fuller12  Rlog_CONSOLI CB Equity   -1.5730  0.7537           No
## Dickey-Fuller13  Rlog_FIDUCTA CB Equity   -1.8292  0.6475           No
## Dickey-Fuller14  Rlog_DEREC26 CB Equity   -1.0931  0.9179           No
## Dickey-Fuller15  Rlog_LIQBYRB CB Equity   -2.5219  0.3599           No
## Dickey-Fuller16  Rlog_OMEFECF CB Equity   -3.8699  0.0182           Sí
## Dickey-Fuller17 Rlog_FINTERES CB Equity   -3.1877  0.0933           No
## Dickey-Fuller18  Rlog_ABIERCA CB Equity   -1.9996  0.5768           No
## Dickey-Fuller19  Rlog_ITMMKTC CB Equity   -2.1807  0.5016           No
## Dickey-Fuller20  Rlog_CAMFVG2 CB Equity   -2.3619  0.4288           No
## Dickey-Fuller21  Rlog_CCAPAIS CB Equity   -2.0151  0.5703           No
## Dickey-Fuller22  Rlog_PARF901 CB Equity   -2.5539  0.3469           No
## Dickey-Fuller23  Rlog_FIDUEXC CB Equity   -2.0463  0.5574           No
## Dickey-Fuller24  Rlog_RENTFIP CB Equity   -4.3241  0.0100           Sí
## Dickey-Fuller25  Rlog_COVPLT1 CB Equity   -2.2882  0.4571           No
## Dickey-Fuller26  Rlog_ACCIVAL CB Equity   -2.0894  0.5395           No
## Dickey-Fuller27  Rlog_BBVFAMA CB Equity   -2.2351  0.4790           No
cols_fondos_log <- setdiff(names(fondos_Rlog), "Dates")

# Aplicar PP a cada fondo
resultados_pp <- lapply(cols_fondos_log, function(col) {
  serie <- na.omit(fondos_Rlog[[col]])
  
  if (length(serie) > 10) {
    test <- pp.test(serie)
    data.frame(
      Fondo        = col,
      Statistic    = round(test$statistic, 4),
      P_value      = round(test$p.value, 4),
      Estacionaria = ifelse(test$p.value < 0.05, "Sí", "No")
    )
  }
})
## Warning in pp.test(serie): p-value smaller than printed p-value
## Warning in pp.test(serie): p-value greater than printed p-value
## Warning in pp.test(serie): p-value smaller than printed p-value

## Warning in pp.test(serie): p-value smaller than printed p-value

## Warning in pp.test(serie): p-value smaller than printed p-value

## Warning in pp.test(serie): p-value smaller than printed p-value

## Warning in pp.test(serie): p-value smaller than printed p-value

## Warning in pp.test(serie): p-value smaller than printed p-value

## Warning in pp.test(serie): p-value smaller than printed p-value

## Warning in pp.test(serie): p-value smaller than printed p-value

## Warning in pp.test(serie): p-value smaller than printed p-value

## Warning in pp.test(serie): p-value smaller than printed p-value

## Warning in pp.test(serie): p-value smaller than printed p-value

## Warning in pp.test(serie): p-value smaller than printed p-value

## Warning in pp.test(serie): p-value smaller than printed p-value

## Warning in pp.test(serie): p-value smaller than printed p-value

## Warning in pp.test(serie): p-value smaller than printed p-value

## Warning in pp.test(serie): p-value smaller than printed p-value
# Convertir a DataFrame
resultados_pp <- do.call(rbind, resultados_pp)
print(resultados_pp)
##                                            Fondo Statistic P_value Estacionaria
## Dickey-Fuller Z(alpha)    Rlog_CAMDCTA CB Equity -116.7498  0.0100           Sí
## Dickey-Fuller Z(alpha)1   Rlog_RLVALBC CB Equity    9.5543  0.9900           No
## Dickey-Fuller Z(alpha)2   Rlog_FCABBPU CB Equity  -25.1035  0.0141           Sí
## Dickey-Fuller Z(alpha)3   Rlog_FGLDEEM CB Equity -100.9082  0.0100           Sí
## Dickey-Fuller Z(alpha)4   Rlog_ALICXCA CB Equity  -51.8541  0.0100           Sí
## Dickey-Fuller Z(alpha)5   Rlog_CRCCFEC CB Equity  -54.7283  0.0100           Sí
## Dickey-Fuller Z(alpha)6   Rlog_ALIGODB CB Equity  -34.8059  0.0100           Sí
## Dickey-Fuller Z(alpha)7   Rlog_ITCOPTC CB Equity -116.9526  0.0100           Sí
## Dickey-Fuller Z(alpha)8   Rlog_PENSIO4 CB Equity   -6.9585  0.7131           No
## Dickey-Fuller Z(alpha)9   Rlog_ALT120A CB Equity  -79.3908  0.0100           Sí
## Dickey-Fuller Z(alpha)10  Rlog_FAAPPM1 CB Equity  -38.6070  0.0100           Sí
## Dickey-Fuller Z(alpha)11  Rlog_BBVEFEA CB Equity  -47.4114  0.0100           Sí
## Dickey-Fuller Z(alpha)12  Rlog_CONSOLI CB Equity  -17.5615  0.0992           No
## Dickey-Fuller Z(alpha)13  Rlog_FIDUCTA CB Equity  -16.6851  0.1505           No
## Dickey-Fuller Z(alpha)14  Rlog_DEREC26 CB Equity   -5.5221  0.7936           No
## Dickey-Fuller Z(alpha)15  Rlog_LIQBYRB CB Equity  -57.0299  0.0100           Sí
## Dickey-Fuller Z(alpha)16  Rlog_OMEFECF CB Equity -100.0044  0.0100           Sí
## Dickey-Fuller Z(alpha)17 Rlog_FINTERES CB Equity -164.7440  0.0100           Sí
## Dickey-Fuller Z(alpha)18  Rlog_ABIERCA CB Equity  -49.7339  0.0100           Sí
## Dickey-Fuller Z(alpha)19  Rlog_ITMMKTC CB Equity  -22.9842  0.0310           Sí
## Dickey-Fuller Z(alpha)20  Rlog_CAMFVG2 CB Equity  -16.1314  0.1433           No
## Dickey-Fuller Z(alpha)21  Rlog_CCAPAIS CB Equity  -23.9163  0.0241           Sí
## Dickey-Fuller Z(alpha)22  Rlog_PARF901 CB Equity  -54.9807  0.0100           Sí
## Dickey-Fuller Z(alpha)23  Rlog_FIDUEXC CB Equity  -20.8628  0.0492           Sí
## Dickey-Fuller Z(alpha)24  Rlog_RENTFIP CB Equity -108.5666  0.0100           Sí
## Dickey-Fuller Z(alpha)25  Rlog_COVPLT1 CB Equity  -34.8522  0.0100           Sí
## Dickey-Fuller Z(alpha)26  Rlog_ACCIVAL CB Equity  -26.2242  0.0151           Sí
## Dickey-Fuller Z(alpha)27  Rlog_BBVFAMA CB Equity  -34.3275  0.0100           Sí
cols_fondos_log <- setdiff(names(fondos_Rlog), "Dates")

resultados_integracion <- lapply(cols_fondos_log, function(col) {
  serie <- na.omit(fondos_Rlog[[col]])
  
  if (length(serie) > 10) {
    
    # Prueba en niveles (retornos log)
    adf_nivel <- adf.test(serie)
    pp_nivel  <- pp.test(serie)
    
    # Prueba en primera diferencia
    adf_diff <- adf.test(diff(serie))
    pp_diff  <- pp.test(diff(serie))
    
    # Determinar orden de integración
    estacionaria_nivel <- adf_nivel$p.value < 0.05 & pp_nivel$p.value < 0.05
    estacionaria_diff  <- adf_diff$p.value  < 0.05 & pp_diff$p.value  < 0.05
    
    orden <- ifelse(estacionaria_nivel, "I(0)",
             ifelse(estacionaria_diff,  "I(1)", "I(2)+"))
    
    data.frame(
      Fondo           = col,
      ADF_nivel       = round(adf_nivel$p.value, 4),
      PP_nivel        = round(pp_nivel$p.value,  4),
      ADF_diff        = round(adf_diff$p.value,  4),
      PP_diff         = round(pp_diff$p.value,   4),
      Orden_Integracion = orden
    )
  }
})
## Warning in adf.test(serie): p-value smaller than printed p-value
## Warning in pp.test(serie): p-value smaller than printed p-value
## Warning in adf.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
## Warning in adf.test(serie): p-value greater than printed p-value
## Warning in pp.test(serie): p-value greater than printed p-value
## Warning in adf.test(diff(serie)): p-value greater than printed p-value
## Warning in pp.test(diff(serie)): p-value greater than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
## Warning in adf.test(serie): p-value smaller than printed p-value
## Warning in pp.test(serie): p-value smaller than printed p-value
## Warning in adf.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(serie): p-value smaller than printed p-value
## Warning in adf.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(serie): p-value smaller than printed p-value
## Warning in adf.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(serie): p-value smaller than printed p-value
## Warning in adf.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
## Warning in adf.test(serie): p-value smaller than printed p-value
## Warning in pp.test(serie): p-value smaller than printed p-value
## Warning in adf.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value

## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(serie): p-value smaller than printed p-value
## Warning in adf.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(serie): p-value smaller than printed p-value
## Warning in adf.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(serie): p-value smaller than printed p-value
## Warning in adf.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
## Warning in adf.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
## Warning in adf.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value

## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(serie): p-value smaller than printed p-value
## Warning in adf.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(serie): p-value smaller than printed p-value
## Warning in adf.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(serie): p-value smaller than printed p-value
## Warning in adf.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(serie): p-value smaller than printed p-value
## Warning in adf.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
## Warning in adf.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
## Warning in adf.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
## Warning in adf.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(serie): p-value smaller than printed p-value
## Warning in adf.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
## Warning in adf.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
## Warning in adf.test(serie): p-value smaller than printed p-value
## Warning in pp.test(serie): p-value smaller than printed p-value
## Warning in adf.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(serie): p-value smaller than printed p-value
## Warning in adf.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
## Warning in adf.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(serie): p-value smaller than printed p-value
## Warning in adf.test(diff(serie)): p-value smaller than printed p-value
## Warning in pp.test(diff(serie)): p-value smaller than printed p-value
resultados_integracion <- do.call(rbind, resultados_integracion)
print(resultados_integracion)
##                      Fondo ADF_nivel PP_nivel ADF_diff PP_diff
## 1   Rlog_CAMDCTA CB Equity    0.0100   0.0100   0.0100    0.01
## 2   Rlog_RLVALBC CB Equity    0.9900   0.9900   0.9900    0.99
## 3   Rlog_FCABBPU CB Equity    0.9402   0.0141   0.6132    0.01
## 4   Rlog_FGLDEEM CB Equity    0.0100   0.0100   0.0100    0.01
## 5   Rlog_ALICXCA CB Equity    0.7262   0.0100   0.0100    0.01
## 6   Rlog_CRCCFEC CB Equity    0.0279   0.0100   0.0100    0.01
## 7   Rlog_ALIGODB CB Equity    0.7116   0.0100   0.0100    0.01
## 8   Rlog_ITCOPTC CB Equity    0.0100   0.0100   0.0100    0.01
## 9   Rlog_PENSIO4 CB Equity    0.6335   0.7131   0.2054    0.01
## 10  Rlog_ALT120A CB Equity    0.0445   0.0100   0.0100    0.01
## 11  Rlog_FAAPPM1 CB Equity    0.5613   0.0100   0.0100    0.01
## 12  Rlog_BBVEFEA CB Equity    0.6174   0.0100   0.0100    0.01
## 13  Rlog_CONSOLI CB Equity    0.7537   0.0992   0.0100    0.01
## 14  Rlog_FIDUCTA CB Equity    0.6475   0.1505   0.0100    0.01
## 15  Rlog_DEREC26 CB Equity    0.9179   0.7936   0.0777    0.01
## 16  Rlog_LIQBYRB CB Equity    0.3599   0.0100   0.0100    0.01
## 17  Rlog_OMEFECF CB Equity    0.0182   0.0100   0.0100    0.01
## 18 Rlog_FINTERES CB Equity    0.0933   0.0100   0.0100    0.01
## 19  Rlog_ABIERCA CB Equity    0.5768   0.0100   0.0100    0.01
## 20  Rlog_ITMMKTC CB Equity    0.5016   0.0310   0.0100    0.01
## 21  Rlog_CAMFVG2 CB Equity    0.4288   0.1433   0.0100    0.01
## 22  Rlog_CCAPAIS CB Equity    0.5703   0.0241   0.0100    0.01
## 23  Rlog_PARF901 CB Equity    0.3469   0.0100   0.0100    0.01
## 24  Rlog_FIDUEXC CB Equity    0.5574   0.0492   0.0100    0.01
## 25  Rlog_RENTFIP CB Equity    0.0100   0.0100   0.0100    0.01
## 26  Rlog_COVPLT1 CB Equity    0.4571   0.0100   0.0100    0.01
## 27  Rlog_ACCIVAL CB Equity    0.5395   0.0151   0.0100    0.01
## 28  Rlog_BBVFAMA CB Equity    0.4790   0.0100   0.0100    0.01
##    Orden_Integracion
## 1               I(0)
## 2              I(2)+
## 3              I(2)+
## 4               I(0)
## 5               I(1)
## 6               I(0)
## 7               I(1)
## 8               I(0)
## 9              I(2)+
## 10              I(0)
## 11              I(1)
## 12              I(1)
## 13              I(1)
## 14              I(1)
## 15             I(2)+
## 16              I(1)
## 17              I(0)
## 18              I(1)
## 19              I(1)
## 20              I(1)
## 21              I(1)
## 22              I(1)
## 23              I(1)
## 24              I(1)
## 25              I(0)
## 26              I(1)
## 27              I(1)
## 28              I(1)

#RESPUESTA

#PUNTO 3

cols_fondos_log <- setdiff(names(fondos_Rlog), "Dates")

resultados_arima <- lapply(cols_fondos_log, function(col) {
  serie <- na.omit(fondos_Rlog[[col]])
  
  modelo <- auto.arima(serie, 
                       seasonal = FALSE,
                       stepwise = FALSE,
                       approximation = FALSE)
  
  data.frame(
    Fondo  = col,
    p      = arimaorder(modelo)[1],
    d      = arimaorder(modelo)[2],
    q      = arimaorder(modelo)[3],
    AIC    = round(AIC(modelo), 4),
    BIC    = round(BIC(modelo), 4),
    Modelo = paste0("ARIMA(", paste(arimaorder(modelo), collapse = ","), ")")
  )
})

resultados_arima <- do.call(rbind, resultados_arima)
print(resultados_arima)
##                       Fondo p d q        AIC        BIC       Modelo
## p    Rlog_CAMDCTA CB Equity 0 0 0  -189.5430  -186.7723 ARIMA(0,0,0)
## p1   Rlog_RLVALBC CB Equity 0 0 0  -442.8361  -440.0654 ARIMA(0,0,0)
## p2   Rlog_FCABBPU CB Equity 1 0 0  -241.3448  -237.1230 ARIMA(1,0,0)
## p3   Rlog_FGLDEEM CB Equity 0 0 0  -596.0304  -590.4890 ARIMA(0,0,0)
## p4   Rlog_ALICXCA CB Equity 3 1 2 -1100.1997 -1083.6267 ARIMA(3,1,2)
## p5   Rlog_CRCCFEC CB Equity 0 0 0  -157.1312  -155.1059 ARIMA(0,0,0)
## p6   Rlog_ALIGODB CB Equity 3 1 0  -679.2893  -669.7611 ARIMA(3,1,0)
## p7   Rlog_ITCOPTC CB Equity 0 0 0   198.5221   201.2928 ARIMA(0,0,0)
## p8   Rlog_PENSIO4 CB Equity 5 1 0 -1240.0889 -1224.2226 ARIMA(5,1,0)
## p9   Rlog_ALT120A CB Equity 1 0 0  -339.1935  -332.8608 ARIMA(1,0,0)
## p10  Rlog_FAAPPM1 CB Equity 0 1 2 -1037.5442 -1029.2577 ARIMA(0,1,2)
## p11  Rlog_BBVEFEA CB Equity 0 1 1 -1058.1204 -1052.5961 ARIMA(0,1,1)
## p12  Rlog_CONSOLI CB Equity 0 1 2  -994.6221  -986.6890 ARIMA(0,1,2)
## p13  Rlog_FIDUCTA CB Equity 4 1 0 -1126.3671 -1112.5562 ARIMA(4,1,0)
## p14  Rlog_DEREC26 CB Equity 0 1 1  -879.2411  -874.4770 ARIMA(0,1,1)
## p15  Rlog_LIQBYRB CB Equity 0 1 1 -1005.5018  -999.9775 ARIMA(0,1,1)
## p16  Rlog_OMEFECF CB Equity 0 1 1  -659.4894  -654.2394 ARIMA(0,1,1)
## p17 Rlog_FINTERES CB Equity 0 1 2  -832.2350  -823.9485 ARIMA(0,1,2)
## p18  Rlog_ABIERCA CB Equity 0 1 3  -936.4173  -925.8015 ARIMA(0,1,3)
## p19  Rlog_ITMMKTC CB Equity 0 1 2 -1102.0212 -1093.7347 ARIMA(0,1,2)
## p20  Rlog_CAMFVG2 CB Equity 4 1 0  -450.5275  -440.6761 ARIMA(4,1,0)
## p21  Rlog_CCAPAIS CB Equity 0 1 2 -1091.6472 -1083.3606 ARIMA(0,1,2)
## p22  Rlog_PARF901 CB Equity 1 0 2  -799.6697  -786.3056 ARIMA(1,0,2)
## p23  Rlog_FIDUEXC CB Equity 0 1 2 -1106.6042 -1098.3176 ARIMA(0,1,2)
## p24  Rlog_RENTFIP CB Equity 0 0 4  -821.0437  -804.4196 ARIMA(0,0,4)
## p25  Rlog_COVPLT1 CB Equity 4 1 0  -959.4971  -946.0864 ARIMA(4,1,0)
## p26  Rlog_ACCIVAL CB Equity 2 1 3 -1097.4743 -1080.9012 ARIMA(2,1,3)
## p27  Rlog_BBVFAMA CB Equity 0 1 2 -1032.2324 -1023.9459 ARIMA(0,1,2)

#PUNTO 4

# 1. Crear DataFrame con fechas, Rf y Rm
datos_mercado <- Fondos_de_Renta_Fija_1 %>%
  arrange(Dates) %>%
  mutate(
    Rf = `GT10 Govt` / 100 / 12,
    Rm = log(COLTES / lag(COLTES)) + (`Dividend Yield of Market` / 100 / 12)
  ) %>%
  select(Dates, Rf, Rm) %>%
  slice(-1)

# 2. Unir por fecha con los retornos logarítmicos
fondos_CAPM <- fondos_Rlog %>%
  left_join(datos_mercado, by = "Dates")

# 3. Columnas de los fondos
cols_fondos_log <- setdiff(names(fondos_Rlog), "Dates")

# 4. Correr CAPM para cada fondo
resultados_capm <- lapply(cols_fondos_log, function(col) {
  
  datos <- fondos_CAPM %>%
    select(Dates, retorno = all_of(col), Rf, Rm) %>%
    na.omit() %>%
    mutate(
      exceso_fondo   = retorno - Rf,
      exceso_mercado = Rm - Rf
    )
  
  modelo <- lm(exceso_fondo ~ exceso_mercado, data = datos)
  
  data.frame(
    Fondo   = col,
    Alpha   = round(coef(modelo)[1], 3),
    Beta    = round(coef(modelo)[2], 3),
    R2      = round(summary(modelo)$r.squared, 4),
    P_Alpha = round(summary(modelo)$coefficients[1,4], 4),
    P_Beta  = round(summary(modelo)$coefficients[2,4], 4)
  )
})

# 5. Consolidar resultados
resultados_capm <- do.call(rbind, resultados_capm)
print(resultados_capm)
##                                 Fondo  Alpha   Beta     R2 P_Alpha P_Beta
## (Intercept)    Rlog_CAMDCTA CB Equity -0.007  0.494 0.0069  0.4630 0.3721
## (Intercept)1   Rlog_RLVALBC CB Equity -0.001  0.051 0.0006  0.8398 0.7889
## (Intercept)2   Rlog_FCABBPU CB Equity -0.003  0.774 0.1405  0.4806 0.0029
## (Intercept)3   Rlog_FGLDEEM CB Equity  0.000  0.659 0.3900  0.9400 0.0000
## (Intercept)4   Rlog_ALICXCA CB Equity  0.003  0.048 0.1103  0.0000 0.0002
## (Intercept)5   Rlog_CRCCFEC CB Equity -0.001  0.731 0.0390  0.8576 0.1446
## (Intercept)6   Rlog_ALIGODB CB Equity  0.003  0.087 0.1675  0.0000 0.0001
## (Intercept)7   Rlog_ITCOPTC CB Equity -0.050  0.756 0.0006  0.3351 0.7919
## (Intercept)8   Rlog_PENSIO4 CB Equity  0.004  0.014 0.0342  0.0000 0.0588
## (Intercept)9   Rlog_ALT120A CB Equity  0.004 -0.001 0.0000  0.0291 0.9924
## (Intercept)10  Rlog_FAAPPM1 CB Equity  0.002  0.095 0.2426  0.0000 0.0000
## (Intercept)11  Rlog_BBVEFEA CB Equity  0.003  0.074 0.1864  0.0000 0.0000
## (Intercept)12  Rlog_CONSOLI CB Equity  0.002  0.093 0.2853  0.0000 0.0000
## (Intercept)13  Rlog_FIDUCTA CB Equity  0.003  0.079 0.2071  0.0000 0.0000
## (Intercept)14  Rlog_DEREC26 CB Equity  0.005  0.044 0.1961  0.0000 0.0000
## (Intercept)15  Rlog_LIQBYRB CB Equity  0.003  0.094 0.2152  0.0000 0.0000
## (Intercept)16  Rlog_OMEFECF CB Equity  0.003  0.001 0.0000  0.0077 0.9884
## (Intercept)17 Rlog_FINTERES CB Equity  0.002  0.071 0.0329  0.0003 0.0494
## (Intercept)18  Rlog_ABIERCA CB Equity  0.002  0.081 0.1908  0.0000 0.0000
## (Intercept)19  Rlog_ITMMKTC CB Equity  0.003  0.085 0.2379  0.0000 0.0000
## (Intercept)20  Rlog_CAMFVG2 CB Equity  0.004  0.158 0.3450  0.0000 0.0000
## (Intercept)21  Rlog_CCAPAIS CB Equity  0.003  0.100 0.3180  0.0000 0.0000
## (Intercept)22  Rlog_PARF901 CB Equity  0.002  0.193 0.2923  0.0009 0.0000
## (Intercept)23  Rlog_FIDUEXC CB Equity  0.003  0.085 0.2306  0.0000 0.0000
## (Intercept)24  Rlog_RENTFIP CB Equity  0.002  0.199 0.2075  0.0053 0.0000
## (Intercept)25  Rlog_COVPLT1 CB Equity  0.002  0.100 0.2359  0.0000 0.0000
## (Intercept)26  Rlog_ACCIVAL CB Equity  0.002  0.092 0.2875  0.0000 0.0000
## (Intercept)27  Rlog_BBVFAMA CB Equity  0.002  0.125 0.3706  0.0000 0.0000