View source: R/robust_transformation.R
| predict.specproc_transformation | R Documentation |
Transforms new data with the parameters (and, with standardize = TRUE, the mean and standard deviation of the inliers) estimated by
robust_boxcox(), robust_yeojohnson() or robust_gandh(). With inverse = TRUE, it
transforms values back to the original scale, such as the predictions of
a model fitted to the transformed variables.
## S3 method for class 'specproc_transformation'
predict(
object,
newdata,
inverse = FALSE,
estimate = c("median", "mean"),
residuals = NULL,
...
)
object |
An object returned by |
newdata |
A numeric vector, matrix or data frame with some or all of
the variables of the fit: new data, or with |
inverse |
A logical value: transform the values back to the
original scale ( |
estimate |
With |
residuals |
With |
... |
Not used. |
newdata can hold any of the variables of the fit, matched by name. A
matrix or vector without column names must have all of them, in their
order.
The back-transformation undoes, in reverse order, the standardization,
the transformation and the pre-standardization of each variable. When a
model predicts \hat{z} on the transformed scale with symmetric
errors, the back-transformed value g^{-1}(\hat{z}) estimates the
median of the variable on the original scale (estimate = "median", the
default). It needs no other assumption, and suits the prediction of one
sample.
For the power transformations, on the original scale the mean is higher than the median when
\lambda < 1 (the transformation compresses the upper tail), and
lower when \lambda > 1. estimate = "mean" gives the smearing
estimate of Duan (1983),
\hat{x} = \frac{1}{n} \sum_{k=1}^{n} g^{-1}(\hat{z} + e_k),
where e_1, \dots, e_n are the residuals of the model on the
transformed scale (preferably cross-validated, since the residuals of the
fit underestimate the errors of prediction). It assumes no distribution
of the errors, only that they do not depend on \hat{z}. The mean
matters when the predictions are averaged or summed, such as for the mean
content of a lot or a mass balance. It is not robust: an outlying
residual pulls it, so pass the residuals of the inliers of a robust model
(for example, of the spectra that rsimpls() does not flag).
Smearing is also available for robust_gandh(); the direction of the
mean-median difference depends on its fitted shape and residuals.
The back-transformation is not defined everywhere, because some
transformations are bounded: the Box-Cox transformation above for
\lambda < 0, and the Yeo-Johnson transformation above for
\lambda < 0 and below for \lambda > 2. Values beyond the
bound (with estimate = "mean", values that pass it with one of the
residuals) become NA, with a warning.
A tibble with the variables of newdata, in the order of the
fit. Missing values stay missing, and with the Box-Cox transformation,
new values that are zero or negative become NA.
Duan, N. (1983). Smearing estimate: a nonparametric retransformation method. Journal of the American Statistical Association, 78(383):605-610.
robust_boxcox(), robust_yeojohnson(), robust_gandh()
data(forageLIBS)
contents <- forageLIBS[c("Ca", "K", "P", "Mn")]
fit <- robust_boxcox(contents[1:300, ])
new <- predict(fit, contents[301:368, ])
# back to mg/kg
head(predict(fit, new, inverse = TRUE))
# Box-Cox is not defined for zero or negative values
x <- contents[301:302, ]
x$Ca[2] <- 0
predict(fit, x)
# a calibration of the transformed Mn contents on the Mn II line at
# 257.61 nm, back to mg/kg
wl <- suppressWarnings(as.numeric(names(forageLIBS)))
line <- forageLIBS[[which.min(abs(wl - 257.61))]]
fit <- robust_boxcox(forageLIBS[1:300, "Mn", drop = FALSE])
model <- lm(Mn ~ log(line), data = data.frame(Mn = fit$transformation$Mn,
line = line[1:300]))
pred <- data.frame(Mn = predict(model, data.frame(line = line[301:368])))
med <- predict(fit, pred, inverse = TRUE)
avg <- predict(fit, pred, inverse = TRUE, estimate = "mean",
residuals = residuals(model))
summary(avg$Mn / med$Mn)
# the mean content of the 68 samples
c(median = mean(med$Mn), mean = mean(avg$Mn),
reference = mean(forageLIBS$Mn[301:368]))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.