I've been using nls() to fit a custom model to my data, but I don't like how the model is fitting and I would like to use an approach that minimizes residuals in both x and y axes.

I've done a lot of searching, and have found solutions for fitting linear models:

but these fit a second order polynomial and not a custom, user-defined model.

What I would like is something similar to nls() that does the x and y residual minimization. This would allow me to enter my custom model. Is anyone aware of any solution in R?

Here's an example, but please note that I'm seeking suggestions on a general solution for nonlinear total least squares regression, and not something specific to this dataset (this is just an example data from here):

df <- structure(list(x = c(3, 4, 5, 6, 7, 8, 9, 10, 11), y = c(1.0385, 
1.0195, 1.0176, 1.01, 1.009, 1.0079, 1.0068, 1.0099, 1.0038)), .Names = c("x", 
"y"), row.names = c(NA, -9L), class = "data.frame")

(nlsfit <- nls(y ~ a^b^x, data = df, start = c(a=0.9, b=0.6)))

ggplot(df, aes(x=x, y=y)) + 
    geom_point() + 
    geom_smooth(method="nls", formula = y ~ a^b^x, se=F, start = list(a=0.9, b=0.6))

Does anyone have any suggestion for how I might proceed?

  • $\begingroup$ If you are only looking for R code, that would be off-topic here. However, I can see a more general (NLS + TLS) question here, whose answer can be illustrated in R. You may want to reframe your Q. $\endgroup$ Commented Mar 14, 2015 at 23:55
  • $\begingroup$ Thanks gung, do you have suggestions on how i should reframe? I'm not just looking for R code, i'm looking for examples that show how this might be done. thanks. $\endgroup$
    – Thomas
    Commented Mar 15, 2015 at 0:25
  • $\begingroup$ No problem, you're fine. $\endgroup$ Commented Mar 15, 2015 at 14:55

1 Answer 1


There is a technique called "Orthogonal Distance Regression" that does this. An implementation in R was recently released:


  • $\begingroup$ Thanks Brian Borchers, that is exactly what I was looking for! $\endgroup$
    – Thomas
    Commented Mar 15, 2015 at 2:19
  • 1
    $\begingroup$ Since links can go dead, can you excerpt the key information here? $\endgroup$ Commented Mar 15, 2015 at 14:56
  • $\begingroup$ The link points to a blog posting describing a recent implementation of Orthogonal Distance Regression in R. A google (or google scholar) search for "Orthogonal Distance Regression" will lead you to lots of papers on the topic. $\endgroup$ Commented Mar 15, 2015 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.