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)) #------- Can anyone help me figure out how to determine the peak (maximum "scores") value in the plot that is generated? 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!
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)) > > #------- > > Can anyone help me figure out how to determine the peak (maximum > "scores") value in the plot that is generated?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
> optimize(function(x) -scores(x), interval=c(0.5, 0))$minimum [1] 0.1830174 $objective [1] -11.67820 So the maximum is at x=0.183 with a score of 11.7, numerically. Uwe Ligges On 17.04.2010 16:30, 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)) > > #------- > > Can anyone help me figure out how to determine the peak (maximum > "scores") value in the plot that is generated? 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.