[R] help with sapply, plot, lines
Berton Gunter
gunter.berton at gene.com
Tue Dec 20 20:53:58 CET 2005
> 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
>
More information about the R-help
mailing list