I'm new using R. I have the following sample dataset:
> head(abn)
Dates DTM YTM
1 2010-09-28 1133 2.965
2 2010-09-28 1834 3.613
3 2010-09-29 1132 2.994
4 2010-09-29 1833 3.595
5 2010-09-30 1131 3.026
6 2010-09-30 1832 3.590
The observations are several bond values on an observation period from 2010-2016. My data set is composed of multiple bonds with maturities between 1-15 years (260-3900 business days as depicted in the dataset). DTM stands for days to maturity and YTM for yield to maturity.
My goal is to construct a synthetic bond with a maturity of 5 years for each day. Therefore I need to make a regression and find the YTM value for the DTM value of 1300, which is exactly 5 years.
Plot of all bonds on all dates
> plot(dtm, ytm, xlab = "Days to maturity", ylab = "Yield to maturity")
> abline(lm (ytm~dtm), col="red")
I plotted all the bonds on all dates in one regression. I need to get the value of the y-axis at x=1300. However I need to have this information for every date separately.
You can do this with dplyr
...
library(dplyr)
newval <- data.frame(DTM=1300) #predict.lm likes new values in a dataframe
abn5y <- abn %>% group_by(Dates) %>%
summarise(Y5=predict(lm(YTM ~ DTM), newval))
abn5y
Dates Y5
<chr> <dbl>
1 2010-09-28 3.119374
2 2010-09-29 3.138034
3 2010-09-30 3.161971