From: Uzuner, Tolga <tolga.uzuner_at_csfb.com>

Date: Wed 21 Dec 2005 - 06:38:12 EST

Please access the attached hyperlink for an important electronic communications disclaimer:

R-help@stat.math.ethz.ch mailing list

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Dec 21 07:35:28 2005

Date: Wed 21 Dec 2005 - 06:38:12 EST

Fantastic. Many thanks, this is great. All understood. I will go with the first recommendation (pass it in explicitly).
Regards,

Tolga

This material is sales and trading commentary and does not constitute investment research. Please follow the attached hyperlink to an important disclaimer http://www.csfb.com/legal_terms/disclaimer_europe.shtml

-----Original Message-----

From: Gabor Grothendieck [mailto:ggrothendieck@gmail.com]
Sent: 20 December 2005 19:30

To: Uzuner, Tolga

Cc: r-help@stat.math.ethz.ch

Subject: Re: [R] help with sapply, plot, lines

The following is as in your post but cleaned up slightly. Note in particular that str is an R function and although its not wrong to also use it as a variable we use Str below to make it clearer: Also we define a variable xx as shown.

atm <- 0.4

rr <- 0.2

Str <- 0.1

sigma <- function(lambda) atm-2*rr*(lambda-0.5)+16*Str*(lambda-0.5)^2
xx <- seq(0, 0.3, 0.05)

plot(xx, sigma(xx))

sapply(xx, function(Str) lines(xx, sigma(xx))) # wrong!

Note that sigma is defined in the global environment so lexical scope implies that that is where sigma will look for Str -- not within the function defined in the sapply statement.

To correct this we could do one of the following:

- pass Str explicitly:

sigma2 <- function(Str, lambda) atm-2*rr*(lambda-0.5)+16*Str*(lambda-0.5)^2
plot(xx, sigma2(Str, xx))

sapply(xx, function(Str, lambda) lines(xx, sigma2(Str, xx)), lambda = xx)

2. (a) define sigma within the sapply so its lexical scope is as desired rather than the global environment:

plot(xx, sigma(xx)) # same sigma as defined at the top sapply(xx, function(Str, lambda) { sigma <- function(lambda) # new local sigma atm-2*rr*(lambda-0.5)+16*Str*(lambda-0.5)^2 lines(xx, sigma(xx)) })

(b) rather than write sigma out again we could make a copy of the original one and reset its environment:

plot(xx, sigma(xx)) # same sigma as defined at the top sapply(xx, function(Str, lambda) { environment(sigma) <- environment() lines(xx, sigma(xx)) })

(c) instead of using environment we could express it slightly more compactly using the proto package. A proto object is an environment but any function component of a proto object defined in a proto statement has its environment reset to that object:

library(proto)

plot(xx, sigma(xx))

sapply(xx, function(Str) lines(xx, with(proto(sigma = sigma), sigma(xx))))

On 12/20/05, Uzuner, Tolga <tolga.uzuner@csfb.com> wrote:

*> Hi,
**>
*

> I am trying to plot multiple lines on a graph.

*>
**> The function is particularly simple:
**>
**> sigma<-function(lambda) atm-2*rr*(lambda-0.5)+16*str*(lambda-0.5)^2
**>
**> which uses the variables atm, rr and str...
**>
**> I define these as such:
**>
**> atm<-0.4
**> rr<-0.2
**> str<-0.1
**>
**>
**> and this plots fine:
**>
**> plot(seq(0.01,0.99,0.01),sigma(seq(0.01,0.99,0.01)),ylim=c(0,1))
**>
**> Now, I want to plot the same function for different values of str, as follows:
**>
**> sapply(seq(0,0.3,0.05),function(s) {str<-s; lines(seq(0.01,0.99,0.01),sigma(seq(0.01,0.99,0.01)))})
**>
**> Hoping that sigma will lexically scope into str and that lines will appear on the same plot as the one I first drew above.
**>
**> Instead, I just get this:
**> > sapply(seq(0,0.3,0.05),function(s) {str<-s; lines(seq(0.01,0.99,0.01),sigma(seq(0.01,0.99,0.01)))})
**> [[1]]
**> NULL
**>
**> [[2]]
**> NULL
**>
**> [[3]]
**> NULL
**>
**> [[4]]
**> NULL
**>
**> [[5]]
**> NULL
**>
**> [[6]]
**> NULL
**>
**> [[7]]
**> NULL
**>
**> and the plot does not change.
**>
**> Any assistance appreciated.
**>
**> Regards,
**> Tolga
**>
**>
**>
**>
**>
**>
**>
**>
**> This material is sales and trading commentary and does not constitute investment research. Please follow the attached hyperlink to an important disclaimer http://www.csfb.com/legal_terms/disclaimer_europe.shtml
**>
**>
**>
**> ==============================================================================
**> Please access the attached hyperlink for an important electronic communications disclaimer:
**>
**> http://www.csfb.com/legal_terms/disclaimer_external_email.shtml
**>
**> ______________________________________________
**> R-help@stat.math.ethz.ch mailing list
**> https://stat.ethz.ch/mailman/listinfo/r-help
**> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
**>
*

Please access the attached hyperlink for an important electronic communications disclaimer:

http://www.csfb.com/legal_terms/disclaimer_external_email.shtml

R-help@stat.math.ethz.ch mailing list

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Dec 21 07:35:28 2005

*
This archive was generated by hypermail 2.1.8
: Wed 21 Dec 2005 - 09:33:56 EST
*