On Tue, Feb 08, 2011 at 07:50:26PM -0800, Frenando
wrote:>
> Hello World!
> I'm working on my thesis right now (something about financial
immunization)
> I'm currently working in the basics, doing a matrix that lists the
present
> value (or weight) of every combination of coupon rate and term to maturity,
> this is the code I have right now which is giving me a "longer object
length
> is not a multiple of shorter object length"
>
> #start
> rm(list=ls())
> #test
> yrt <- c(0.065,0.069,0.075,0.082)
> coupon <- c(0.06, 0.07, 0.08, 0.09, 0.1, 0.11, 0.12)
> term <- c(2:7)
> #test#
>
> PV <- function(coupon, term)
> {
> PV <- sum(coupon/(1+yrt)^(1:term)) + 1/((1+yrt[term])^term)
> }
>
>
> matrizPV <- matrix(, nrow=6, ncol=6)
> for (j in 1:6)
> {
> for (i in 1:6)
> {
> matrizPV[i, j] = PV(coupon[i], term[j])
> }
> }
The warning, which you get, is generated in
(1+yrt)^(1:term)
if term is not equal to the length of yrt, which is 4. See
section Value of ?"^".
Let me point out that the assignment "PV <- ..." inside the
function PV() is not needed. The returned value is determined
either by the command return() or as the last expression
evaluated. See section Details in ?return. So, one of the
following may be used
PV <- function(coupon, term)
{
sum(coupon/(1+yrt)^(1:term)) + 1/((1+yrt[term])^term)
}
PV <- function(coupon, term)
{
return(sum(coupon/(1+yrt)^(1:term)) + 1/((1+yrt[term])^term))
}
Hope this helps.
Petr Savicky.