Dear R helpers
(I have already written the required R code which is giving me correct results
for a given single set of data. I just wish to wish to use it for multiple
data.)
I have defined a function (as given below) which helps me calculate Macaulay
Duration and Modified Duration and its working fine with given set of data.
My Code -
## ONS - PPA
duration = function(par_value, coupon_rate, frequency_copoun, tenure, ytm)
{
macaulay_duration = NULL
modified_duration = NULL
freq_coupon_new = NULL
if(frequency_copoun <= 0)
{
freq_coupon_new = 365
}
if(frequency_copoun > 0 & frequency_copoun <= 1)
{
freq_coupon_new = 12
}
if(frequency_copoun > 1 & frequency_copoun <= 2)
{
freq_coupon_new = 4
}
if(frequency_copoun > 2 & frequency_copoun <= 3)
{
freq_coupon_new = 2
}
if(frequency_copoun > 3 & frequency_copoun <= 4)
{
freq_coupon_new = 1
}
## COMPUTATIONS
terms_coupon_payment = (seq(1/freq_coupon_new, tenure, by =
1/freq_coupon_new))*freq_coupon_new
coupon = coupon_rate*par_value/100
coupon_amount = coupon/(freq_coupon_new)
cash_flow1 = rep(c(coupon_amount), (tenure*freq_coupon_new -
1))
cash_flow2 = par_value + coupon_amount
cash_flow = c(cash_flow1, cash_flow2)
ytm_effective = ((1+ytm/100)^(1/freq_coupon_new))-1
pv = NULL
for (i in 1:(tenure*freq_coupon_new))
{
pv[i] = cash_flow[i] / ((1+ytm_effective)^terms_coupon_payment[i])
}
macaulay_duration = sum(pv*terms_coupon_payment)/sum(pv)
modified_duration = macaulay_duration / (1+(ytm_effective)/freq_coupon_new)
return(data.frame(macaulay_duration, modified_duration))
}
### For a given data say
result = duration(par_value = 1000, coupon_rate = 10, frequency_copoun = 0,
tenure = 5, ytm = 12)
I get the output as
macaulay_duration modified_duration
1 1423.797 1423.795
## __________________________________________________________________
## MY PROBLEM
If instead of having only one set of data, suppose I have multiple data (say as
given below in a csv file), I am not able to get these results.
Suppose my 'input.csv' file is as given below.
par_value coupon_rate frequency_copoun tenure ytm
1000 10 0 5
12
100
7 1 8 11
Sir, I am not asking for the modification of existing code as it is running fine
with a single set of data (and I have checked that the output tallies with other
methods). I just want to use this code for multiple data so that I get an output
something like
maculay_duration modified_duration
1423.797 1423.795
44.339 44.307
I request you to please guide me. I undesratnd its not my right to seek help,
but this is for the third time I am requesting to guide me.
Thanking you all
Regards
Madhavi
The INTERNET now has a personality. YOURS! See your Yahoo! Homepage.
[[alternative HTML version deleted]]
Dieter Menne
2010-Jan-20 08:41 UTC
[R] Please Please Please Help me (Changed to: calling a function with a list of parameters)
Please, do not write "Help, Help" but give a meaningful header. Most people here a specialized and check the posts they understand best. Madhavi Bhave wrote:> > ? > (I have already written the required R code which is giving me correct > results for a given single set of data. I just wish to wish to use it for > multiple data.) >It's fine that you posted the working part; in general, for posting you should simplify the thing that do work to the bare bones (see example below) Madhavi Bhave wrote:> > I have defined a function (as given below) which helps me calculate > Macaulay Duration and Modified Duration and its working fine with given > set of data. > > I just want to use this code for multiple data so that I get an output > something like > > ???????? maculay_duration??????????????????? modified_duration > ? > ????????? 1423.797?????????????????????????????????? 1423.795 > ? > ???????????? 44.339?????????????????????????????????????? 44.307 > ? >The most elegant approach uses package plyr; you even get the function parameters in your output. library(plyr) # simplified version of your original function duration = function(par_value, coupon_rate) { data.frame(macaulay_duration=2*par_value,modified_duration=3*coupon_rate) } # use read.table in your application instead of the line below pars = data.frame(par_value=c(1000,100), coupon_rate=c(10,7)) mdply(pars,.fun=duration) # par_value coupon_rate macaulay_duration modified_duration #1 1000 10 2000 30 #2 -- View this message in context: http://n4.nabble.com/Please-Please-Please-Help-me-tp1018209p1018251.html Sent from the R help mailing list archive at Nabble.com.
Hello On Wed, Jan 20, 2010 at 7:00 AM, Madhavi Bhave <madhavi_bhave at yahoo.com> wrote:> Sir, I am not asking for the modification of existing code as it is running fine with a single set of data (and I have checked that the output tallies with other methods). I just want to use this code for multiple data so that I get an output something like > > ???????? maculay_duration??????????????????? modified_duration > > ????????? 1423.797?????????????????????????????????? 1423.795 > > ???????????? 44.339?????????????????????????????????????? 44.307 >I am not sure that this would be the most efficient, but see if something similar does the job for you: x <- matrix(NA, 5,2); x for (i in 1:5){ x1 <- cbind(mean(rnorm(100)), mean(runif(100))) ## use duration() instead of cbind() and r*() funs x[i, ] <- x1 } x Liviu