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 -llocf -lmut -lm
locfit(&lfd,&des,&sp,0,1,0);The first three arguments are LOCFIT data, design and smoothing parameter structures respectively. These are defined by including the header file "local.h", and have types lfdata, design and smpar. These structures need to be set up with your data and smoothing parameters before the call to locfit().
The remaining three arguments are logical flags named noit, nb, and cv. If noit=1, only a single iteration of local likelihood is performed, instead of iterating to convergence. If nb=1, neigborhood weights are computed (as opposed to being computed externally to LOCFIT). If cv=1, the variance-covariance matrix of the local coefficients is computed, in addition to the local fit.
#include "local.h"
int lf_error;
int main()
{ lfdata lfd;
smpar sp;
design des;
double x[10], y[10], xev;
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;
lfdata_init(&lfd);
lfd.x[0] = x;
lfd.y = y;
lfd.n = 10;
lfd.d = 1;
smpar_init(&sp,&lfd);
des_init(&des,lfd.n,npar(&sp));
xev = 4.5;
des.xev = &xev;
locfit(&lfd,&des,&sp,0,1,0);
printf("%8.5f %8.5f %8.5f\n", des.cf[0],des.cf[1],des.cf[2]);
}
Initially, data arrays x and y are set up.
The lfdata structure is initialized with the call to
lfdata_init().
The x and y arrays, along with the data length
and dimension (number of x variables) are then assigned to
appropriate places on this structure.
Next, the functions smpar_init() and deschk() are called to set default values and assign space on the smoothing parameters and design structures.
Finally, the evaluation point, des.xev must be set. Then, the call to locfit() evaluates the fit at a single point. The local coefficients are stored in the des.cf vector. The first component, des.cf[0], is the smoothed estimate; des.cf[1] is the local slope and so forth.
Usually, you'll want to fit at more than just a single point. This is achieved through multiple calls to locfit(), setting the des.xev value before each call, and extracting the desired coefficients after each call. Calls to the initialization functions and deschk() need not be repeated. Save the above code, let's say in lftest.c. Then run the following commands:
catherine$ cc -o lftest lftest.c -llocf -lmut -lm catherine$ ./lftest 0.76557 0.20775 -0.19036The output is the estimated coefficients of a local quadratic expansion around x=4.5: the function value, and its first and second derivatives.
The lfdata_init() also initializes scales and styles for each variable. The scales control the relative amounts of smoothing in each dimension, and by default, lfd.sca[i]=1 for all i. Changing these values gives elliptical smoothing neighborhoods.
In one dimension, if the data vector lfd.x[0] is in ascending order, set the flag lfd.ord=1. This may speed up the computations.