lfbas                 package:locfit                 R Documentation

_U_s_e_r-_s_p_e_c_i_f_i_e_d _b_a_s_i_s _f_u_n_c_t_i_o_n_s _f_o_r _L_o_c_f_i_t.

_D_e_s_c_r_i_p_t_i_o_n:

     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.

_U_s_a_g_e:

     locfit(..., basis)

_S_e_e _A_l_s_o:

     'locfit', 'locfit.raw'

_E_x_a_m_p_l_e_s:

     ## Specify a bivariate linear with interaction basis.
     data(ethanol, package="locfit")
     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, package="locfit")
     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))

