# [R] fitting a curve according to a custom loss function

Liaw, Andy andy_liaw at merck.com
Wed Feb 19 14:34:03 CET 2003

```> From: M.Kondrin [mailto:mkondrin at hppi.troitsk.ru]
> Sent: Wednesday, February 19, 2003 2:32 PM
>
>
> >Dear R-Users,
> >
> >I need to find a smooth function f() and coefficients a_i
> that give the best
> >fit to
> >
> >y ~ a_0 + a_1*f(x_1) + a_2*f(x_2)
> >
> >Note that it is the same non-linear transformation f() that
> is applied to
> >both x_1 and x_2.
> >
> >So my first question is how can I do it in R?
> >
> >A more general question is this: suppose I have a utility
> function U(a_i,
> >f()), where f() is say a spline. Is there a general
> optimizer that could
> >find an extremum of such U()? If not, how easy it would be to hack up
> >something like this? Would it become easier if U() depended
> on f() only,
> >i.e. no a_i terms?
> >
> >
> >--------------------------------------------------
> >DISCLAIMER \ This e-mail, and any attachments thereto, is
> intend ... [[dropped]]
> >
> >______________________________________________
> >R-help at stat.math.ethz.ch mailing list
> >http://www.stat.math.ethz.ch/mailman/listinfo/r-help
> >
> >
> >
> It seems to me that ls (linear least squares) will be
> enourgh. You have
> to find linear coefficients of three vectors - first consisting of all
> ones , second and third filled with values f(x_1) and f(x_2).
> Answer to more general question will be ?optim (in general).
> You have to
> write a function that have as a result a sum of residuals between y
> values to be fit and modelled values and find minimum of this
> function.
> This is what optim exactly do.

Note that Vadim said he wanted to find f().  You're assuming f() is known.

The model is very strange (to me, at least).  It's not obvious to me that
it's even identifiable.  (Sorry that I don't have anything constructive to