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
Ah OK. That is probably it, though I can't see why. In function sigma, I use a variable falled str, which would be bound in the environment to some value. Then, in the sapply statment, I was hoping that by setting str to the values in the vector that is the first argument to sapply, I would get different plots each time I called lines afterwards with sigma. Do you see what I mean ? 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 <http://www.csfb.com/legal_terms/disclaimer_europe.shtml> > -----Original Message----- From: jim holtman [mailto:jholtman@gmail.com] Sent: 20 December 2005 19:20 To: Uzuner, Tolga Subject: Re: [R] help with sapply, plot, lines you are plotting the same line as originally over and over again. what do you want to do with the parameters that you are passing in. There is no call to sigma in the sapply. How do you want to vary the parameters in the plot? On 12/20/05, Uzuner, Tolga < tolga.uzuner@csfb.com <mailto: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 <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 <http://www.csfb.com/legal_terms/disclaimer_external_email.shtml> ______________________________________________ R-help@stat.math.ethz.ch <mailto:R-help@stat.math.ethz.ch> mailing list https://stat.ethz.ch/mailman/listinfo/r-help <https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html <http://www.R-project.org/posting-guide.html> -- Jim Holtman Cincinnati, OH +1 513 247 0281 What the problem you are trying to solve? =============================================================================Please access the attached hyperlink for an important electronic communications disclaimer: http://www.csfb.com/legal_terms/disclaimer_external_email.shtml ============================================================================= [[alternative HTML version deleted]]
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: 1. 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 at 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 at 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 >
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 at gmail.com] Sent: 20 December 2005 19:30 To: Uzuner, Tolga Cc: r-help at 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: 1. 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 at 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 at 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
> 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. >Lexical scoping means that the free variable str in sigma is looked for in the enclosing environment, not the parent environment. This is the environment in which sigma was defined, not called. Hence str is constant in your calls and you get identical results at each call. See the section 3.3.1 of the R FAQ. In your example, the straightforward solution is to pass str as an argument of sigma. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Uzuner, Tolga > Sent: Tuesday, December 20, 2005 10:18 AM > To: 'r-help at stat.math.ethz.ch' > Subject: [R] help with sapply, plot, lines > > 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 at 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 >