2017-10-02 8 views
0

J'ai les deux jeux de données suivants. La première est une liste qui se présente comme suit:Changement significatif des changements moyens quotidiens de plusieurs variables

head(CDS_bond_basis) 
     Dates CDS  Bond Swap zero rate CDS-bond basis    Bank 
1 2015-01-22 124.50 194.7738   31.10  -39.17377 AIB Group UK PLC 
2 2015-01-23 124.41 185.0195   27.20  -33.40953 AIB Group UK PLC 
3 2015-01-26 124.41 184.3250   31.50  -28.41500 AIB Group UK PLC 
4 2015-01-27 124.41 184.2980   30.90  -28.98801 AIB Group UK PLC 
5 2015-01-28 124.41 184.7475   27.45  -32.88754 AIB Group UK PLC 
6 2015-01-29 124.41 186.9114   32.05  -30.45136 AIB Group UK PLC 

L'important est la base colonne CDS liaison. Il est simplement calculé par cette formule

CDS-(Bond-Swap à taux zéro)

l'ensemble de données contient 45078 entrées de 25 banques différentes au cours de la période 02.01.2007-30.12.2016.

Le second ensemble de données se présente comme suit:

head(RatingDowngradesFinal_) 
       Bank  Dates Rating agency New rating Previous rating State 
1 ABN AMRO Bank NV 2016-02-17  Moody's   WR    Ba1 NL 
2 ABN AMRO Bank NV 2015-09-29   DBRS   A   AH *- NL 
3 ABN AMRO Bank NV 2015-05-20   DBRS  AH *-    AH NL 
4 ABN AMRO Bank NV 2015-05-20   DBRS  AL *-    AL NL 
5 ABN AMRO Bank NV 2015-05-19   Fitch   A    A+ NL 
6 ABN AMRO Bank NV 2015-05-19   Fitch   A    A+ NL 

Cet ensemble de données contient des informations sur déclassements sur la période.

d'abord tout ce que je voudrais partager toute la période de temps en trois intervalles distincts:

1. 02.01.2007-31.12.2009 

2. 01.01.2010-31.12.2012 

3. 01.01.2013-30.12.2016 

Ensuite, je voudrais résumer les changements quotidiens moyens des variables: CDS, obligations, swaps de taux zéro et base CDS-obligations au cours des intervalles de temps suivants ->

1. [-30,-1] 

2. [1,30] 

3. [31,60] 

4. [61,90] 

5. [-1,1] 

6. [1,10] 

, où, par exemple [-30, -1] représente l'intervalle de temps bewteen 30 jours et 1 jour avant le déclassement et [1,10] représente l'intervalle entre 1 jour et 10 jours à l'arrière er la rétrogradation. Par conséquent, les banques doivent être identiques dans les deux ensembles de données -> AIB Group UK PLC = AIB Group UK PLC.

Une autre difficulté est que mes jeux de données ne sont composés que de jours ouvrables, donc tous les 5 jours, 2 jours sont manquants à cause du week-end.

Merci pour votre aide à l'avance, Ramon

+0

Vérifiez 'HMisc :: cut2'. –

Répondre

2

Ici vous êtes. Il imprime 3 trames de données (une pour chacun des trois intervalles séparés que vous vouliez).

Il y a probablement une façon plus élégante de manipuler toutes les différentes listes et vecteurs, n'hésitez pas à travailler dessus.

library(readxl) 

CDS_bond_basis <- read_excel("CDS-bond basis.xlsx") 
RatingDowngradesFinal_ <- read_excel("RatingDowngradesFinal.xlsx") 
CDS_bond_basis$Dates <- as.Date(CDS_bond_basis$Dates) 
RatingDowngradesFinal_$Dates <- as.Date(RatingDowngradesFinal_$Dates) 

# Ordered Fitch and Moody's rating scale 

fitch <- c("AAA", "AA+ ", "AA", "AA–", "A+", "A ", "A– ", "BBB+", "BBB", "BBB–", "BB+", "BB", "BB–", "B+", "B", "B–", "CCC", "CC", "C", "RD/D") 
moodys <- c("Aaa", "Aa1 *-", "Aa2", "Aa3", "A1", "A2", "A3", "Baa1", "Baa2", "Baa3", "Ba1", "Ba2", "Ba3", "B1", "B2", "B3", "Caa1", "Caa2", "Caa3", "Ca", "C", "WR") 
standardandpoors <- c("AA *-", "AA- *-", "AA", "AA-", "A+", "A+ *-", "A", "A *-", "A-", "A- *-", "BBB+", "BBB+ *-", "BBB", "BBB *-", "BBB-", "BB+ *-", "BB *-", "B") 
dbrs <- c("AAA *-", "AAH *-", "AAH", "AAL *-", "AAL", "AA", "AA *-", "AH *-", "AH", "A", "A *-", "AL", "AL *-", "BBBH", "BBBH *-", "BBB", "BBB *-", "BBBL *-") 

# A way to split your dataframe 

firstPeriod <- split(CDS_bond_basis,as.Date("2007-01-02") <= CDS_bond_basis$Dates & 
         CDS_bond_basis$Dates <= as.Date("2009-12-31"))[2] 
secondPeriod <- split(CDS_bond_basis,as.Date("2010-01-01") <= CDS_bond_basis$Dates & 
         CDS_bond_basis$Dates <= as.Date("2012-12-31"))[2] 
thirdPeriod <- split(CDS_bond_basis,as.Date("2013-01-01") <= CDS_bond_basis$Dates & 
         CDS_bond_basis$Dates <= as.Date("2016-12-30"))[2] 

listIntervals <- list(c(-30, -1), c(1, 30), c(31, 60), c(61, 90), c(-1, 1), c(1, 10)) 

# Create list of vectors that will contain the mean data for each of your 6 intervals, First/Second/Third is used 
# for your "First of all I would like to split the whole time period into three separate intervals" request 

listMeanCDSFirst <- list(c(), c(), c(), c(), c(), c()) 
listMeanBondFirst <- list(c(), c(), c(), c(), c(), c()) 
listMeanSwapZRFirst <- list(c(), c(), c(), c(), c(), c()) 
listMeanCDSbbFirst <- list(c(), c(), c(), c(), c(), c()) 

listMeanCDSSecond <- list(c(), c(), c(), c(), c(), c()) 
listMeanBondSecond <- list(c(), c(), c(), c(), c(), c()) 
listMeanSwapZRSecond <- list(c(), c(), c(), c(), c(), c()) 
listMeanCDSbbSecond <- list(c(), c(), c(), c(), c(), c()) 

listMeanCDSThird <- list(c(), c(), c(), c(), c(), c()) 
listMeanBondThird <- list(c(), c(), c(), c(), c(), c()) 
listMeanSwapZRThird <- list(c(), c(), c(), c(), c(), c()) 
listMeanCDSbbThird <- list(c(), c(), c(), c(), c(), c()) 

for (i in seq(nrow(RatingDowngradesFinal_))) { 

    # Check whether a downgrade occured 

    if (isTRUE(match(RatingDowngradesFinal_$`New rating`[i], fitch) > 
      match(RatingDowngradesFinal_$`Previous rating`[i], fitch)) | 
     isTRUE(match(RatingDowngradesFinal_$`New rating`[i], moodys) > 
      match(RatingDowngradesFinal_$`Previous rating`[i], moodys)) | 
     isTRUE(match(RatingDowngradesFinal_$`New rating`[i], standardandpoors) > 
      match(RatingDowngradesFinal_$`Previous rating`[i], standardandpoors)) | 
     isTRUE(match(RatingDowngradesFinal_$`New rating`[i], dbrs) > 
      match(RatingDowngradesFinal_$`Previous rating`[i], dbrs))) { 

    # Set the interval 

    for (j in seq(length(listIntervals))) { 

     interval <- c(RatingDowngradesFinal_$Dates[i] + listIntervals[[j]][1], RatingDowngradesFinal_$Dates[i] + listIntervals[[j]][2]) 

     # Filter the dataframe by "interval" 
     beforeDownGrade <- split(CDS_bond_basis, interval[1] <= CDS_bond_basis$Dates & 
           CDS_bond_basis$Dates <= interval[2] & 
           CDS_bond_basis$Bank == as.character(RatingDowngradesFinal_$Bank[i])) 

     if (is.null(beforeDownGrade$'TRUE') == FALSE) { 

     if (nrow(beforeDownGrade$'TRUE') > 1) { 

      if (as.Date("2007-01-02") <= RatingDowngradesFinal_$Dates[i] & RatingDowngradesFinal_$Dates[i] <= as.Date("2009-12-31")) { 

      listMeanCDSFirst[[j]] <- c(listMeanCDSFirst[[j]], mean(diff(beforeDownGrade$'TRUE'$CDS))) 
      listMeanBondFirst[[j]] <- c(listMeanBondFirst[[j]], mean(diff(beforeDownGrade$'TRUE'$Bond))) 
      listMeanSwapZRFirst[[j]] <- c(listMeanSwapZRFirst[[j]], mean(diff(beforeDownGrade$'TRUE'$`Swap zero rate`))) 
      listMeanCDSbbFirst[[j]] <- c(listMeanCDSbbFirst[[j]], mean(diff(beforeDownGrade$'TRUE'$`CDS-bond basis`))) 

      } else if (as.Date("2010-01-01") <= RatingDowngradesFinal_$Dates[i] & RatingDowngradesFinal_$Dates[i] <= as.Date("2012-12-31")) { 
      listMeanCDSSecond[[j]] <- c(listMeanCDSSecond[[j]], mean(diff(beforeDownGrade$'TRUE'$CDS))) 
      listMeanBondSecond[[j]] <- c(listMeanBondSecond[[j]], mean(diff(beforeDownGrade$'TRUE'$Bond))) 
      listMeanSwapZRSecond[[j]] <- c(listMeanSwapZRSecond[[j]], mean(diff(beforeDownGrade$'TRUE'$`Swap zero rate`))) 
      listMeanCDSbbSecond[[j]] <- c(listMeanCDSbbSecond[[j]], mean(diff(beforeDownGrade$'TRUE'$`CDS-bond basis`))) 

      } else if (as.Date("2013-01-01") <= RatingDowngradesFinal_$Dates[i] & RatingDowngradesFinal_$Dates[i] <= as.Date("2016-12-30")) { 
      listMeanCDSThird[[j]] <- c(listMeanCDSThird[[j]], mean(diff(beforeDownGrade$'TRUE'$CDS))) 
      listMeanBondThird[[j]] <- c(listMeanBondThird[[j]], mean(diff(beforeDownGrade$'TRUE'$Bond))) 
      listMeanSwapZRThird[[j]] <- c(listMeanSwapZRThird[[j]], mean(diff(beforeDownGrade$'TRUE'$`Swap zero rate`))) 
      listMeanCDSbbThird[[j]] <- c(listMeanCDSbbThird[[j]], mean(diff(beforeDownGrade$'TRUE'$`CDS-bond basis`))) 

      } 

     } 

     } 

    } 

    } 

} 

PreviousMonth1 <- c(mean(listMeanCDSFirst[[1]]), mean(listMeanBondFirst[[1]]), mean(listMeanSwapZRFirst[[1]]), mean(listMeanCDSbbFirst[[1]])) 
NextMonth1 <- c(mean(listMeanCDSFirst[[2]]), mean(listMeanBondFirst[[2]]), mean(listMeanSwapZRFirst[[2]]), mean(listMeanCDSbbFirst[[2]])) 
NextSecondMonth1 <- c(mean(listMeanCDSFirst[[3]]), mean(listMeanBondFirst[[3]]), mean(listMeanSwapZRFirst[[3]]), mean(listMeanCDSbbFirst[[3]])) 
NextThirdMonth1 <- c(mean(listMeanCDSFirst[[4]]), mean(listMeanBondFirst[[4]]), mean(listMeanSwapZRFirst[[4]]), mean(listMeanCDSbbFirst[[4]])) 
PreviousAndNextDay1 <- c(mean(listMeanCDSFirst[[5]]), mean(listMeanBondFirst[[5]]), mean(listMeanSwapZRFirst[[5]]), mean(listMeanCDSbbFirst[[5]])) 
NextTenDays1 <- c(mean(listMeanCDSFirst[[6]]), mean(listMeanBondFirst[[6]]), mean(listMeanSwapZRFirst[[6]]), mean(listMeanCDSbbFirst[[6]])) 

PreviousMonth2 <- c(mean(listMeanCDSSecond[[1]]), mean(listMeanBondSecond[[1]]), mean(listMeanSwapZRSecond[[1]]), mean(listMeanCDSbbSecond[[1]])) 
NextMonth2 <- c(mean(listMeanCDSSecond[[2]]), mean(listMeanBondSecond[[2]]), mean(listMeanSwapZRSecond[[2]]), mean(listMeanCDSbbSecond[[2]])) 
NextSecondMonth2 <- c(mean(listMeanCDSSecond[[3]]), mean(listMeanBondSecond[[3]]), mean(listMeanSwapZRSecond[[3]]), mean(listMeanCDSbbSecond[[3]])) 
NextThirdMonth2 <- c(mean(listMeanCDSSecond[[4]]), mean(listMeanBondSecond[[4]]), mean(listMeanSwapZRSecond[[4]]), mean(listMeanCDSbbSecond[[4]])) 
PreviousAndNextDay2 <- c(mean(listMeanCDSSecond[[5]]), mean(listMeanBondSecond[[5]]), mean(listMeanSwapZRSecond[[5]]), mean(listMeanCDSbbSecond[[5]])) 
NextTenDays2 <- c(mean(listMeanCDSSecond[[6]]), mean(listMeanBondSecond[[6]]), mean(listMeanSwapZRSecond[[6]]), mean(listMeanCDSbbSecond[[6]])) 

PreviousMonth3 <- c(mean(listMeanCDSThird[[1]]), mean(listMeanBondThird[[1]]), mean(listMeanSwapZRThird[[1]]), mean(listMeanCDSbbThird[[1]])) 
NextMonth3 <- c(mean(listMeanCDSThird[[2]]), mean(listMeanBondThird[[2]]), mean(listMeanSwapZRThird[[2]]), mean(listMeanCDSbbThird[[2]])) 
NextSecondMonth3 <- c(mean(listMeanCDSThird[[3]]), mean(listMeanBondThird[[3]]), mean(listMeanSwapZRThird[[3]]), mean(listMeanCDSbbThird[[3]])) 
NextThirdMonth3 <- c(mean(listMeanCDSThird[[4]]), mean(listMeanBondThird[[4]]), mean(listMeanSwapZRThird[[4]]), mean(listMeanCDSbbThird[[4]])) 
PreviousAndNextDay3 <- c(mean(listMeanCDSThird[[5]]), mean(listMeanBondThird[[5]]), mean(listMeanSwapZRThird[[5]]), mean(listMeanCDSbbThird[[5]])) 
NextTenDays3 <- c(mean(listMeanCDSThird[[6]]), mean(listMeanBondThird[[6]]), mean(listMeanSwapZRThird[[6]]), mean(listMeanCDSbbThird[[6]])) 

period1 <- data.frame(PreviousMonth1, NextMonth1, NextSecondMonth1, NextThirdMonth1, PreviousAndNextDay1, NextTenDays1) 
rownames(period1) <- c("CDS", "Bond", "Swap zero Rate", "CDS-bond-basis") 
colnames(period1) <- c("[-30,-1]", "[1,30]", "[31,60]", "[61,90]", "[-1,1]", "[1,10]") 

period2 <- data.frame(PreviousMonth2, NextMonth2, NextSecondMonth2, NextThirdMonth2, PreviousAndNextDay2, NextTenDays2) 
rownames(period2) <- c("CDS", "Bond", "Swap zero Rate", "CDS-bond-basis") 
colnames(period2) <- c("[-30,-1]", "[1,30]", "[31,60]", "[61,90]", "[-1,1]", "[1,10]") 

period3 <- data.frame(PreviousMonth3, NextMonth3, NextSecondMonth3, NextThirdMonth3, PreviousAndNextDay3, NextTenDays3) 
rownames(period3) <- c("CDS", "Bond", "Swap zero Rate", "CDS-bond-basis") 
colnames(period3) <- c("[-30,-1]", "[1,30]", "[31,60]", "[61,90]", "[-1,1]", "[1,10]") 

print(period1) 
print(period2) 
print(period3) 

Ce qui donne, pour period1:

> print(period1) 
       [-30,-1]  [1,30]  [31,60]  [61,90] [-1,1]  [1,10] 
CDS   -0.1934029 0.5002909 0.09593413 -0.38126535 1.4342439 0.50836275 
Bond   0.1001838 0.5286359 0.78631190 -0.88260529 1.3531346 -0.06724158 
Swap zero Rate -0.5743715 -0.4472814 -0.13148844 -0.09563088 0.7412500 -0.30337037 
CDS-bond-basis -0.8679582 -0.4756264 -0.82186622 0.40570906 0.8223592 0.27223396 
+0

Merci beaucoup! Je vais l'essayer dès que je rentre à la maison. – rbonac

+0

J'ai eu quelques messages d'erreur, afin que vous puissiez le voir, j'ai mis mes changements dans une réponse à ma question. Merci encore pour votre aide à l'avance! – rbonac

+0

Avez-vous utilisé toutes vos données pour effectuer le test? Il semble qu'il n'ait pas trouvé de downgrade correspondant dans CDS_bond_basis. Ou si vous pouvez fournir un plus grand échantillon de données ... –

0

J'ai ajouté les échelles d'évaluation de DRB et Standard and Poors comme vous le dites.De plus j'ai changé quelques notes sur la liste afin de l'adapter à mes données comme suit:

fitch <- c("AA+ *-","AA *-", "AA- *-", "AA", "AA-", "A+", "A+ *-", "A", "A 
*-", "A-", "A- *-", "BBB+", "BBB+ *-", "BBB", "BBB *-", "BBB-", "BBB- *-", 
"BB+", "BB+ *-", "BB", "BB *-", "BB-", "B+ *-", "B-", "B- *-", "CCC", "CC", 
"C") 
moodys <- c("Aaa*-", "Aa1 *-", "Aa2", "Aa2 *-", "Aa3", "Aa3 *-", "A1", "A1 
*-", "A2", "A2 *-", "A3", "A3 *-", "Baa1", "Baa1 *-", "Baa2", "Baa2 *-", 
"Baa3", "Baa3 *-", "Ba1", "Ba1 *-", "Ba2", "Ba2 *-", "Ba3", "Ba3 *-", "B1", 
"B2", "Caa2", "Caa2 *-", "Caa3", "Caa3 *-", "Ca", "C", "C-","C *-","C- *- 
","C+","C+ *-") 
standardandpoors <- c("AA *-", "AA- *-", "AA", "AA-", "A+", "A+ *-", "A", 
"A *-", "A-", "A- *-", "BBB+", "BBB+ *-", "BBB", "BBB *-", "BBB-", "BB+ *-", 
"BB *-", "B") 
dbrs <- c("AAA *-", "AAH *-", "AAH", "AAL *-", "AAL", "AA", "AA *-", "AH *- 
", "AH", "A", "A *-", "AL", "AL *-", "BBBH", "BBBH *-", "BBB", "BBB *-", 
"BBBL *-") 

Ensuite je l'ai également ajouté dans la section que pour vérifier si downgrade est survenue:

if (isTRUE(match(RatingDowngradesFinal_$New.rating[i], fitch) > 
    match(RatingDowngradesFinal_$Previous.rating[i], fitch)) | 
    isTRUE(match(RatingDowngradesFinal_$New.rating[i], standardandpoors) > 
    match(RatingDowngradesFinal_$Previous.rating[i], standardandpoors)) | 
    isTRUE(match(RatingDowngradesFinal_$New.rating[i], dbrs) > 
    match(RatingDowngradesFinal_$Previous.rating[i], dbrs)) | 
    isTRUE(match(RatingDowngradesFinal_$New.rating[i], moodys) > 
    match(RatingDowngradesFinal_$Previous.rating[i], moodys))) {` 

Je a couru tout le code dans R et a obtenu les messages d'erreur suivants dans chaque intervalle de temps:

PreviousMonth1 <- c(mean(listMeanCDSbbFirst[[1]]), 
mean(listMeanBondFirst[[1]]), mean(listMeanSwapZRFirst[[1]]), 
mean(listMeanCDSbbFirst[[1]])) 
Warning messages: 
1: In mean.default(listMeanCDSbbFirst[[1]]) : 
Argument ist weder numerisch noch boolesch: gebe NA zurück 
2: In mean.default(listMeanBondFirst[[1]]) : 
Argument ist weder numerisch noch boolesch: gebe NA zurück 
3: In mean.default(listMeanSwapZRFirst[[1]]) : 
Argument ist weder numerisch noch boolesch: gebe NA zurück 
4: In mean.default(listMeanCDSbbFirst[[1]]) : 
Argument ist weder numerisch noch boolesch: gebe NA zurück` 

cela a abouti à ce résultat:

print(period1) [-30,-1] [1,30] [31,60] [61,90] [-1,1] [1,10] CDS NA NA NA NA NA NA Bond NA NA NA NA NA NA Swap zero Rate NA NA NA NA NA NA CDS-bond-basis NA NA NA NA NA NA print(period2) [-30,-1] [1,30] [31,60] [61,90] [-1,1] [1,10] CDS NA NA NA NA NA NA Bond NA NA NA NA NA NA Swap zero Rate NA NA NA NA NA NA CDS-bond-basis NA NA NA NA NA NA print(period3)
[-30,-1] [1,30] [31,60] [61,90] [-1,1] [1,10] CDS NA NA NA NA NA NA Bond NA NA NA NA NA NA Swap zero Rate NA NA NA NA NA NA CDS-bond-basis NA NA NA NA NA NA
` Qu'est-ce qui semble être le problème?