Dear Sir,   Thank you for valuable guidance. Though I have been using R occassionally, it was limited to some basics and that way I am new to R. As suggested by you, I have gone through the said chapter of Introduction to R manual, though I have some urgent comittments to meet.   I have tried writing function as given below.     f = function(price, tenure, no_comp, coupon_rate, face_value)   { coupon_payment = face_value * coupon_rate / no_comp cash_flow = c(rep(c(coupon_payment), (no_comp * tenure - 1)), face_value + coupon_payment)   E = NULL  for (i in 1 : (tenure * no_comp - 1))   {   E[i] = cash_flow[i]/(1+ytm)^i   }    F = NULL   {   F = sum(E) + ((face_value + coupon_payment)/(1+ytm)^(no_comp * tenure)) - price   }   return(data.frame(S = uniroot.all(F, interval=c(0,25))))     }   output = f(1010, 3, 1, 0.10, 1000)   ##  End of code   However, when I try to execute the same, I get following error.   Error: object 'ytm' not found My objective is to find ytm itself and I am not able to figure out where I am going wrong and how to overcome the same.   Regards   Madhavi --- On Tue, 2/2/10, Dennis Murphy wrote: From: Dennis Murphy Subject: Re: [R] Yield to Maturity using R To: "Madhavi Bhave" Date: Tuesday, 2 February, 2010, 3:49 AM Hi: On Tue, Feb 2, 2010 at 3:01 AM, Madhavi Bhave wrote: Dear R helpers,     Yesterday I had raised following query which was addressed by Mr Ellison. The query and the wonderful solution as provided by Mr. Ellison are as given below.    ## PROBLEM   I am calculating the 'Yield to Maturity' for the Bond with following characteristics.    Its a $1000 face value, 3 year bond with 10% annual coupon and is priced at 101. The yield to maturity can be calculated after solving the equation -     1010 = [100 / (1+ytm)]  + [100 / (1+ytm)^2] + [ 1100 / (1 + ytm)^3]    This can be solved by trial and error method s.t. ytm = 9.601%. I wanted to find out how to solve this equation in R.     ## SOLUTION   Mr. Elisson had given me following wonderful solution   f.ytm<-function(ytm) 100 / (1+ytm)  +100 / ((1+ytm)^2) + 1100 / ((1 + ytm)^3) -1010 uniroot(f.ytm, interval=c(0,25))  #$root has the answer   And I got the answer as 9.601.   ## _____________________________________________________________   I was just trying to generalize this solution to any equation and accordingly written a code as given below.   The following input I will be reading using csv file and thus my equation will change if tenure or no_comp etc. changes. So taking into account the variable nature of the input, I am trying to write a generalized code.   ## Input   price = 101       # Price of bond tenure = 3          no_comp = 1      # no of times coupon paid in a year. coupon_rate = 0.10  # i.e. 10% face_value  = 100   # Computations   coupon_payment = face_value * coupon_rate cash_flow = c(rep(c(coupon_payemnt), (no_comp * tenure - 1)), face_value + coupon_payment) cash_flow   ## I am trying to customize the code as given by Mr Ellison.   f.ytm = function(ytm)   {    for (i in 1 : (tenure * no_comp - 1))  E = NULL  F = NULL  {  E[i] = cash_flow[i]/(1+ytm)^i  F = (sum(E) + (face_value + coupon_payment)/((1+ytm)^(tenure * no_comp))) - price     } }   For this to work, tenure, no_comp, cash_flow, face_value and coupon_payment have to be visible to the function - i.e., they either have to be in the function's calling environment or in the global environment. These are called 'free variables' under the lexical scoping rules of R. (Welcome to function writing :)  You might want to look a little more closely at uniroot(), especially the conventions it requires for the *functions* it can evaluate. You want the body of what you send to uniroot() for evaluation to be a function of a single variable. Your previously supplied solution meets that requirement. This one doesn't (yet). Moreover, it appears that E[i] is never used and nothing is returned from f.ytm. I'd suggest an excursion into R function writing fundamentals. Start with Ch. 10 of the Introduction to R manual. HTH, Dennis solution = uniroot(f.ytm, interval=c(0,25))    ytm = solution$root   However, when I execute this code I get following error.   > solution = uniroot(f.ytm, interval=c(0,25))  Error in uniroot(f.ytm, interval = c(0, 25)) : f.lower = f(lower) is NA Please guide. ytm should be 0.09601 (i.e. 9.601%)     with regards   Madhavi Bhave          Your Mail works best with the New Yahoo Optimized IE8. Get it NOW! http://downloads.yahoo.com/in/internetexplorer/        [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[elided Yahoo spam]] [[alternative HTML version deleted]]