transace  R Documentation 
transace
is ace
packaged for easily automatically
transforming all variables in a formula without a lefthand side.
transace
is a fast
oneiteration version of transcan
without imputation of
NA
s. The ggplot
method makes nice transformation plots
using ggplot2
. Binary variables are automatically kept linear,
and character or factor variables are automatically treated as categorical.
areg.boot
uses areg
or
avas
to fit additive regression models allowing
all variables in the model (including the lefthandside) to be
transformed, with transformations chosen so as to optimize certain
criteria. The default method uses areg
whose goal it is
to maximize R^2
. method="avas"
explicity tries to
transform the response variable so as to stabilize the variance of the
residuals. Allvariablestransformed models tend to inflate R^2
and it can be difficult to get confidence limits for each
transformation. areg.boot
solves both of these problems using
the bootstrap. As with the validate
function in the
rms library, the Efron bootstrap is used to estimate the
optimism in the apparent R^2
, and this optimism is subtracted
from the apparent R^2
to optain a biascorrected R^2
.
This is done however on the transformed response variable scale.
Tests with 3 predictors show that the avas
and
ace
estimates are unstable unless the sample size
exceeds 350. Apparent R^2
with low sample sizes can be very
inflated, and bootstrap estimates of R^2
can be even more
unstable in such cases, resulting in optimismcorrected R^2
that
are much lower even than the actual R^2
. The situation can be
improved a little by restricting predictor transformations to be
monotonic. On the other hand, the areg
approach allows one to
control overfitting by specifying the number of knots to use for each
continuous variable in a restricted cubic spline function.
For method="avas"
the response transformation is restricted to
be monotonic. You can specify restrictions for transformations of
predictors (and linearity for the response). When the first argument
is a formula, the function automatically determines which variables
are categorical (i.e., factor
, category
, or character
vectors). Specify linear transformations by enclosing variables by
the identify function (I()
), and specify monotonicity by using
monotone(variable)
. Monotonicity restrictions are not
allowed with method="areg"
.
The summary
method for areg.boot
computes
bootstrap estimates of standard errors of differences in predicted
responses (usually on the original scale) for selected levels of each
predictor against the lowest level of the predictor. The smearing
estimator (see below) can be used here to estimate differences in
predicted means, medians, or many other statistics. By default,
quartiles are used for continuous predictors and all levels are used
for categorical ones. See Details below. There is also a
plot
method for plotting transformation estimates,
transformations for individual bootstrap resamples, and pointwise
confidence limits for transformations. Unless you already have a
par(mfrow=)
in effect with more than one row or column,
plot
will try to fit the plots on one page. A
predict
method computes predicted values on the original
or transformed response scale, or a matrix of transformed
predictors. There is a Function
method for producing a
list of R functions that perform the final fitted transformations.
There is also a print
method for areg.boot
objects.
When estimated means (or medians or other statistical parameters) are
requested for models fitted with areg.boot
(by
summary.areg.boot
or predict.areg.boot
), the
“smearing” estimator of Duan (1983) is used. Here we
estimate the mean of the untransformed response by computing the
arithmetic mean of ginverse(lp + residuals)
,
where ginverse is the inverse of the nonparametric
transformation of the response (obtained by reverse linear
interpolation), lp is the linear predictor for an individual
observation on the transformed scale, and residuals is the
entire vector of residuals estimated from the fitted model, on the
transformed scales (n residuals for n original observations). The
smearingEst
function computes the general smearing estimate.
For efficiency smearingEst
recognizes that quantiles are
transformationpreserving, i.e., when one wishes to estimate a
quantile of the untransformed distribution one just needs to compute
the inverse transformation of the transformed estimate after the
chosen quantile of the vector of residuals is added to it. When the
median is desired, the estimate is
ginverse(lp + \mbox{median}(residuals))
.
See the last example for how smearingEst
can be used outside of
areg.boot
.
Mean
is a generic function that returns an R function to
compute the estimate of the mean of a variable. Its input is
typically some kind of model fit object. Likewise, Quantile
is
a generic quantile functionproducing function. Mean.areg.boot
and Quantile.areg.boot
create functions of a vector of linear
predictors that transform them into the smearing estimates of the mean
or quantile of the response variable,
respectively. Quantile.areg.boot
produces exactly the same
value as predict.areg.boot
or smearingEst
. Mean
approximates the mapping of linear predictors to means over an evenly
spaced grid of by default 200 points. Linear interpolation is used
between these points. This approximate method is much faster than the
full smearing estimator once Mean
creates the function. These
functions are especially useful in nomogram
(see the
example on hypothetical data).
transace(formula, trim=0.01, data=environment(formula))
## S3 method for class 'transace'
print(x, ...)
## S3 method for class 'transace'
ggplot(data, mapping, ..., environment, nrow=NULL)
areg.boot(x, data, weights, subset, na.action=na.delete,
B=100, method=c("areg","avas"), nk=4, evaluation=100, valrsq=TRUE,
probs=c(.25,.5,.75), tolerance=NULL)
## S3 method for class 'areg.boot'
print(x, ...)
## S3 method for class 'areg.boot'
plot(x, ylim, boot=TRUE, col.boot=2, lwd.boot=.15,
conf.int=.95, ...)
smearingEst(transEst, inverseTrans, res,
statistic=c('median','quantile','mean','fitted','lp'),
q)
## S3 method for class 'areg.boot'
summary(object, conf.int=.95, values, adj.to,
statistic='median', q, ...)
## S3 method for class 'summary.areg.boot'
print(x, ...)
## S3 method for class 'areg.boot'
predict(object, newdata,
statistic=c("lp", "median",
"quantile", "mean", "fitted", "terms"),
q=NULL, ...)
## S3 method for class 'areg.boot'
Function(object, type=c('list','individual'),
ytype=c('transformed','inverse'),
prefix='.', suffix='', pos=1, ...)
Mean(object, ...)
Quantile(object, ...)
## S3 method for class 'areg.boot'
Mean(object, evaluation=200, ...)
## S3 method for class 'areg.boot'
Quantile(object, q=.5, ...)
formula 
a formula without a lefthandside variable. Variables
may be enclosed in 
x 
for 
object 
an object created by 
transEst 
a vector of transformed values. In lognormal regression these could be predicted log(Y) for example. 
inverseTrans 
a function specifying the inverse transformation needed to change

trim 
quantile to which to trim original and transformed values
for continuous variables for purposes of plotting the
transformations with 
nrow 
the number of rows to graph for 
data 
data frame to use if 
environment , mapping 
ignored 
weights 
a numeric vector of observation weights. By default, all observations are weighted equally. 
subset 
an expression to subset data if 
na.action 
a function specifying how to handle 
B 
number of bootstrap samples (default=100) 
method 

nk 
number of knots for continuous variables not restricted to be
linear. Default is 4. One or two is not allowed. 
evaluation 
number of equallyspaced points at which to evaluate (and save) the
nonparametric transformations derived by 
valrsq 
set to 
probs 
vector probabilities denoting the quantiles of continuous predictors to use in estimating effects of those predictors 
tolerance 
singularity criterion; list source code for the

res 
a vector of residuals from the transformed model. Not required when

statistic 
statistic to estimate with the smearing estimator. For

q 
a single quantile of the original response scale to estimate, when

ylim 
2vector of yaxis limits 
boot 
set to 
col.boot 
color for bootstrapped transformations 
lwd.boot 
line width for bootstrapped transformations 
conf.int 
confidence level (01) for pointwise bootstrap confidence limits and
for estimated effects of predictors in 
values 
a list of vectors of settings of the predictors, for predictors for
which you want to overide settings determined from 
adj.to 
a named vector of adjustment constants, for setting all other
predictors when examining the effect of a single predictor in

newdata 
a data frame or list containing the same number of values of all of
the predictors used in the fit. For 
type 
specifies how 
ytype 
By default the first function created by 
prefix 
character string defining the prefix for function names created when

suffix 
character string defining the suffix for the function names 
pos 
See 
... 
arguments passed to other functions. Ignored for

As transace
only does one iteration over the predictors, it may
not find optimal transformations and it will be dependent on the order
of the predictors in x
.
ace
and avas
standardize transformed variables to have
mean zero and variance one for each bootstrap sample, so if a
predictor is not important it will still consistently have a positive
regression coefficient. Therefore using the bootstrap to estimate
standard errors of the additive least squares regression coefficients
would not help in drawing inferences about the importance of the
predictors. To do this, summary.areg.boot
computes estimates
of, e.g., the interquartile range effects of predictors in predicting
the response variable (after untransforming it). As an example, at
each bootstrap repetition the estimated transformed value of one of
the predictors is computed at the lower quartile, median, and upper
quartile of the raw value of the predictor. These transformed x
values are then multipled by the least squares estimate of the partial
regression coefficient for that transformed predictor in predicting
transformed y. Then these weighted transformed x values have the
weighted transformed x value corresponding to the lower quartile
subtracted from them, to estimate an x effect accounting for
nonlinearity. The last difference computed is then the standardized
effect of raising x from its lowest to its highest quartile. Before
computing differences, predicted values are backtransformed to be on
the original y scale in a way depending on statistic
and
q
. The sample standard deviation of these effects (differences)
is taken over the bootstrap samples, and this is used to compute
approximate confidence intervals for effects andapproximate Pvalues,
both assuming normality.
predict
does not reinsert NA
s corresponding to
observations that were dropped before the fit, when newdata
is
omitted.
statistic="fitted"
estimates the same quantity as
statistic="median"
if the residuals on the transformed response
have a symmetric distribution. The two provide identical estimates
when the sample median of the residuals is exactly zero. The sample
mean of the residuals is constrained to be exactly zero although this
does not simplify anything.
transace
returns a list of class transace
containing
these elements: n
(number of nonmissing observations used), transformed
(a matrix containing transformed values), rsq
(vector of R^2
with which each
variable can be predicted from the others), omitted
(row
numbers of data that were deleted due to NA
s),
trantab
(compact transformation lookups), levels
(original levels of character and factor varibles if the input was a
data frame), trim
(value of trim
passed to
transace
), limits
(the limits for plotting raw and
transformed variables, computed from trim
), and type
(a
vector of transformation types used for the variables).
areg.boot
returns a list of class ‘areg.boot’ containing
many elements, including (if valrsq
is TRUE
)
rsquare.app
and rsquare.val
. summary.areg.boot
returns a list of class ‘summary.areg.boot’ containing a matrix
of results for each predictor and a vector of adjustto settings. It
also contains the call and a ‘label’ for the statistic that was
computed. A print
method for these objects handles the
printing. predict.areg.boot
returns a vector unless
statistic="terms"
, in which case it returns a
matrix. Function.areg.boot
returns by default a list of
functions whose argument is one of the variables (on the original
scale) and whose returned values are the corresponding transformed
values. The names of the list of functions correspond to the names of
the original variables. When type="individual"
,
Function.areg.boot
invisibly returns the vector of names of the
created function objects. Mean.areg.boot
and
Quantile.areg.boot
also return functions.
smearingEst
returns a vector of estimates of distribution
parameters of class ‘labelled’ so that print.labelled
wil
print a label documenting the estimate that was used (see
label
). This label can be retrieved for other purposes
by using e.g. label(obj)
, where obj was the vector
returned by smearingEst
.
Frank Harrell
Department of Biostatistics
Vanderbilt University School of Medicine
fh@fharrell.com
Harrell FE, Lee KL, Mark DB (1996): Stat in Med 15:361–387.
Duan N (1983): Smearing estimate: A nonparametric retransformation method. JASA 78:605–610.
Wang N, Ruppert D (1995): Nonparametric estimation of the transformation in the transformbothsides regression model. JASA 90:522–534.
See avas
, ace
for primary references.
avas
, ace
,
ols
, validate
,
predab.resample
, label
,
nomogram
# xtrans < transace(~ monotone(age) + sex + blood.pressure + categorical(race.code))
# print(xtrans) # show R^2s and a few other things
# ggplot(xtrans) # show transformations
# Generate random data from the model y = exp(x1 + epsilon/3) where
# x1 and epsilon are Gaussian(0,1)
set.seed(171) # to be able to reproduce example
x1 < rnorm(200)
x2 < runif(200) # a variable that is really unrelated to y]
x3 < factor(sample(c('cat','dog','cow'), 200,TRUE)) # also unrelated to y
y < exp(x1 + rnorm(200)/3)
f < areg.boot(y ~ x1 + x2 + x3, B=40)
f
plot(f)
# Note that the fitted transformation of y is very nearly log(y)
# (the appropriate one), the transformation of x1 is nearly linear,
# and the transformations of x2 and x3 are essentially flat
# (specifying monotone(x2) if method='avas' would have resulted
# in a smaller confidence band for x2)
summary(f)
# use summary(f, values=list(x2=c(.2,.5,.8))) for example if you
# want to use nice round values for judging effects
# Plot Y hat vs. Y (this doesn't work if there were NAs)
plot(fitted(f), y) # or: plot(predict(f,statistic='fitted'), y)
# Show fit of model by varying x1 on the xaxis and creating separate
# panels for x2 and x3. For x2 using only a few discrete values
newdat < expand.grid(x1=seq(2,2,length=100),x2=c(.25,.75),
x3=c('cat','dog','cow'))
yhat < predict(f, newdat, statistic='fitted')
# statistic='mean' to get estimated mean rather than simple inverse trans.
xYplot(yhat ~ x1  x2, groups=x3, type='l', data=newdat)
## Not run:
# Another example, on hypothetical data
f < areg.boot(response ~ I(age) + monotone(blood.pressure) + race)
# use I(response) to not transform the response variable
plot(f, conf.int=.9)
# Check distribution of residuals
plot(fitted(f), resid(f))
qqnorm(resid(f))
# Refit this model using ols so that we can draw a nomogram of it.
# The nomogram will show the linear predictor, median, mean.
# The last two are smearing estimators.
Function(f, type='individual') # create transformation functions
f.ols < ols(.response(response) ~ age +
.blood.pressure(blood.pressure) + .race(race))
# Note: This model is almost exactly the same as f but there
# will be very small differences due to interpolation of
# transformations
meanr < Mean(f) # create function of lp computing mean response
medr < Quantile(f) # default quantile is .5
nomogram(f.ols, fun=list(Mean=meanr,Median=medr))
# Create S functions that will do the transformations
# This is a table lookup with linear interpolation
g < Function(f)
plot(blood.pressure, g$blood.pressure(blood.pressure))
# produces the central curve in the last plot done by plot(f)
## End(Not run)
# Another simulated example, where y has a lognormal distribution
# with mean x and variance 1. Untransformed y thus has median
# exp(x) and mean exp(x + .5sigma^2) = exp(x + .5)
# First generate data from the model y = exp(x + epsilon),
# epsilon ~ Gaussian(0, 1)
set.seed(139)
n < 1000
x < rnorm(n)
y < exp(x + rnorm(n))
f < areg.boot(y ~ x, B=20)
plot(f) # note log shape for y, linear for x. Good!
xs < c(2, 0, 2)
d < data.frame(x=xs)
predict(f, d, 'fitted')
predict(f, d, 'median') # almost same; median residual=.001
exp(xs) # population medians
predict(f, d, 'mean')
exp(xs + .5) # population means
# Show how smearingEst works
res < c(1,0,1) # define residuals
y < 1:5
ytrans < log(y)
ys < seq(.1,15,length=50)
trans.approx < list(x=log(ys), y=ys)
options(digits=4)
smearingEst(ytrans, exp, res, 'fitted') # ignores res
smearingEst(ytrans, trans.approx, res, 'fitted') # ignores res
smearingEst(ytrans, exp, res, 'median') # median res=0
smearingEst(ytrans, exp, res+.1, 'median') # median res=.1
smearingEst(ytrans, trans.approx, res, 'median')
smearingEst(ytrans, exp, res, 'mean')
mean(exp(ytrans[2] + res)) # should equal 2nd # above
smearingEst(ytrans, trans.approx, res, 'mean')
smearingEst(ytrans, trans.approx, res, mean)
# Last argument can be any statistical function operating
# on a vector that returns a single value
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.