Creating Coupon Bond Objects for Nelson–Siegel–Svensson Estimation in R
Summary
The document explains how to prepare an object accepted by the `estim_nss` function in R’s `termstrc` package, which estimates a spot curve from bond data. The key requirement is to build the package’s expected nested bond-data structure and assign it the `couponbonds` class; passing a plain list without that class leads to a method-dispatch error.
The responses describe two approaches: construct country-level lists containing bond details and cash-flow data, or reclassify an existing list after ensuring its fields follow the expected names. One response notes that `ISSUEDATE` should be used in place of `STARTDATE` in that approach, while the manual-based example uses `STARTDATE`. This inconsistency means the precise field requirements may depend on the package’s expected data format or version. The discussion gives no curve-fitting results or validation, and offers only a basic object-construction solution rather than guidance on estimating or assessing the curve.
Key ideas
- The `estim_nss` function expects input with the `couponbonds` class, not an ordinary list.
- The example structure nests country-level bond records and includes cash-flow fields.
- One answer says to replace `STARTDATE` with `ISSUEDATE` before assigning the class.
- The source examples disagree about the date-field name, so package documentation should be checked for the relevant format.
Tags
Full text
# Using the termstrc package in R
# Using the termstrc package in R
I am attempting to use the function estim_nss from the termstrc package in R to find the spot curve from constant maturity rates published by the Fed. I am using this package because I will need to deal with variable rate coupon bonds in the future. The function requires an object of class "couponbonds" but I cannot figure out how to create this class. I tried creating a list object with similar data but the function rejects this object with the following message:
Error in UseMethod("estim_nss") : no applicable method for 'estim_nss' applied to an object of class "list"
Any assistance would be appreciated.
## Answer by ProbablePattern (score 1, accepted)
https://quant.stackexchange.com/a/4817
It turned out to be more simple than I thought. First, be sure to replace "STARTDATE" with "ISSUEDATE" when building the list. Once the list is build simply reclassify it using the following command:
> class(mybonds)="couponbonds"
That's it!
## Answer by Bob Jansen (score 3)
https://quant.stackexchange.com/a/4809
You can create the data using the procedure described in the reference manual on pages 31 and 32. The necessary code is copied below:
```
# The following code may be used to generate an empty data set,
# which can then be filled with bond data:
ISIN <- vector()
MATURITYDATE <- vector()
STARTDATE <- vector()
COUPONRATE <- vector()
PRICE <- vector()
ACCRUED <- vector()
CFISIN <- vector()
CF <- vector()
DATE <- vector()
CASHFLOWS <- list(CFISIN,CF,DATE)
names(CASHFLOWS) <- c("ISIN","CF","DATE")
TODAY <- vector()
mycountry1 <- list(ISIN,MATURITYDATE,STARTDATE,
COUPONRATE,PRICE,ACCRUED,CASHFLOWS,TODAY)
mycountry2 <- list(ISIN,MATURITYDATE,STARTDATE,
COUPONRATE,PRICE,ACCRUED,CASHFLOWS,TODAY)
names(mycountry1) <- c("ISIN","MATURITYDATE","STARTDATE","COUPONRATE",
"PRICE","ACCRUED","CASHFLOWS","TODAY")
names(mycountry2) <- c("ISIN","MATURITYDATE","STARTDATE","COUPONRATE",
"PRICE","ACCRUED","CASHFLOWS","TODAY")
mybonds <- list(mycountry1,mycountry2)
names(mybonds) <- c("mycountry1","mycountry2")
```
Finally set the class of the object just created:
```
class(mybonds)="couponbonds"
```Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.