[R] help with sapply, plot, lines
Gabor Grothendieck
ggrothendieck at gmail.com
Tue Dec 20 20:58:58 CET 2005
On 12/20/05, Uzuner, Tolga <tolga.uzuner at csfb.com> wrote:
> 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.
>
The environment into which sigma looks for str is the environment
where sigma was defined, i.e. the global environment, not the environment
where sigma was called from. That's what lexical scoping means.
The alternative, in which sigma would look into the environment from
which it is called would be dynamic scoping but that's not the way
R works.
> 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 at 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 at csfb.com <mailto: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 <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 at stat.math.ethz.ch <mailto:R-help at 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]]
>
> ______________________________________________
> 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
>
More information about the R-help
mailing list