Does anyone know of a method that I can get the intersection where the red and blue curves meet i.e. the value on the x-axis? x <- 1:10 y <- 10:1 plot(x,y) abline(lm(y~x),col="blue") abline(h=2.5,col="red") Muhammad
On Apr 23, 2010, at 1:06 PM, Muhammad Rahiz wrote:> Does anyone know of a method that I can get the intersection where > the red and blue curves meet i.e. the value on the x-axis? > > x <- 1:10 > y <- 10:1 > plot(x,y) > abline(lm(y~x),col="blue") > abline(h=2.5,col="red")Two ways : > xy <- lm(y~x) > xyf <- function(x) coef(xy)[1] +x*coef(xy)[2] # absolute difference > optimise(f=function(x) abs(xyf(x)-2.5), c(1,10) ) $minimum [1] 8.49998 $objective (Intercept) 1.932015e-05 #N minimize squared difference > optimise(f=function(x) (xyf(x)-2.5)^2, c(1,10) ) $minimum [1] 8.5 $objective (Intercept) 3.155444e-30 -- David Winsemius, MD West Hartford, CT
On 2010-04-23 11:46, David Winsemius wrote:> > On Apr 23, 2010, at 1:06 PM, Muhammad Rahiz wrote: > >> Does anyone know of a method that I can get the intersection where the >> red and blue curves meet i.e. the value on the x-axis? >> >> x <- 1:10 >> y <- 10:1 >> plot(x,y) >> abline(lm(y~x),col="blue") >> abline(h=2.5,col="red") > > Two ways : > > > xy <- lm(y~x) > > xyf <- function(x) coef(xy)[1] +x*coef(xy)[2] > > # absolute difference > > optimise(f=function(x) abs(xyf(x)-2.5), c(1,10) ) > $minimum > [1] 8.49998 > > $objective > (Intercept) > 1.932015e-05 > > #N minimize squared difference > > optimise(f=function(x) (xyf(x)-2.5)^2, c(1,10) ) > $minimum > [1] 8.5 > > $objective > (Intercept) > 3.155444e-30 >Another (crude) way is to use locator(). I usually maximize the plot window for this. -- Peter Ehlers University of Calgary
<quote> Well it sounds like you won't lose any work, but you will lose some time. But why not open another session of R for R-help questions? Then you're never in any danger. I often have four or five instances of R running for different projects. Hadley </quote> Well for one thing you turkey, OS X will not under normal circumstances open two instances of a given app. Don't be Windows-centric. And as Gabor said, why post bad code in the first place? rm(list=ls()) is practically a cliche precisely because it's nuclear and should never be used anywhere. Typical code listed in package help files may end with unlink([stuff]), which makes far better sense than, e.g. unlink(*.*) . Carl
<quote> > Many people seem to be reluctant to define functions, > even thought I think it is a pretty small step from > writing scripts to writing functions. I'm not so sure - I find most students struggle to grasp that next level of abstraction. Generalising from a specific task to a general function is a big step. Most R users aren't programmers and many have never had any formal training - I think writing functions well is hard to pick up on your own. Hadley </quote> With all due respect, I fail to see how *anyone* can use R without some basic understanding of programming. Same goes for Mathematica and Matlab, just in case anyone thought I was app-biased :-) . From what I see both at work (hi-tech optics company) and everything my kids are doing in college and grad school, it's simply unthinkable that someone in any branch of science or math (or finance, business, etc. for that matter) can operate in a modern environment without knowing some basic principles of programming. Carl