1

I have two vectors. I need to find the intersection between these two, and do a nice plot of it.

So, here is a very simple data frame example:

df <- data.frame( id <- c(1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2),
                 p <-c(5,7,9,11,13,15,17,19,21,23,20,18,16,14,12,10,8,6,4,2 ),
                 q <-c(3,5,7,13,19,31,37,53,61,67,6,18,20,24,40,46,66,70,76,78))
colnames(df) <- c("id","price","quantity")

supply <- df[df$id == 1,]
demand <- df[df$id == 2,]

plot( x = supply$quantity,  y = supply$price, type = "l", ylab = "price", xlab = "quantity")
lines(x = demand$quantity , y = demand$price, type = "l")
grid()

Now, I can plot them and find the intersection manually, but can you make R calculate the intersection between these two lines?

The data can take huge jumps, and the lines can go from very step to nearly horizontal.

Hack-R
  • 22,422
  • 14
  • 75
  • 131
Thorst
  • 1,590
  • 1
  • 21
  • 35
  • If they are lines! I suspect you need to learn a little math before going further. Please look up some material on doing a linear fit (aka linear regression) and then go to any geometry book to get the formula for the intersection of two (nonparallel) lines. – Carl Witthoft Oct 21 '13 at 12:15
  • The data is at best piece-wise linear, so you can't really do a linear approximation, it won't give the right result. – Thorst Oct 21 '13 at 12:19
  • In that case, you need to come up with a definition of "intersect." Do you want the intersection of spline fits, for example? What is the actual problem you are trying to solve? (quoting the Data Munger Guru) Finding an intersection if your source data are crappy won't help you. – Carl Witthoft Oct 21 '13 at 13:10
  • @Carl : It seems to me that OP is trying to find out the equilibrium quantity and equilibrium price: quantity/price at which quantity demanded-quantity supplied. This is not programming question to me. Voting for close – Metrics Oct 21 '13 at 13:18
  • This question appears to be off-topic because it is about economics – Metrics Oct 21 '13 at 13:18
  • Yes it is about economics, but its an programming problem im faced with in R. How can that be off-topic ? – Thorst Oct 21 '13 at 13:22
  • If it's piece-wise linear you could calculate an intersection for each combination of lines by the method described here http://stackoverflow.com/questions/7114703/finding-where-two-linear-fits-intersect-in-r?rq=1 and stop when the intersection is located between 2 points of one of the lines used to calculate the intersection. – Jonas Tundo Oct 21 '13 at 13:37
  • Have you tried `crossing.psp` in the `spatstat` package? "Finds any crossing points between two line segment patterns". – Henrik Oct 21 '13 at 14:02

1 Answers1

5

Be careful creating your data frame. You want =, not <-. Also, make id a factor, for clarity.

df <- data.frame( 
  id       = factor(rep(c("supply", "demand"), each = 10)),
  price    = c(5,7,9,11,13,15,17,19,21,23,20,18,16,14,12,10,8,6,4,2 ),
  quantity = c(3,5,7,13,19,31,37,53,61,67,6,18,20,24,40,46,66,70,76,78)
)

First we define common, frequent points to evaluate the quantity at.

quantity_points <- with(
  df,
  seq(min(quantity), max(quantity), length.out = 500)
)

Now split the dataset into supply/demand parts.

by_id <- split(df[, c("price", "quantity")], df$id)

Then we use approx to calculate the price at each of these quantities, for supply and demand separately.

interpolated_price <- lapply(
  by_id,
  function(x)
  {
    with(
      x,
      approx(
        quantity,
        price, 
        xout = quantity_points
      )
    )$y       
  }
)

Finally, the crossing point is where the absolute value of the supply price minus the demand price is minimised.

index_of_equality <- with(interpolated_price, which.min(abs(supply - demand)))
quantity_points[index_of_equality]
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360