gensmooth {plgraphics}R Documentation

Smooth: wrapper function

Description

Generate fits of a smoothing function for multiple y's. Smooths can be calculated within given groups.

Usage

gensmooth(x, y, band = FALSE, power = 1, resid = "difference",
  weight = NULL, plargs=NULL, ploptions=NULL, ...)

Arguments

x

vector of x values.

y

vector or matrix of y values.

band

logical: Should a band consisting of low and high smooth be calculated? It will only be calculated for the first column of y.

power

y will be raised to power before smoothing. Results will be back-transformed. (Useful for smoothing absolute values for a 'scale plot', for which power=0.5 is recommended.)

resid

Which residuals be calculated? resid=1 or ="difference" means usual residuals; resid=2 or ="ratio" means $y_i/\hat y_i$, which is useful to get scaled y's (regression residuals) according to a smooth fit in the scale plot.

weight

weights of observations, may also be passed by a variable .smoothWeights. in the data set plargs$pldata

plargs, ploptions

result of calling pl.control. The component plargs$pdata may contain smooth.weight and smooth.group, and ploptions specifies smoothPar and smoothIter. All of these may be used by the smoothing function.

...

Further arguments, passed to the smoothing function.

Details

This function is useful for generating the smooths enhancing residual plots. It generates a smooth for a single x variable and multiple y's. It is also used to draw smooths from simulated residuals.

NA's in either x or any column of y cause dropping the observation (equivalent to na.omit).

The smoothing function used to produce the smooth is smoothRegr, which relies loess, by default. This may be changed via ploptions(smooth.function = func) where func is a smoothing function with the same arguments as smoothRegr.

The result of the smoothing function may carry an attribute xtrim. This regulates if the fitted values corresponding to extreme x values will be suppressed when plotting: The number of extreme x values corresponding to ploptions("smooth.xtrim") will be multiplied by this attribute to obtain the number of extreme points suppressed at each end. If the smoothing function is smoothLm, which fits a straight line, then trimming is suppressed since this function returns 0 as the xtrim attribute.

If band is TRUE, a vector of "low" and a vector of "high" smooth values will be calculated for the first column of y in the following way: Residuals are calculated as the diference between the observations and the respective smoothed values hat.$s_i$. Then a smooth is calculated for the square roots of the positive residuals, and the squared fitted values are added to the hat.$s_i$. (The transformation by square roots makes the distribution of the residuals more symmetric.) This defines the “high” smooth values. The construction of the “low” one is analogous. The resulting values of the two are stored in the list component yband, and ybandindex contains the information to which group ("low" or "high") the value belongs.

Value

A list with components:

x

vector of x values, sorted, within levels of group if grouping is actif.

y

matrix with 1 or more columns of corresponding fitted values of the smoothing.

group

grouping factor, sorted, if actif. NULL otherwise.

index

vector of indices of the argument x used for sorting. This is useful to relate the results to the input. Use ysmoothed[value$index,] <- value$y to get values corresponding to input y.

xorig

original x values

ysmorig

corresponding fitted values

residuals

if required by the argument resid, residuals from the smooth fit are provided in the original order, i.e. value$resid[i,j] corresponds to the input value$y[i,j].

If band==TRUE,

yband

vector of low and high smoothed values (for the first column of y)

ybandindex

Indicator if yband is a high value

Note

This function is called by plyx and plmatrix when smooth=T is set, as well as by plregr applied to model objects. It is rarely needed to call it directly.
A band is generated only for the first columnn of y since the others are supposed to be simulated versions of the first one and do not need a band.

Author(s)

Werner A. Stahel, ETH Zurich

See Also

smoothRegr, plsmooth, plsmoothline

Examples

data(d.blast)
r.blast <-
  lm(log10(tremor)~location+log10(distance)+log10(charge), data=d.blast,
    na.action=na.exclude)
r.smooth <- gensmooth( fitted(r.blast), residuals(r.blast))
showd(r.smooth$y)
plot(fitted(r.blast), resid(r.blast), main="Tukey-Anscombe Plot")
abline(h=0)
lines(r.smooth$x,r.smooth$y, col="red")

## grouped data
t.plargs <- list(pdata=data.frame(".smooth.group."=d.blast$location))

r.smx <- gensmooth( d.blast$dist, residuals(r.blast), plargs=t.plargs)

plot(d.blast$dist, residuals(r.blast), main="Residuals against Regressor")
abline(h=0)
plsmoothline(r.smx, d.blast$dist, resid(r.blast), plargs=t.plargs)
## or, without using plsmoothlines:
## for (lg in 1:length(levels(r.smx$group))) {
##   li <- as.numeric(r.smx$group)==lg 
##   lines(r.smx$x[li],r.smx$y[li], col=lg+1, lwd=3)
## }

[Package plgraphics version 1.2 Index]