You should have the self-extracting zip file. Run this file (if it didn't automatically when downloading); the contents should be extracted to a C:\locfit folder. The locfit executable is C:\locfit\locfit.exe; the example data sets should be automatically installed in the LFData subfolder.
You should have the locfit source archive. Unpack this file (sh locfit.shar) in a new locfit directory. Then
% make locfitat the unix prompt, and pray it works!
Some possible problems and solutions (thanks to users for reporting these; please keep the problem reports coming):
CFLAGS=-g -fno-strict-prototypemay be needed in the Makefile (without this, Locfit will probably compile, but strange core dumps may result when running).
COBJ= main.o cmd.o lfd.o pout.o arith.oby
COBJ= mainnox.o cmd.o lfd.o pout.o arith.oAlso, remove the X11 stuff from the LDFLAGS line. That is, set LDFLAGS=-lm. The default graphics will then be ASCII style.
#define LGAMMA(x) gamma(x)at the top of the local.h may need changing to a legitimate \log(G(x)) function. Check your /usr/include/math.h file for gamma() or lgamma() functions. If neither exists, use
#define LGAMMA(x) lfgamma(x)
% locfit locfit> run install.cmd locfit> exit
The basic data structure in LOCFIT is a matrix, consisting of a number of named variables of the same length. The basic commands for creating data matrices are readfile, readdata and design. readfile reads an ASCII file; readdata reads previously saved locfit binary datafiles, and design generates variables using random number generators and other features. An example is:
locfit> readfile file=ethanol.dat NOx C E
locfit> design E2=E*E g=rnorm()
locfit> prdata
NOx C E E2 g
3.741000 12.000000 0.907000 0.822649 -1.320317
2.295000 12.000000 0.761000 0.579121 -2.436422
1.498000 12.000000 1.108000 1.227664 -0.863074
...
locfit> summdata
Dataset contains 5 variables and 88 observations.
Variable names are:
NOx
C
E
E2
g
Here, we have read a file ethanol.dat (the file= can
be omitted, as long as the filename is given as the first argument),
creating three variables: NOx, C and E.
The design command then generates two new variables,
E2=E*E, and g=rnorm(); the latter using a standard
normal random number generator. We can then print the dataset
(with prdata), or summarize the dataset (with summdata).
Another optional argument to readfile is arith. If arith=T, then each item in the input file is parsed by LOCFIT's arithmetic interpreter before being assigned to the data matrix. If the input file is numeric, this has little effect other than to slow things down slightly. One use of the arith argument is processing categorical input; for example
locfit> versicolor=0 virginica=1 locfit> iris=readfile iris.dat arith=T species seplen sepwid petlen petwidHere, the input file contains a categorical variable, species that can't be handled by LOCFIT. By first assigning numeric values to the two species, and then specifying arith=T, the species variable will be created with 0-1 values.
The design command can also be used to start a new dataset, simply by giving a sample size n argument:
locfit> design n=50 x=runif() y=rnorm()
Variables can have names up to 9 characters long, consisting of almost any characters. It's best to stick to the alphanumeric characters -- naming a variable x+y would be quite legitimate, but might result in considerable confusion! Also, you can't create a variable called n in a design command.
For more details of legitimate expressions for generating variables in the design command, see `Arithmetic Expressions' below.
Permanent datasets are created using savedata and readdata.
locfit> readfile ethanol.dat NOx C E locfit> savedata ethanolreads the ethanol.dat file, and saves it (in a binary format) in the file LFData/ethanol.lfd. In the next LOCFIT session, restore the dataset with the command
locfit> readdata ethanol
In fact, it's rarely necessary to directly use the savedata and readdata commands. Datasets can also be saved by naming the readfile and design commands; for example,
locfit> ethanol=readfile ethanol.dat NOx C Eautomatically saves the ethanol dataset. Also, many commands have an argument to specify a dataset name as an argument; for example,
locfit> summdata data=ethanolis equivalent to
locfit> readdata ethanol locfit> summdata
The main purpose of LOCFIT is local fitting, using the locfit command. The simplest use is
locfit> locfit NOx~E data=ethanolwhere the first argument NOx~E specifies a model formula, and data=ethanol provides a dataset (if omitted, the current dataset is assumed). The model formula is similar to that in the S version: NOx~E+C specifies a model with two predictors; ~E specifies a model with no response; e.g. for density estimation. One difference is that variables cannot be created in the formula. Thus log(NOx)~E is not valid; instead, the transformed variable must be created separately. Thus use
locfit> readdata ethanol locfit> design logNOx=log(NOx) locfit> locfit logNOx~E
A closely related command is relocfit. This differs from locfit only in that default values for parameters are not set; thus, values from the previous fit are retained. Thus
locfit> readdata ethanol locfit> locfit NOx~E alpha=0.7 locfit> plotfit data=T locfit> # decide smoothing parameter is too large, try smaller value locfit> relocfit alpha=0.4 locfit> plotfit data=T
The locfit command has numerous optional arguments, mostly in 1-1 correspondence with the same arguments in the S version:
Like data matrices, the fits can be saved in binary format. The commands are savefit and readfit, to save and read the fit respectively. For example:
locfit> readdata ethanol locfit> locfit NOx~E alpha=0.5 locfit> savefit ethanolcreates a file LFPlot/ethanol.fit. It's rarely neccessary to call readfit and savefit explicitly; the above commands could be accomplished through the single line
locfit> ethanol=locfit NOx~E alpha=0.5 data=ethanolLikewise, most of the commands in the next section have an optional fit= argument, that can be used to read a fit.
The current dataset should have variables of the same names as the dataset used in the original fit. Usually, of course, it should still be the same dataset, but there is no check to see whether this is the case.
Arithmetric expressions can be typed at the locfit prompt:
locfit> c1=3+4
7
locfit> c1*c1 sin(c1)
49 0.656987
In the first line, 3+4 is computed, and the result assigned to
the constant c1. In the second, we compute c1*c1
and sin(c1), and leave the results unassigned.
The arithmetic interpreter is used for many numeric arguments to other commands. For example, we could use log(c1)/10 as a smoothing parameter:
> locfit NOx~E data=ethanol alpha=0,log(c1)/10
The arithmetic interpreter is also used to generate variables in design commands.
Constants can be named almost anything, although it's best to avoid arithmetic symbols and the like. Also, don't duplicate names from the current dataset.
There are a number of predefined constants, which point to components of the locfit structure: alpha; h; pen (the three components of the smoothing parameter); like (the likelihood at the fitted values); infl, vari (fitted degrees of freedom by the \tr(L) and \tr(LTL) definitions) and resv (estimate of the residual variance). You can assign to these constants like any other; obviously this should only be done with extreme care. More usefully, these constants can be used in computations; for example,
locfit> locfit NOx~E data=ethanol alpha=0.7
locfit> alpha vari gcv=88*(-2*like)/(88-infl)^2
0.7 4.76494 0.151757
Here, we fit a local quadratic model to the ethanol dataset.
Then, the smoothing paramter, fitted degrees of freedom and
GCV statistic are printed.
Recognized components of arithmetic expressions include:
In the design command, these can also be used:
For some users, the interface provided may not be satisfactory. A `custom' interface can access the LOCFIT code at several points. You will always want to include the header file local.h. Also, you will need to link with the object file locfit.o, and all the files listed in the OBJS line of the Makefile.
A simple interface to locfit for a one dimensional local regression is
#include "local.h" struct design des; struct tree lf;Here, x and y are predictor and response variables, of length n. dum0 and dum1 should also be vectors of length n, filled with 0's and 1's respectively. These are used as `defaults' for the weights, censoring and base variables.void mylfcall(x,y,n) double *x, *y; int n; { lf.x[0] = x; lf.y = y; lf.w = lf.c = lf.base = NULL; lf.mi[MDIM] = 1; lf.mi[MN] = n; fitdefault(&lf); evaluator(&des,&lf,procv); }
There are two parameter arrays on the tree structure: The integer array lf.mi and double precision array lf.dp. These store the smoothing parameters and numerous other options (the full list is given later). In the above example, we set lf.mi[MDIM] and lf.mi[MN] to be the dimension and sample size for the regression problem. Other components are set to default values by the routine fitdefault(&lf).
The final line in the above example, evaluator(&des,&lf,procv) is the call to locfit. The first two arguments, des and lf, are the design structure (used as work space) and the locfit tree structure. The third argument, procv is a vertex processing function; this will be described more later.
To set the data pointers:
The tree structure contains two parameter vectors texttt{lf.mi} and lf.dp for integer and double parameters respectively. The lengths should be allocated to LENM and LEND respectively. The two components lf.mi[MN] and lf.mi[MDIM] must always be set; other components can be set to default values by fitdefault(&lf). Other components that may want setting are
The third argument to evaluator, vfun, is a vertex processing function; it is called sequentially for each fitting point. The vertex processing function should have three arguments: the design structure des; the tree structure lf and an integer v, representing the index of the fitting point. Two vertex processing functions are provided with locfit: procv and procvraw. The second computes the bandwidth and fits, while the first also computes variance estimates. For specialized applications, custom vertex processing functions could be written; typically these would be a call to one of the existing functions, accompanied by some additional processing. An example is in the file simul.c; see the procvscb and scbmax functions in that file.
When evaluator returns, components lf.nv will be set to the number of evaluation points; lf.xev stores the evaluation points; tr.coef contains the fitted values and all coefficients of the local polynomials, tr.nlx the standard errors.
To interpolate the fit to additional points, use the intp(), intv(), intg() or intd() functions. e.g.
y = intp(&lf,x,PCOEF,NULL,0);to interpolate at a single point, pointed to by x. PCOEF says to interpolate the coefficients (other choices are PNLX, PT0 and PBAND); the last two arguments can be used for derivative estimation.
intv(&lf,x,f,n,PCOEF,NULL,0);to interpolate to a vector of n points; x[0]...x[d-1] point to the first component of each variable, and f is a vector of length n to contain the result.
intg(&lf,x,f,mg,PCOEF,NULL,0);interpolates on a grid -- x[0]...x[d-1] point to the grid margins and mg is the length of the margins.
intd(&lf,lf.x,f,mg,PCOEF,NULL,0);interpolates to data points -- like intv, but no checks when lf.mi[MEV]=EDATA.