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 |
power |
|
resid |
Which residuals be calculated?
|
weight |
weights of observations, may also be passed by a
variable |
plargs , ploptions |
result of calling |
... |
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 |
y |
matrix with 1 or more columns of corresponding fitted values of the smoothing. |
group |
grouping factor, sorted, if actif. |
index |
vector of indices of the argument |
xorig |
original |
ysmorig |
corresponding fitted values |
residuals |
if required by the argument |
If band==TRUE
,
yband |
vector of low and high smoothed values (for the first
column of |
ybandindex |
Indicator if |
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)
## }