Learn R Programming

locfit (version 1.1-3)

lfbas: User-specified basis functions for Locfit.

Description

By default, Locfit uses a polynomial basis as the fitting functions. An alternative set of basis functions can be specified through the basis argument to locfit.raw. lfbas is a wrapper function used internally in Locfit's processing.

To use the basis argument, you must write a function f(x,t) to evaluate the basis function at a fitting point t and data point(s) x. See below for an example. Note that the basis function will be called with multiple data points simultaneously, so should assume x is a matrix with d columns, where d is the number of independent variables. The fitting point t will always be a single point, in a vector of length d.

The basis function should return a matrix, with each column being the evaluation of one fitting function.

To ensure that the returned fit estimates the mean function, the first component of the basis should be 1; the remaining components should be 0 when x=t. To help ensure Locfit's interpolation routines are meaningful, the next d components should represent partial derivatives at x=t. Specifically, df(x,t)/dx[i], evaluated at x=t, should be the unit vector with 1 in the (i+1)st position.

Violation of these rules can be useful, if functionals other than the mean are of interest. But this will require extreme care.

Specifying a user basis results in extensive use of the call_S function, which may be slow. Speed may also be significantly affected by different ways of writing the basis.

Arguments

See Also

locfit, locfit.raw

Examples

Run this code
# Specify a bivariate linear with interaction basis.
data(ethanol)
my.basis <- function(x,t)
{
  u1 <- x[, 1] - t[1]
  u2 <- x[, 2] - t[2]
  cbind(1, u1, u2, u1 * u2)
}
fit <- locfit(NOx~E+C, data=ethanol, scale=0, basis=my.basis)
# With this basis, Locfit's standard interpolation and plot methods
# should be reasonable.
plot(fit,get.data=TRUE)

# Estimation of change points. This provides an alternative to using
# left() and right(), and can easily be modified to detecting
# a change in slopes or other parameters. Note that the first
# component is the indicator of x>t, so the coefficient estimates
# the size of the change, assuming the change occurs at t.
data(penny)
my.basis <- function(x,t) cbind(x>t,1,x-t)
xev <- (1945:1988) + 0.5
fit <- locfit(thickness~year, data=penny, alpha=c(0,10), ev=xev, basis=my.basis)
# The plot will show peaks where change points are most likely.
# in S4, S-Plus 5 etc,
# plot(preplot(fit,where="fitp")^2, type="b") is an alternative.
plot(xev, predict(fit,where="fitp")^2, type="b")

# Estimate the mean function using local linear regression, with
# discontinuities at 1958.5 and 1974.5.
# The basis functions must consist of the constant 1, the linear term
# x-t, and indicator functions for two of the three sections.
# Note the care taken to ensure my.basis(t,t) = c(1,0,0,0) for all t.
my.basis <- function(x,t)
{ ret <- NULL
  if (t<1958.5) ret <- cbind(1, x>=1958.5, x>1974.5, x-t)
  if (t>1974.5) ret <- cbind(1, x<=1974.5, x<1958.5, x-t)
  if (is.null(ret))
    ret <- cbind(1, x<1958.5, x>1974.5, x-t)
  ret
}
fit <- locfit(thickness~year, data=penny, alpha=c(0,10), ev=xev, basis=my.basis)
plot(preplot(fit,where="fitp", get.data=TRUE))

Run the code above in your browser using DataLab