#ifDLLCALL
/*
**  mloess.src  -  multivariate loess
**
**  ported to GAUSS by Ron Schoenberg, rons@u.washington.edu, August 1999
**  from original C code developed by William S. Cleveland, Eric Grosse,
**  and Min-Jen Shyu.  
**
**  This code is provided without guarantee for public noncommercial use.
**
**-------------------**------------------**-------------------**-----------**
**-------------------**------------------**-------------------**-----------**
**
**> mloess
**
**  Purpose:  loess regression with multiple independent variables
**
**  Format:   { yhat,ys,xs } = mloess(y,x)
**
**  Input:    y    Nx1 vector, dependent variable.
**
**            x    NxK vector, independent variable.
**
**  Output:   yhat    Nx1 vector, predicted depvar given indvars.
**
**            ys      numEvalx1 vector, ordinate values given abscissae
**                    values in xs.
**
**            xs      numEvalx1 vector, equally spaced abscissae values.
**
**
**   Globals:
**
**   _loess_Span -- scalar, degree of smoothing, must be greater than
**                 2 / N.  Default = .67777.
**
**   _loess_NumEval -- scalar, number of points in ys and xs.
**                     Default = 50.
**
**   _loess_Degree -- scalar, if 2, quadratic fit, otherwise linear.
**                    Default = 1.
**
**   _loess_WgtType -- scalar, type of weights.  Default weight type
**                 is Gaussian. If set to 1, robust, symmetric weights
**                 are used.
**
**   __ouput -- scalar, if 1 (default), iteration information and results
**                 are printed, otherwise nothing is printed.
**
*/

#include gauss.ext
#include loess.ext


proc(3) = mloess(y,x);

   local yy,yhat,xx,i,lo,hi,rows_yy,rows_y,cols_x,span,deg,fam,err;
   rows_y = rows(y);
   cols_x = cols(x);
   rows_yy = _loess_NumEval;
   err = 0;

   if rows(x) /= 1 and rows(x) /= rows_y and rows_y /= 1;
       if not trapchk(1);
           errorlog "mloess: matrices not conformable";
           end;
       else;
           retp(error(0));
       endif;
   endif;

   if cols(y) /= 1;
       if not trapchk(1);
           errorlog "mloess: dependent variable has more than one column";
           end;
       else;
           retp(error(0));
       endif;
   endif;

   yy = zeros(rows_yy,1);
   xx = zeros(rows_yy,cols_x);
   lo = minc(x);
   hi = maxc(x);
   i = 1;
   do until i > cols_x;
       xx[.,i] = seqa(lo[i],(hi[i]-lo[i])/(rows_yy-1),rows_yy);
       i = i + 1;
   endo;

   yhat = zeros(rows_y,1);

   span = _loess_Span;
   deg = _loess_degree;
   fam = _loess_WgtType;

   dllcall mloessdll(x,y,rows_y,cols_x,yhat,yy,xx,rows_yy,span,deg,fam,err);

   if err /= 0;
       retp(error(0),error(0),error(0));
   else;
       retp(yhat,yy,xx);
   endif;

endp;


#endif

