LOCFIT


Library liblfev

This needs updating for Locfit 2.0

The liblfev library is a front-end for the locfit() function. While locfit() performs the local fitting one point at a time, the functions in liblfev() generate structures of points (e.g. a grid) at which the fitting is performed.

The structures so computed can then be interpolated to compute approximate local fits at additional points. This has the advantage of being much faster than direct fitting.

Before proceeding, you should have downloaded and installed the LOCFIT libraries (see the C-LOCFIT page).

To link with your program, use

cc -o myprog myprog.c -llfev -llocf -ltube -lmut -lm

Fitting on a Design Structure

The main function to call is
startlf(&des,&lf,procv,1);
The first two arguments are LOCFIT design and fit structures, defined by including the header file "local.h", with types design and lfit.

The third argument is a `vertex processing function', which controls what is computed at each fit point. The most useful choices are procvraw (compute the fit only) and procv (compute fit and variances).

If the final argument is 0, a parametric fit is first computed, and the local fit is computed on top of that. This can improve accuracy of LOCFIT's interpolation methods, at the expense of making results more difficult to interpret.

Example: Computing a Local Fit

#include "local.h"

int lf_error;

  lfit lf;
  design des;

void calllf(x,y,n)
double *x, *y;
int n;
{ lfdata *lfd;
  fitpt *fp;
  int i;

  lfit_init(&lf);

  lfd = &(lf.lfd);
  lfd->x[0] = x;
  lfd->y = y;
  lfd->n = n;
  lfd->d = 1;

  startlf(&des,&lf,procv,1);

  fp = &lf.fp;
  for (i=0; i<fp->nv; i++)
    printf("%8.5f %8.5f\n",evptx(fp,i,0),fp->coef[i]);
}

int main()
{ double x[10], y[10];

  x[0] = 0; x[1] = 1; x[2] = 2; x[3] = 3; x[4] = 4;
  x[5] = 5; x[6] = 6; x[7] = 7; x[8] = 8; x[9] = 9;

  y[0] = 0.3692449; y[1] = 0.8194270;
  y[2] = 1.6363139; y[3] =-0.9969944;
  y[4] = 0.5359200; y[5] = 1.8642622;
  y[6] = 0.3568127; y[7] = 0.4746753;
  y[8] =-2.0038246; y[9] = 1.6636109;

  calllf(x,y,10);
}
The calllf() function above takes data vectors x and y as arguments. First, it initializes the lfit structure. The lfit structure has several components. One of these is lfdata, and now the data must be attached to this.

Next, the LOCFIT function startlf() is called. The results are stored on the fitpt structure The output gives the five fitting points, and corresponding fitted values, selected by LOCFIT. A table of computed values is then produced; the output is

catherine$ ./lfevt
 0.00000  0.59785
 9.00000  0.89170
 4.50000  0.76557
 2.25000  0.40143
 6.75000 -0.08561

Evaluation Structures

In the example above, LOCFIT has selected fitting points according to an adaptive tree structure. Roughly, this is a binary tree that partitions the predictor space sufficient refinement is achieved.

This can be changed by setting the evaluation structure before calling startlf(). The evaluation structure is set on a component lf.evs, of the lfit, and has type evstruc.

Insert the following code before the call to startlf():

ev(&lf.evs) = EGRID;
mg(&lf.evs)[0] = 6;
Now, run the program. The output should be
catherine$ ./lfevt
 0.00000  0.59785
 1.80000  0.51080
 3.60000  0.24653
 5.40000  1.30902
 7.20000 -0.29455
 9.00000  0.89170
This has evaluated on a grid of six equally spaced points.

If you set ev(&lf.evs) = EDATA, then the fit is computed at each data point.

Interpolating the Fit

The computed local fit can be interpolated to produce a fitted value at other points in the predictor (x) space. This has the advantage of being faster than direct fitting, especially for large multi-dimensional datasets.

The basic interpolation function is

double dointpoint(lf,x,what,ev,j)
lfit *lf;
double *x;
int what, ev, j;
lf is the local fit structure; x is the point to interpolate at, ev=ev(&lf->evs) is the evaluation structure type, j is usually ignored.

what says what to interpolate. what=PCOEF interpolates the fit itself.

(To be continued)