smartpred {VGAM} | R Documentation

## Smart Prediction

### Description

Data-dependent parameters in formula terms can cause problems in when predicting. The smartpred package for R and S-PLUS saves data-dependent parameters on the object so that the bug is fixed. The `lm` and `glm` functions have been fixed properly. Note that the VGAM package by T. W. Yee automatically comes with smart prediction.

### Details

R version 1.6.0 introduced a partial fix for the prediction problem because it does not work all the time, e.g., for terms such as `I(poly(x, 3))`, `poly(c(scale(x)), 3)`, `bs(scale(x), 3)`, `scale(scale(x))`. See the examples below. Smart prediction, however, will always work.

The basic idea is that the functions in the formula are now smart, and the modelling functions make use of these smart functions. Smart prediction works in two ways: using `smart.expression`, or using a combination of `put.smart` and `get.smart`.

### Value

Returns the usual object, but with one list/slot component called `smart.prediction` containing any data-dependent parameters.

### Side Effects

The variables `.max.smart`, `.smart.prediction` and `.smart.prediction.counter` are created while the model is being fitted. In R they are created in a new environment called `smartpredenv`. In S-PLUS they are created in frame 1. These variables are deleted after the model has been fitted. However, in R, if there is an error in the model fitting function or the fitting model is killed (e.g., by typing control-C) then these variables will be left in `smartpredenv`. At the beginning of model fitting, these variables are deleted if present in `smartpredenv`.

During prediction, the variables `.smart.prediction` and `.smart.prediction.counter` are reconstructed and read by the smart functions when the model frame is re-evaluated. After prediction, these variables are deleted.

If the modelling function is used with argument `smart=FALSE` (e.g., `vglm(..., smart=FALSE)`) then smart prediction will not be used, and the results should match with the original R or S-PLUS functions.

### WARNING

In S-PLUS, if the `"bigdata"` library is loaded then it is `detach()`'ed. This is done because `scale` cannot be made smart if `"bigdata"` is loaded (it is loaded by default in the Windows version of Splus 8.0, but not in Linux/Unix). The function `search` tells what is currently attached.

In R and S-PLUS the functions `predict.bs` and `predict.ns` are not smart. That is because they operate on objects that contain attributes only and do not have list components or slots. In R the function `predict.poly` is not smart.

### Note

In S-PLUS you will need to load in the smartpred library with the argument `first=T`, e.g., `library(smartpred, lib="./mys8libs", first=T)`. Here, `mys8libs` is the name of a directory of installed packages. To install the smartpred package in Linux/Unix, type something like `Splus8 INSTALL -l ./mys8libs ./smartpred_0.8-2.tar.gz`.

### Author(s)

T. W. Yee and T. J. Hastie

### See Also

`get.smart.prediction`, `get.smart`, `put.smart`, `smart.expression`, `smart.mode.is`, `setup.smart`, `wrapup.smart`. Commonly used data-dependent functions include `scale`, `poly`, `bs`, `ns`. In R, the functions `bs` and `ns` are in the splines package, and this library is automatically loaded in because it contains compiled code that `bs` and `ns` call.

The website http://www.stat.auckland.ac.nz/~yee contains more information such as how to write a smart function, and other technical details.

The functions `vglm`, `vgam`, `rrvglm` and `cqo` in T. W. Yee's VGAM package are examples of modelling functions that employ smart prediction.

### Examples

    # Create some data first
    n = 20
    set.seed(86) # For reproducibility of the random numbers
    x = sort(runif(n))
    y = sort(runif(n))
    ## Not run:
    if(is.R()) library(splines)   # To get ns() in R
    ## End(Not run)

    # This will work for R 1.6.0 and later, but fail for S-PLUS
    fit = lm(y ~ ns(x, df=5))
    ## Not run:
    plot(x, y)
    lines(x, fitted(fit))
    newx = seq(0, 1, len=n)
    points(newx, predict(fit, data.frame(x=newx)), type="b", col=2, err=-1)
    ## End(Not run)

    # The following fails for R 1.6.x and later but works with smart prediction
    ## Not run:
    fit = lm(y ~ ns(scale(x), df=5))
    fit$smart.prediction
    plot(x, y)
    lines(x, fitted(fit))
    newx = seq(0, 1, len=n)
    points(newx, predict(fit, data.frame(x=newx)), type="b", col=2, err=-1)
    ## End(Not run)

    # The following requires the VGAM package to be loaded
    ## Not run:
    library(VGAM)
    fit = vlm(y ~ ns(scale(x), df=5))
    fit@smart.prediction
    plot(x, y)
    lines(x, fitted(fit))
    newx = seq(0, 1, len=n)
    points(newx, predict(fit, data.frame(x=newx)), type="b", col=2, err=-1)
    ## End(Not run)

[Package VGAM version 0.7-9 Index]