data.tablefinancediscount

Rstudio monthly discount factor interpolation for data table


How can I have a linear interpolation on the column DF in order to replace the 0 with a "weighted amount" obtained combining 0 and 0.98 for the first 11 records and 0.98 and 0.95 for the records from 13 to 23 and use 0.95 for the last two records. The idea is to have a kind of interpolation to get the monthly discount factors.

DT <- data.table(MM=c(1,2,3,4,5,6,7,8,9,10,11,12,12,14,15,16,17,18,19,20,21,22,23,24,25,26),
                    DF=c(1,0,0,0,0,0,0,0,0,0,0,0.98,0,0,0,0,0,0,0,0,0,0,0.95,0,0,0))
DT

Solution

  • Something like this, perhaps:

    DT[, DF2 := approx(MM[DF > 0], DF[DF > 0], xout = MM)$y
      ][, DF2 := nafill(DF2, type = "locf")]
    #        MM    DF       DF2
    #     <num> <num>     <num>
    #  1:     1  1.00 1.0000000
    #  2:     2  0.00 0.9981818
    #  3:     3  0.00 0.9963636
    #  4:     4  0.00 0.9945455
    #  5:     5  0.00 0.9927273
    #  6:     6  0.00 0.9909091
    #  7:     7  0.00 0.9890909
    #  8:     8  0.00 0.9872727
    #  9:     9  0.00 0.9854545
    # 10:    10  0.00 0.9836364
    # ---                      
    # 17:    17  0.00 0.9663636
    # 18:    18  0.00 0.9636364
    # 19:    19  0.00 0.9609091
    # 20:    20  0.00 0.9581818
    # 21:    21  0.00 0.9554545
    # 22:    22  0.00 0.9527273
    # 23:    23  0.95 0.9500000
    # 24:    24  0.00 0.9500000
    # 25:    25  0.00 0.9500000
    # 26:    26  0.00 0.9500000
    

    Steps: