Hi, Suppose I have two vectors, not necessarily the same length (in fact, they usually are different lengths): y.1 that has increasing values between 0 and 1; y.2 that has decreasing values between 1.0 and 0. You can picture these as being supply (= y.1) and demand (= y.2) curves from economics. I typically plot these vectors on the same graph against a common x variable, which happens to be price for what I do. The price variable runs from, say, $0 to $25. When I plot y.1 and y.2, I've been eye-balling a vertical line at a price point where y.1 intersects y.2. I'm now tired of eye-balling a line through the intersection -- takes too much time to get it right or just close enough. I can't figure out how to find the price value at which the two curves intersect. Going back to the economics interpretation, I want the price where supply equals demand. Any suggestions as to how I can find that price point in R? Any functions that help? Thanks, Walt ________________________ Walter R. Paczkowski, Ph.D. Data Analytics Corp. 44 Hamilton Lane Plainsboro, NJ 08536 ________________________ (V) 609-936-8999 (F) 609-936-3733 walt at dataanalyticscorp.com www.dataanalyticscorp.com
On Jul 6, 2011, at 4:43 PM, Data Analytics Corp. wrote:> Hi, > > Suppose I have two vectors, not necessarily the same length (in > fact, they usually are different lengths): y.1 that has increasing > values between 0 and 1; y.2 that has decreasing values between 1.0 > and 0. You can picture these as being supply (= y.1) and demand (= > y.2) curves from economics. I typically plot these vectors on the > same graph against a common x variable, which happens to be price > for what I do. The price variable runs from, say, $0 to $25. When > I plot y.1 and y.2, I've been eye-balling a vertical line at a price > point where y.1 intersects y.2. I'm now tired of eye-balling a line > through the intersection -- takes too much time to get it right or > just close enough. I can't figure out how to find the price value > at which the two curves intersect. Going back to the economics > interpretation, I want the price where supply equals demand. Any > suggestions as to how I can find that price point in R? Any > functions that help??approxfun # or.. ?splinefun # should allow you to make two functions and then solve for the X taht minimizes the difference. -- David Winsemius, MD West Hartford, CT
On 07/07/11 08:43, Data Analytics Corp. wrote:> Hi, > > Suppose I have two vectors, not necessarily the same length (in fact, > they usually are different lengths): y.1 that has increasing values > between 0 and 1; y.2 that has decreasing values between 1.0 and 0. > You can picture these as being supply (= y.1) and demand (= y.2) > curves from economics. I typically plot these vectors on the same > graph against a common x variable, which happens to be price for what > I do. The price variable runs from, say, $0 to $25. When I plot y.1 > and y.2, I've been eye-balling a vertical line at a price point where > y.1 intersects y.2. I'm now tired of eye-balling a line through the > intersection -- takes too much time to get it right or just close > enough. I can't figure out how to find the price value at which the > two curves intersect. Going back to the economics interpretation, I > want the price where supply equals demand. Any suggestions as to how > I can find that price point in R? Any functions that help?There will actually be two pairs of points, one pair on the price curve and one pair on the demand curve, so that the intersection of the two curves lies between the respective pair on each curve. Without an algebraic expression for the curves, that's all you can say. The following function joins the respective pairs of points> findInt <- function (x1,y1,x2,y2,plot=FALSE) { > # > # x1 and y1 are the coordinates of the points on the INCREASING curve. > # x2 and y2 are the coordinates of the points on the DECREASING curve. > # > y1star <- approx(x2,y2,xout=x1,yleft=Inf,yright=-Inf)$y > k <- sum(y1 <= y1star) > y2star <- approx(x1,y1,xout=x2,yleft=-Inf,yright=Inf)$y > ell <- sum(y2 >= y2star) > b1 <- y1[k] > b2 <- y2[ell] > m1 <- (y1[k+1] - y1[k])/(x1[k+1] - x1[k]) > m2 <- (y2[ell+1] - y2[ell])/(x2[ell+1] - x2[ell]) > x <- (b1-b2-m1*x1[k]+m2*x2[ell])/(m2-m1) > y <- b1 + m1*(x-x1[k]) > if(plot) { > plot(x1,y1,xlim=range(x1,x2),ylim=range(y1,y2)) > points(x2,y2,col="red") > segments(x1[k],y1[k],x1[k+1],y1[k+1]) > segments(x2[ell],y2[ell],x2[ell+1],y2[ell+1]) > points(x,y,pch=20) > } > c(x=x,y=y) > }by straight lines and finds the point of intersection of the two lines. This is probably pretty similar to the point you'd get by eye-balling the data. cheers, Rolf Turner
On Wed, Jul 6, 2011 at 9:43 PM, Data Analytics Corp. <walt at dataanalyticscorp.com> wrote:> close enough. ?I can't figure out how to find the price value at which the > two curves intersect. ?Going back to the economics interpretation, I want > the price where supply equals demand. ?Any suggestions as to how I can find > that price point in R? ?Any functions that help?You could roll out the 100,000-pound gorilla that is rgeos, treat the two lines as spatial lines and then use gIntersection: > x1 [1] 3 4 6 8 10 > x2 [1] 1 2 6 7 10 > y1 [1] 0.23898824 0.48215370 0.45215557 0.08049115 0.18068038 > y2 [1] 0.2749391 0.3638511 0.1650239 0.3064780 0.8515887 > s1=SpatialLines(list(Lines(list(Line(cbind(x1,y1))),ID=1))) > s2=SpatialLines(list(Lines(list(Line(cbind(x2,y2))),ID=1))) > gIntersection(s1,s2) SpatialPoints: x y 1 3.256617 0.3013887 1 6.877310 0.2891230 Coordinate Reference System (CRS) arguments: NA Here my example crosses twice at those x-y coordinates. Note that if the two lines are exactly equal along a line, you'll get back a SpatialLines object as part of your result. In your case this would be if supply dropped to 24 as demand rose to 24 and they both stayed like that for some time before crossing. If you want the first time that the values are equal you'd just take the minimum X-coordinate in the returned object... But yeah, it might be overkill, but possibly handy if you want to compute multiple times when two curves cross. Barry