Given the following table:
IBNR <- data.table(IncurredYear=c(2020,2020,2020,2020,2021,2021,2021,2022,2022,2023),
DevYear=c(0,1,2,3,0,1,2,0,1,0),
Amount=c(100,80,70,50,70,40,35,50,45,80))
I would like to calculate to get the “Amount cumulative”:
IBNRCumulative <- data.table(IncurredYear=c(2020,2020,2020,2020,2021,2021,2021,2022,2022,2023),
DevYear=c(0,1,2,3,0,1,2,0,1,0),
Amount=c(100,80,70,50,70,40,35,50,45,80),
AmountCumulative=c(100,180,250,300,70,110,145,50,95,80))
and after reshaping from long to wide:
IBNRtriang <- dcast(IBNRCumulative, IncurredYear ~ DevYear, fun.aggregate = list(sum), value.var = c("AmountCumulative"))
I would like to derive the DevFactors:
DevelopmentFactor <- data.table(DevYear=c(0,1,2,3),
DevFactor = c(1.75, 1.362, 1.2, 1))
where: 1.75 = (180+110+95)/(100+70+50), 1.362 = (250+145)/(180+110) and so on, the last one should be 1.
Can you help me to find a compact and elegant way to get it done? Thanks.