2

Possible Duplicate:
Finding where two linear fits intersect in R

Given some points on a graph (usually only about 6 or 7 points), I need to find a best fit solution where the solution consists of the following:

  1. Two linear lines
  2. The lines must intersect
  3. The intersection point (the x point) must lie between two values I specify (such as xLow and xHigh)

How would I do this using nls (or something better?)?

If there are multiple best fits, then any of them are fine. Basically, the two lines form a V.

Community
  • 1
  • 1
CodeGuy
  • 28,427
  • 76
  • 200
  • 317
  • What happens when there are multiple solutions, i.e. multiple lines? And how is fit measured? Via the standard method of MSE of residuals for y_hat? – Iterator Aug 19 '11 at 15:04
  • yes, just the standard way that nls works. – CodeGuy Aug 19 '11 at 15:09
  • Two lines that cross forming an X? Or two lines that meet forming a V? – Aaron left Stack Overflow Aug 19 '11 at 16:02
  • -1 This was already asked and answered in the comments of http://stackoverflow.com/questions/7114703/finding-where-two-linear-fits-intersect-in-r/7115758#7115758 – G. Grothendieck Aug 19 '11 at 16:24
  • It looks like in that question, the points were defined as being from two different groups, but here they're in one group. CodeGuy, will you clarify? – Aaron left Stack Overflow Aug 19 '11 at 16:52
  • Basically, it's one set of x values and y values (just a simple 2D plot). I want to find a bilinear solution where the two linear lines intersect at a specific point between two values I specify – CodeGuy Aug 19 '11 at 16:54
  • I think people are missing the point, though it wasn't clear at first. The OP is asking for a piecewise linear function with one "knot" (in the spline sense), i.e. just 2 pieces, and the knot's location is within some specified range. – Iterator Aug 19 '11 at 20:04
  • I'm not aware of any R package that allows for linear b-splines with box constraints for the location of the knot. Might be a fun little thing to program. Of course, it could be done through easier methods. :) – Iterator Aug 19 '11 at 20:11

1 Answers1

1

Generally if you first do a fit of y on x and then x on y the lines will cross within the data at their joint means and they are both least squares fits, albeit to different data situations.

IRTFM
  • 258,963
  • 21
  • 364
  • 487