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

M.Kondrin mkondrin at hppi.troitsk.ru
Wed Feb 19 19:39:53 CET 2003

```>
>
>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
>
>Andy
>
>
>
Yes, I have missed the point.

But if you have a grid in (x_1,x_2) plane and y[i,j] values in the
nodes  (or you can interpolate an irregularly spaced data) then you may
solve your problem with Fourier transform, get fourier coefficients and
find your function (i.e tabulated values) with inverse fourier transform.
Precisely it would look like that
Suppose there is a square grid in plane. Your model is
y[i,j]=a0+a*(z<-cbind(c(f1,f2,f3...fn), c(f1,f2,f3,...fn)....))+t(z),
i,j<N (because function f(x) is a table then may painlessly suppose that
a2 is included in it). Then you convolve row-wise and column-wise y[i,j]
with sin(2*pi*w*i/N), cos(...) (w<N). "Best-fit" Fourier coefficient
(with multiplier "a" for columnwise convolution) is an average of vector
you got after convolution.   a0 may be found then convolving with 1
(i.e. just calculate average). After you got all coefficients "a" may be
found with linear fit and (fi) with inverse fourier transform.
Fourier transform is little tricky when dealing with partial sums (with
truncated series) because a result is not always smooth although in
theory it should be, but why not give it a try.

```