Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6355335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:44:41+00:00 2026-05-24T22:44:41+00:00

I have 5 (x,y) data points and I’m trying to find a best fit

  • 0

I have 5 (x,y) data points and I’m trying to find a best fit solution consisting of two lines which intersect at a point (x0,y0), and which follow these equations:

y1 = (m1)(x1 - x0) + y0
y2 = (m2)(x2 - x0) + y0

Specifically, I require that the intersection must occur between x=2 and x=3. Have a look at the code:

#Initialize x1, y1, x2, y2
x1 <- c(1,2)
y1 <- c(10,10)

x2 <- c(3,4,5)
y2 <- c(20,30,40)

g <- c(TRUE, TRUE, FALSE, FALSE, FALSE)

q <- nls(c(y1, y2) ~ ifelse(g == TRUE, m1 * (x1 - x0) + y0, m2 * (x2 - x0) + y0), start = c(m1 = -1, m2 = 1, y0 = 0, x0 = 2), algorithm = "port", lower = c(m1 = -Inf, m2 = -Inf, y0 = -Inf, x0 = 2), upper = c(m1 = Inf, m2 = Inf, y0 = Inf, x0 = 3))
coef <- coef(q)
m1 <- coef[1]
m2 <- coef[2]
y0 <- coef[3]
x0 <- coef[4]

#Plot the original x1, y1, and x2, y2
plot(x1,y1,xlim=c(1,5),ylim=c(0,50))
points(x2,y2)

#Plot the fits
x1 <- c(1,2,3,4,5)
fit1 <- m1 * (x1 - x0) + y0
lines(x1, fit1, col="red")

x2   <- c(1,2,3,4,5)
fit2 <- m2 * (x2 - x0) + y0
lines(x2, fit2, col="blue")

So, you can see the data points listed there. Then, I run it through my nls, get my parameters m1, m2, x0, y0 (the slopes, and the intersection point).

But, take a look at the solution:
enter image description here

Clearly, the red line (which is supposed to only be based on the first 2 points) is not the best line of fit for the first 2 points. This is the same case with the blue line (the 2nd fit), which supposed to be is dependent on the last 3 points). What is wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:44:42+00:00Added an answer on May 24, 2026 at 10:44 pm

    I’m not exactly sure what’s wrong but I can get it to work by rearranging things a bit. Please note the comment in ?nls about “Do not use ‘nls’ on artificial “zero-residual” data.“; I added a bit of noise.

    ## Initialize x1, y1, x2, y2
    x1 <- c(1,2)
    y1 <- c(10,10)
    
    x2 <- c(3,4,5)
    y2 <- c(20,30,40)
    
    ## make single x, y vector
    x <- c(x1,x2)
    set.seed(1001)
    ## (add a bit of noise to avoid zero-residual artificiality)
    y <- c(y1,y2)+rnorm(5,sd=0.01)
    
    g <- c(TRUE,TRUE,FALSE,FALSE,FALSE) ## specify identities of points
    
    ## particular changes:
    ##   * you have lower=upper=2 for x0.  Did you want 2<x0<3?
    ##   * specified data argument explicitly (allows use of predict() etc.)
    ##   * changed name from 'q' to 'fit1' (avoid R built-in function)
    fit1 <- nls(y ~ ifelse(g,m1,m1+delta_m)*(x - x0) + y0,
             start = c(m1 = -1, delta_m = 2, y0 = 0, x0 = 2),
             algorithm = "port",
             lower = c(m1 = -Inf, delta_m = 0, y0 = -Inf, x0 = 2),
             upper = c(m1 = Inf, delta_m = Inf, y0 = Inf, x0 = 3),
             data=data.frame(x,y))
    
    #Plot the original 'data'
    plot(x,y,col=rep(c("red","blue"),c(2,3)),
               xlim=c(1,5),ylim=c(0,50))
    
    ## add predicted values
    xvec <- seq(1,5,length.out=101)
    lines(xvec,predict(fit1,newdata=data.frame(x=xvec)))
    

    edit: based ifelse clause on point identity, not x position

    edit: changed to require second slope to be > first slope

    On a second look, I think the issue above is probably due to the use of separate vectors for x1 and x2 above, rather than a single x vector: I suspect these got replicated by R to match up with the g vector, which would have messed things up pretty badly. For example, this stripped-down example:

    g <- c(TRUE, TRUE, FALSE, FALSE, FALSE)
    ifelse(g,x1,x2)
    ## [1] 1 2 5 3 4
    

    shows that x2 gets extended to (3 4 5 3 4) before being used in the ifelse clause. The scariest part is that normally one gets a warning such as this:

    > x2 + 1:5
    [1] 4 6 8 7 9
    Warning message:
    In x2 + 1:5 :
      longer object length is not a multiple of shorter object length
    

    but in this case there is no warning …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data with a best fit line draw. I need to draw two
I have 8823 data points with x,y coordinates. I'm trying to follow the answer
I have thousands of data points and each data point has 50 dimensions. I
I've run simulations which have given me data points corresponding to X number of
I have a set of data points in 3D space which apparently all fall
I have an example class containing two data points: public enum Sort { First,
I have a database full of two-dimensional data - points on a map. Each
I have about 100 data points which mostly satisfying a certain function (but some
I have a set of 3d data points for each of which the value
I have a bunch of data points that I would like to two-way bind

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.