knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
In order to make it easy and fun the use of fixedincome spot rate curve, a few operators have been implemented.
The fixedincome package is loaded and one curve crv
is created.
library(fixedincome) crv <- spotratecurve( c(0.1315, 0.1319, 0.1338, 0.1348, 0.1372, 0.1381), c(1, 21, 42, 63, 126, 252), "discrete", "business/252", "Brazil/ANBIMA", refdate = Sys.Date() )
The spot rate curve crv
is used in the examples.
The operator [
indexes the elements by their positions and returns one SpotRateCurve object.
The code below returns one SpotRateCurve object with the first, third and fifth elements.
crv[c(1, 3, 5)]
This curve has the terms 1, 42, and 126 days.
The positional indexing also works with logical
vectors.
For example, let's get the part of the curve that is greater than 13.5%
crv[crv > 0.135]
or the elements which terms are even.
crv[(crv@terms %% 2) == 0]
The operator [[
indexes the elements according to the terms of the term structured and returns
one SpotRateCurve object.
The code below returns one SpotRateCurve object with the terms 1, 21, and 42.
crv[[c(1, 21, 42)]]
All these terms are present in the SpotRateCurve.
If any given term doesn't correspond to the SpotRateCurve terms, NA
is returned for the unmatched
elements.
crv[[c(1, 13, 42)]]
To solve that an interpolation has to be set and in these cases the interpolation fulfills these gaps.
interpolation(crv) <- interp_linear() crv[[c(1, 13, 42)]]
Let's get back to the original curve removing the interpolation.
interpolation(crv) <- NULL
The spot rate elements in an SpotRateCurve object can be replaced positionally with the operator [
.
For example, the second element can be set accordingly.
crv[2] <- 0.14 crv[c(1, 2, 3)]
Negative indexes remove elements
crv[-2]
But it doesn't change the object in place, instead returns a new object without the removed elements.
crv
The second element (term 21) hasn't been removed.
The spot rate elements in an SpotRateCurve object can be replaced by term with the operator [[
.
For example, let's replace the element related to the term 21.
crv[[21]] <- 0.1320 crv[[c(1, 21, 42)]]
Negative terms also remove elements.
crv[[-21]]
Nonetheless, if the interpolation is defined, negative terms raises an error.
interpolation(crv) <- interp_linear() crv[[-21]]
The methods first
and last
are handy to slice parts of the SpotRateCurve.
first
and last
return spot rate curve according to the term given in the second argument.
For example, the following call returns the elements that lies inside the first 50 days.
fixedincome::first(crv, "50 days")
The fixedincome::
prefix is necessary to avoid conflicts with dplyr's methods.
We can do similar with the last
method, for example, let's get the elements in the last 6 months
of the spot rate curve.
fixedincome::last(crv, "6 months")
There is one last spotratecurve
method that isn't properly an indexing method, in the usual way,
but certainly it is useful.
This is the fixedincome::closest
method, for the spotratecurve
, it returns the element of
the curve that is close to the given term.
For example, to get the curve element that is the closest to 1 year, we do
fixedincome::closest(crv, "1 year")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.