smoothM {regr0}R Documentation

Smooth: wrapper function

Description

Generate fits of a smoothing function for multiple y's (smoothM) and multiple x's (smoothMM). Smooths can be calculated within given groups.

Usage

  smoothM(x, y, weights = NULL, band = FALSE, group = NULL, power = 1,
          resid = FALSE,
          par = 5 * length(x)^log10(1/2), parband = par*2^log10(2),
          iterations = 50)
  smoothMM(x, y, y2 = NULL, weights = NULL, band = FALSE, group = NULL,
          power = 1, resid = FALSE, 
          par = 5 * NROW(x)^log10(1/2), parband = par*2^log10(2),
          iterations = 50)

Arguments

x

vector (smoothM) or matrix (smoothMM) of x values.

y

vector or matrix (smoothM) or matrix with the same dimension as x (smoothMM) of y values.

y2

(smoothMM:) a list or 3-dim array with additional y values. !!!

weights

vector of weights to be used for fitting.

group

NULL or a factor that defines the groups for which the smooth is calculated.

band

logical: Should a band consisting of low and high smooth be calculated?

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

Should 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.

par, parband

argument to be passed to smooth, parband when calculating "band" smooths

iterations

argument passed on to the smoothing function.

Details

These functions are useful for generating the smooths enhancing residual plots. (smoothM) generates a smooth for a single x variable and multiple y's. It is used to draw smooths from simulated residuals. (smoothMM) is the wrapper for multivariate regression. If argument group is specified, the smooths will be calculated within the specified groups.

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

Value

smoothM: 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.

If band==TRUE,

yband

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

ybandind

Indicator if yband is a high value

resid

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].

For smoothMM, the value is a list of such lists, with a component for each column of x.

Author(s)

Werner A. Stahel, ETH Zurich

See Also

smoothRegr

Examples

data(d.blast)
r.blast <-
  regr(log10(tremor)~location+log10(distance)+log10(charge), data=d.blast)
r.smooth <- smoothM( 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
r.smx <- smoothM( d.blast$dist, residuals(r.blast), group=d.blast$location)
plot(d.blast$dist, resid(r.blast), main="Residuals against Regressor")
abline(h=0)
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)

## example with data from stats
data(swiss)
  plot(Fertility~Agriculture, swiss)

  r.sm <- smoothM( swiss$Agriculture, swiss$Fertility )
  lines(r.sm)

  r.sm <- smoothM( swiss$Agriculture, swiss$Fertility, band=TRUE )
  li <- r.sm$ybandind
  lines(r.sm$x[li], r.sm$yband[li], lty=2)
  lines(r.sm$x[!li], r.sm$yband[!li], lty=2)

## example with groups and band
  t.group <- swiss$Catholic>70
  plot(Fertility~Agriculture, swiss, pch=2+t.group, col=2+t.group)
  r.sm <- smoothM( swiss$Agriculture, swiss$Fertility,
                   group=t.group, band=TRUE )

  for (lg in c(FALSE,TRUE)) {
    lig <- which(r.sm$group==lg)
    lines(r.sm$x[lig], r.sm$y[lig], lty=1, col=2+lg)
    li <- r.sm$ybandind[lig]
    ligh <- lig[li]
    ligl <- lig[!li]
    lines(r.sm$x[ligh], r.sm$yband[ligh], lty=2+2*lg, col=2+lg)
    lines(r.sm$x[ligl], r.sm$yband[ligl], lty=2+2*lg, col=2+lg)
  }

}

## use of simresiduals is missing

## multivariate regression
data(d.fossiles)
r.mregr <-
  regr(cbind(sAngle,lLength,rWidth)~SST.Mean+Salinity+lChlorophyll+region+N,
                data=d.fossiles)
r.msmooth <- smoothMM( fitted(r.mregr), residuals(r.mregr), resid=TRUE )
showd(r.msmooth[[2]]$resid)
r.msmresid <- sapply(r.msmooth, function(x) x$resid)
  ## row.names are lost by sapply
showd(r.msmresid)


[Package regr0 version 1.0-5 Index]