Problem with xts Object

Asked

Viewed 131 times

3

Package ːYieldcurve'

Guys, I’m using this package and I have a little problem.

MatrizCurva<-MatrizUmaLinha20colunas[1,1:20]
[1] 0.1164000 0.1264816 0.1265631 0.1366446 0.1467260 0.1568073 0.1668885 0.179694  
[9] 0.1770501 0.1771306 0.1772109 0.1772908 0.1773703 0.1774496 0.1775284 0.1076068
[17] 0.1776847 0.1777621 0.1778391 0.17079155

> class(MatrizEttjDiaria[1,1:20])
[1] "numeric"

maturityBOND <- seq(1,20,1)
maturityBOND

[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20

The first function which is to return the Betas and the lambda is ok:

NSParameters <- Nelson.Siegel( MatrizCurva, maturityBOND)

NSParameters
   beta_0       beta_1       beta_2     lambda
[1,] 0.111046 -0.004705511 -0.003196441 0.08965621

It returns right. In the pdf it says that Matrizcurva needs to be an object of type xts. In my case Matrizcurva is an object "Numeric":

class(MatrizCurva )
[1] "numeric"

But it still spins.

Now, when I call the Nsrates function presents the following problem:

> NSParameters <- NSrates(NSParameters[1,],maturityBOND)
Error in matrix(0, nrow(Coeff), length(maturity)) : 
  non-numeric matrix extent

I must turn to xts?

In fact, I’ve been trying to turn to xts and the thing doesn’t come out.

1 answer

1


Just convert your data to an object xts. For this, you need to select an argument order.by which should be a date. I put the current date, but you should use something that makes sense in your analysis.

library("YieldCurve")

MatrizCurva <- c(0.1164000, 0.1264816, 0.1265631, 0.1366446, 0.1467260, 0.1568073, 0.1668885, 0.179694,  
0.1770501, 0.1771306, 0.1772109, 0.1772908, 0.1773703, 0.1774496, 0.1775284, 0.1076068,
0.1776847, 0.1777621, 0.1778391, 0.17079155)

maturityBOND <- 1:20    

NSParameters <- Nelson.Siegel(MatrizCurva, maturityBOND)

nspar <- xts(NSParameters, Sys.Date())

nsrate <- NSrates(nspar, maturityBOND)

Final result:

> nsrate
                  X1        X2        X3        X4        X5        X6        X7
2015-07-26 0.1095283 0.1238727 0.1356903 0.1453332 0.1531076 0.1592796 0.1640806
                  X8        X9       X10       X11       X12       X13       X14
2015-07-26 0.1677111 0.1703454 0.1721342 0.1732082 0.1736803 0.1736481 0.1731957
                 X15       X16       X17       X18       X19       X20
2015-07-26 0.1723957 0.1713102 0.1699929 0.1684897 0.1668397 0.1650763

Browser other questions tagged

You are not signed in. Login or sign up in order to post.