Hi all, I'm trying to get the value of y when x=203 by using the intersect of three curves. The horizontal curve does not meet with the other two. How can I rectify the code below? Thanks Muhammad ts <- 1:10 dd <- 10:1 ts <- seq(200,209,1) dd <- c(NA,NA,NA,NA,1.87,1.83,1.86,NA,1.95,1.96) plot(ts,dd,ylim=c(1.5,2)) abline(lm(dd~ts),col="blue",lty=2) abline(v=203,col="blue",lty=2) xy <- lm(dd~ts) fc <- function(x) coef(xy)[1] + x*coef(xy)[2] val <- optimize(f=function(x) abs(fc(x)-203),c(1.5,2)) abline(h=val,col="blue",lty=2)
optimize
6 messages · Joshua Wiley, Muhammad Rahiz, David Winsemius
Hi Muhammad, On Mon, Jan 3, 2011 at 6:52 AM, Muhammad Rahiz
<muhammad.rahiz at ouce.ox.ac.uk> wrote:
Hi all, I'm trying to get the value of y when x=203 by using the intersect of three curves. The horizontal curve does not meet with the other two. How can I rectify the code below?
What is your code supposed to do? This is one way to get three intersecting curves, but I am not sure it is doing what you were hoping to do with the sample code you sent. ts <- seq(200, 209, 1) dd <- c(NA,NA,NA,NA,1.87,1.83,1.86,NA,1.95,1.96) xy <- lm(dd ~ ts) plot(x = ts, y = dd, ylim = c(1.5,2)) abline(xy, col = "blue", lty = 2) abline(v = 203, h = predict(xy, data.frame(ts = 203)), col = "blue", lty = 2) Best regards, Josh
Thanks Muhammad ?ts <- 1:10 ?dd <- 10:1 ?ts <- seq(200,209,1) ?dd <- c(NA,NA,NA,NA,1.87,1.83,1.86,NA,1.95,1.96) ?plot(ts,dd,ylim=c(1.5,2)) ?abline(lm(dd~ts),col="blue",lty=2) ?abline(v=203,col="blue",lty=2) ?xy <- lm(dd~ts) ?fc <- function(x) coef(xy)[1] + x*coef(xy)[2] ?val <- optimize(f=function(x) abs(fc(x)-203),c(1.5,2)) ?abline(h=val,col="blue",lty=2)
______________________________________________ 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.
Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
On Jan 3, 2011, at 9:52 AM, Muhammad Rahiz wrote:
Hi all, I'm trying to get the value of y when x=203 by using the intersect of three curves. The horizontal curve does not meet with the other two. How can I rectify the code below?
Extend the appropriate axes. xlim and ylim are the arguments to use. Looks too much like a homework problem for me to do the work for you, though.
Thanks Muhammad ts <- 1:10 dd <- 10:1 ts <- seq(200,209,1) dd <- c(NA,NA,NA,NA,1.87,1.83,1.86,NA,1.95,1.96) plot(ts,dd,ylim=c(1.5,2)) abline(lm(dd~ts),col="blue",lty=2) abline(v=203,col="blue",lty=2) xy <- lm(dd~ts) fc <- function(x) coef(xy)[1] + x*coef(xy)[2] val <- optimize(f=function(x) abs(fc(x)-203),c(1.5,2)) abline(h=val,col="blue",lty=2)
David Winsemius, MD West Hartford, CT
On Jan 3, 2011, at 9:52 AM, Muhammad Rahiz wrote:
Hi all, I'm trying to get the value of y when x=203 by using the intersect of three curves. The horizontal curve does not meet with the other two. How can I rectify the code below? Thanks Muhammad ts <- 1:10 dd <- 10:1 ts <- seq(200,209,1) dd <- c(NA,NA,NA,NA,1.87,1.83,1.86,NA,1.95,1.96) plot(ts,dd,ylim=c(1.5,2)) abline(lm(dd~ts),col="blue",lty=2) abline(v=203,col="blue",lty=2) xy <- lm(dd~ts) fc <- function(x) coef(xy)[1] + x*coef(xy)[2] val <- optimize(f=function(x) abs(fc(x)-203), c(1.5,2))
^???^ Joshua Wiley's answer made me realize that your question was different than I thought. At this point maybe you should have made your objective function match on the coordinate difference to be minimized. You are at this point taking the absolute difference between an x- value, 203. and a y-value. fc(x). That makes little sense. You can use predict() as Joshua showed you or you can change your objective functions so it is more meaningful. Furthermore, I have had better success with squared differences that with abs(differences). I'm not sure whether that is due to the availability of a more informative derivative. (Again, looks like homework and I refrain from posting completed solutions.)
abline(h=val,col="blue",lty=2)
-- David Winsemius, MD West Hartford, CT
Josh's recommendation to use predict works. At the same time, I'll work on your suggestions, David. Thanks. Muhammad Rahiz Researcher & DPhil Candidate (Climate Systems & Policy) School of Geography & the Environment University of Oxford
On Mon, 3 Jan 2011, David Winsemius wrote:
On Jan 3, 2011, at 9:52 AM, Muhammad Rahiz wrote:
Hi all, I'm trying to get the value of y when x=203 by using the intersect of three curves. The horizontal curve does not meet with the other two. How can I rectify the code below? Thanks Muhammad ts <- 1:10 dd <- 10:1 ts <- seq(200,209,1) dd <- c(NA,NA,NA,NA,1.87,1.83,1.86,NA,1.95,1.96) plot(ts,dd,ylim=c(1.5,2)) abline(lm(dd~ts),col="blue",lty=2) abline(v=203,col="blue",lty=2) xy <- lm(dd~ts) fc <- function(x) coef(xy)[1] + x*coef(xy)[2] val <- optimize(f=function(x) abs(fc(x)-203), c(1.5,2))
^???^ Joshua Wiley's answer made me realize that your question was different than I thought. At this point maybe you should have made your objective function match on the coordinate difference to be minimized. You are at this point taking the absolute difference between an x- value, 203. and a y-value. fc(x). That makes little sense. You can use predict() as Joshua showed you or you can change your objective functions so it is more meaningful. Furthermore, I have had better success with squared differences that with abs(differences). I'm not sure whether that is due to the availability of a more informative derivative. (Again, looks like homework and I refrain from posting completed solutions.)
abline(h=val,col="blue",lty=2)
-- David Winsemius, MD West Hartford, CT
On Jan 3, 2011, at 11:09 AM, Muhammad Rahiz wrote:
Josh's recommendation to use predict works. At the same time, I'll work on your suggestions, David. Thanks. Muhammad Rahiz Researcher & DPhil Candidate (Climate Systems & Policy) School of Geography & the Environment University of Oxford On Mon, 3 Jan 2011, David Winsemius wrote:
On Jan 3, 2011, at 9:52 AM, Muhammad Rahiz wrote:
Hi all, I'm trying to get the value of y when x=203 by using the intersect of three curves. The horizontal curve does not meet with the other two. How can I rectify the code below? Thanks Muhammad ts <- 1:10 dd <- 10:1 ts <- seq(200,209,1) dd <- c(NA,NA,NA,NA,1.87,1.83,1.86,NA,1.95,1.96) plot(ts,dd,ylim=c(1.5,2)) abline(lm(dd~ts),col="blue",lty=2) abline(v=203,col="blue",lty=2) xy <- lm(dd~ts) fc <- function(x) coef(xy)[1] + x*coef(xy)[2] val <- optimize(f=function(x) abs(fc(x)-203), c(1.5,2))
^???^
And a further observation: You should pay close attention to the limits of optimization. If your answers keep coming out at the extremes of the ranges then perhaps you have confused the x and y coordinates there, as well as in your objective function.
David. >> >> Joshua Wiley's answer made me realize that your question was >> different >> than I thought. At this point maybe you should have made your >> objective function match on the coordinate difference to be >> minimized. >> You are at this point taking the absolute difference between an x- >> value, 203. and a y-value. fc(x). That makes little sense. You can >> use >> predict() as Joshua showed you or you can change your objective >> functions so it is more meaningful. Furthermore, I have had better >> success with squared differences that with abs(differences). I'm not >> sure whether that is due to the availability of a more informative >> derivative. (Again, looks like homework and I refrain from posting >> completed solutions.) >> >>> abline(h=val,col="blue",lty=2) >> >> -- >> David Winsemius, MD >> West Hartford, CT >> >> David Winsemius, MD West Hartford, CT