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

M.Kondrin mkondrin at hppi.troitsk.ru
Wed Feb 19 09:42:05 CET 2003

```Vadim Ogranovich wrote:

>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?
>
>Thanks, Vadim
>
>--------------------------------------------------
>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
>
>
>
Vadim
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.

```

More information about the R-help mailing list