[R] Question regarding R plot
David Winsemius
dwinsemius at comcast.net
Sat Apr 17 21:20:50 CEST 2010
On Apr 17, 2010, at 3:13 PM, Akito Y. Kawahara wrote:
> Thanks, this is really great!
>
> Just another quick question - if one were to calculate the mean of
> this curve, what command would you suggest? "Optimize" doesn't allow
> you to search for a mean value since it is only working on one point,
> and am just curious if there is a quick way to do this. Sorry for the
> basic questions.
Any reason to not accept this approach?
> mean(scores(seq(0.5, 0, by=-0.0001)))
[1] 9.112905
>
> On Sat, Apr 17, 2010 at 2:49 PM, David Winsemius <dwinsemius at comcast.net
> > wrote:
>>
>> On Apr 17, 2010, at 1:18 PM, David Winsemius wrote:
>>
>>>
>>> On Apr 17, 2010, at 12:45 PM, Charles C. Berry wrote:
>>>
>>>> On Sat, 17 Apr 2010, Akito Y. Kawahara wrote:
>>>>
>>>>> Hi, I am new to R, and have a quick question regarding an R script
>>>>> that I received from a kind colleague.
>>>>>
>>>>> I am trying to determine the "peak" (maximum value) of the graph
>>>>> that
>>>>> is plotted when executing the following. There is an input file
>>>>> called
>>>>> "rates_values.txt" which begins as:
>>>>>
>>>>> rateValue
>>>>> 0.375693
>>>>> 0
>>>>> 1.71274
>>>>> 0
>>>>> 0
>>>>> 1.02832
>>>>> 0
>>>>> 0.16343
>>>>> 1.02349
>>>>> 0
>>>>> 0
>>>>> 1.47258
>>>>> 0.703522
>>>>> 0.390541
>>>>> 1.83415
>>>>>
>>>>>
>>>>>
>>>>> The script, below, must run with the rates_values.txt in the
>>>>> same dir.
>>>>>
>>>>> #-------
>>>>> rates<-read.table("rates_values.txt",header=T)
>>>>> attach(rates)
>>>>> scores<-function(x){
>>>>> l<-length(rateValue)
>>>>> total<-0
>>>>> for(i in 1:l){
>>>>> value<-16*rateValue[i]*rateValue[i]*x*exp(-4*rateValue[i]*x)
>>>>> total<-total+value
>>>>> }
>>>>> return(total)
>>>>> }
>>>>>
>>>>>
>>>>> #-------
>>>>> #preparing the plot
>>>>> linewidth=3
>>>>> linetype=3
>>>>> color="blue"
>>>>> xvalue=0.5
>>>>>
>>>>> plot(scores,xvalue,0,type="n",font.axis=2,xlim=c(xvalue,0))
>>>>>
>>>>> curve
>>>>> (scores
>>>>> ,xvalue
>>>>> ,add=TRUE,col=color,lty=linetype,lwd=linewidth,xlim=c(xvalue,0))
>>>
>>> I was a bit puzzled by these lines, since in most R plotting tasks
>>> one
>>> uses plot(x, y, ...). So unless the task it to plot some inverse
>>> function,
>>> then the arguments are reversed. Furthermore, scores was defined
>>> above as a
>>> function of "x", and yet is being offered to plot with no
>>> arguments. What is
>>> intended?
>>
>> Thanks to Charles Berry for advising me that this is an acceptable,
>> if
>> somewhat obscure, usage of plot.function().
>>
>>>
>>>
>>>>>
>>>>> #-------
>>>>>
>>>>> Can anyone help me figure out how to determine the peak (maximum
>>>>> "scores") value in the plot that is generated?
>>
>> Perhaps this approximation would be effective:
>>
>>> max(scores(seq(0.5, 0, by=-0.001)))
>> [1] 11.67820
>>
>>
>> An alternate approach might be to calculate a smoothing function,
>> but this
>> seems to "work" in the sense of giving an answer that matches the
>> results
>> from Berry's suggestion:
>>
>>> optimise( scores, range(rateValue), maximum=TRUE )
>> $maximum
>> [1] 0.1830172
>>
>> $objective
>> [1] 11.67820
>>
>>>>
>>>>
>>>> Yes. Anyone who knows something called 'the calculus' could help.
>>>> And
>>>> they could point out that the 0's in the data really do not matter.
>>>>
>>>> If the 0's are real data that should influence the result, it
>>>> seems there
>>>> is something wrong with scores() and you might do well to get a
>>>> consult from
>>>> a statistician.
>>>>
>>>> If you are still determined to solve the problem as stated, see
>>>>
>>>> ?optimise
>>>>
>>>>
>>>> Something like
>>>>
>>>> optimise( scores, range(rateValue), maximum=TRUE )
>>>>
>>>> should do it.
>>>>
>>>> HTH,
>>>>
>>>> Chuck
>>>>
>>>>
>>>> It should be about 11.7
>>>>>
>>>>> but I would like to get an exact value. This should be a
>>>>> relatively
>>>>> easy question, but I'm new to R, and what I have tried doesn't
>>>>> seem to
>>>>> work.
>>>>>
>>>>> Thanks!
>>>>>
>>>>> ______________________________________________
>>>>> R-help at 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.
>>>>>
>>>>
>>>> Charles C. Berry (858) 534-2098
>>>> Dept of Family/Preventive
>>>> Medicine
>>>> E mailto:cberry at tajo.ucsd.edu UC San Diego
>>>> http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego
>>>> 92093-0901
>>>>
>>>> ______________________________________________
>>>> R-help at 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.
>>>
>>> David Winsemius, MD
>>> West Hartford, CT
>>>
>>> ______________________________________________
>>> R-help at 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.
>>
>> David Winsemius, MD
>> West Hartford, CT
>>
>> ______________________________________________
>> R-help at 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.
>>
>
>
>
> --
> Akito Y. Kawahara
> Department of Entomology
> University of Maryland
> 4112 Plant Sciences Building
> College Park, MD 20742-4454
> Tel: 301.405.2089
> Fax: 301.314.9290
> kawahara at umd.edu
>
> ______________________________________________
> R-help at 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.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list