| outpro | R Documentation |
outpro assesses how well observations are supported by the
training data in a selected set of predictors. With a varpro
object, variable priorities guide the selection and weighting of these
predictors. Larger distances indicate greater departure from the
training data. outpro.null provides a reference distribution for
interpreting the distances.
outpro(object,
newdata,
neighbor = NULL,
distancef = "knn",
reduce = TRUE,
cutoff = NULL,
max.rules.tree = 150,
max.tree = 150,
knn.chunk.size = 100L,
newdata.xscale = FALSE)
outpro.null(object,
nulldata = NULL,
neighbor = NULL,
distancef = "knn",
reduce = TRUE,
cutoff = NULL,
max.rules.tree = 150,
max.tree = 150,
knn.chunk.size = 100L,
nulldata.xscale = FALSE)
object |
A |
newdata |
Data frame of observations to score. For a
|
neighbor |
Number of training neighbors used for each
observation. The default is |
distancef |
Distance function. The default, |
reduce |
Variable selection and weighting. Use |
cutoff |
Minimum variable-priority |
max.rules.tree |
Maximum number of rules per tree used to
construct forest-derived neighborhoods. Ignored when
|
max.tree |
Maximum number of trees used to construct
forest-derived neighborhoods. Ignored when
|
knn.chunk.size |
Positive integer giving the number of
observations processed together for |
newdata.xscale |
Advanced option for |
nulldata |
Optional data frame representing the population
regarded as in distribution. These observations are scored against
the training data to obtain a reference distribution of distances.
Supply predictors in the same form as |
nulldata.xscale |
Advanced option for |
outPro assesses whether an observation is close to the training data in
a predictor subspace chosen for the modeling problem. For a
varpro object, variable priorities guide both predictor selection
and weighting. The resulting distance emphasizes departures in
predictors that varPro identifies as important.
The default method, distancef = "knn", standardizes the selected
predictors using their training means and standard deviations. It then
finds the neighbor training observations closest to each scored
observation, using weighted Manhattan distance. The score is the mean
distance to these neighbors. Smaller scores indicate closer training
support; larger scores indicate greater separation. Smaller
neighborhoods emphasize immediate local support, while larger
neighborhoods average over a broader region of the training data.
Use reduce = TRUE for automatic varPro selection based on
cutoff, or reduce = FALSE to assess departures across all
predictors with equal weights. A character vector, such as
reduce = c("x1", "x2"), restricts scoring to those predictors
with equal weights.
A named numeric vector specifies predictors and relative weights.
Priorities and supplied weights are squared and normalized to sum to
one for the distance calculation. For example,
reduce = c(x1 = 2, x2 = 1) gives distance weights of 0.8
and 0.2. Supply nonnegative relative weights, with at least one
positive value.
Names supplied through reduce must match the training predictor
columns, including hot-encoded names in object$x for
varpro objects. Predictors with zero or nonfinite training
standard deviation are omitted. Inspect selected.variables and
distance.args$weights.used in the result to see the predictors
and weights used.
Use outpro(object, newdata = newdata) to score new observations.
The returned distance vector contains one score per row, in the
same order as newdata.
To score the training observations, omit newdata. The default
KNN method then excludes each observation from its own neighborhood,
leaving at most n - 1 neighbors. Supplying the training data
explicitly as newdata retains self matches.
outpro.null provides an empirical reference distribution for
interpreting distances. Supply nulldata to use a separate sample
representing the population regarded as in distribution, or omit it to
use the training observations. The model's training data remain the
source of neighbors; nulldata supplies the observations whose
distances form the reference distribution.
For a reference result ref and a scoring result op,
ref$cdf(op$distance) gives each observation's reference
percentile. A value of 0.99 means that 99 percent of the
reference distances are no larger than that observation's distance.
Large percentiles therefore identify unusually weak training support
relative to the reference sample. A rule such as
ref$cdf(op$distance) > 0.95 flags observations in the upper tail
of this distribution.
The corresponding support score, 1 - ref$cdf(op$distance), is
the fraction of reference distances greater than the scored distance.
Smaller values indicate less support. These summaries describe position
within the empirical reference distribution.
Use the same model, predictors, weights, neighborhood size, and distance
function for reference and new observations. For forest-based distances,
also keep max.tree and max.rules.tree the same. The
examples illustrate the default KNN calibration workflow.
The other distancef options use neighborhoods defined by the
forest. Each score summarizes standardized differences between the
observation and its forest-derived neighbors:
"prod"Mean across neighbors of the weighted geometric mean of absolute coordinate differences, with a small positive offset.
"euclidean"Mean weighted Euclidean distance.
"manhattan"Mean weighted Manhattan distance. This
uses the same pairwise metric as "knn", but with
forest-derived neighbors.
"minkowski"Mean weighted Minkowski distance of order 4.
"mahalanobis"Mean Mahalanobis-type distance based on weighted absolute coordinate differences and the covariance of the standardized training predictors.
"kernel"One minus the mean Gaussian similarity to the neighbors, based on weighted squared Euclidean distances. The bandwidth is estimated separately for each scoring call, so scores from separate calls can have different scales.
outpro returns a list with the following components:
distance |
Numeric vector of distances, one per scored observation. Larger values indicate greater departure from the training data in the selected predictor subspace. |
selected.variables |
Predictor names used in scoring, after removing variables with zero or nonfinite training standard deviation. |
selected.weights |
Relative predictor weights after rescaling
and squaring. These precede the final normalization to sum to one;
|
distance.args |
Distance settings used, including
|
neighbor |
Requested neighborhood size after rounding and
restriction to the training sample size. For KNN training scores,
|
cutoff |
Variable-priority threshold setting. It affects
selection only for |
means, sds |
Training means and standard deviations for the predictors used in scoring. |
dropped.zero.sd.variables |
Names of predictors removed because their training standard deviations were zero or nonfinite. |
distance.object |
A list retaining the distance information,
including standardized training and scored predictors
( |
score |
Forest-derived neighborhood information. |
oob.bits |
Scoring-mode indicator: |
newdata.xscale |
Whether supplied data were treated as already expressed in the model's encoded predictor columns. |
call |
The matched function call. |
outpro.null returns the same components, together with:
cdf |
The empirical cumulative distribution function of the reference distances. Apply this function to new distances to obtain their reference percentiles. |
quantile |
The empirical cumulative probability of each reference
observation's distance, computed as |
varpro, rfsrc.
## Simulate a regression problem with two signal and two noise variables.
set.seed(123)
n <- 800
dta <- data.frame(x1 = rnorm(n), x2 = rnorm(n),
noise1 = rnorm(n), noise2 = rnorm(n))
dta$y <- 2 * dta$x1 + dta$x2^2 + rnorm(n)
## Use separate training, reference, and test samples.
vp <- varpro(y ~ ., data = dta[1:500, ])
reference.data <- dta[501:650, ]
test.data <- dta[651:800, ]
## Shift one signal variable in half of the test observations.
shifted <- seq_len(nrow(test.data)) > 75
test.data$x1[shifted] <- test.data$x1[shifted] + 6
## Score test observations using the default KNN method.
op <- outpro(vp, newdata = test.data)
print(op$selected.variables)
print(setNames(op$distance.args$weights.used, op$selected.variables))
## Compare the test distances with an in-distribution reference sample.
ref <- outpro.null(vp, nulldata = reference.data)
percentile <- ref$cdf(op$distance)
support <- 1 - percentile
flag <- percentile > 0.95
print(head(data.frame(distance = op$distance, percentile = percentile,
support = support, flag = flag)))
print(tapply(percentile, shifted, median))
print(table(shifted = shifted, flagged = flag))
## Alternatively, use training distances as the reference distribution.
## Omit nulldata so each training observation excludes itself in KNN scoring.
ref.train <- outpro.null(vp)
print(head(ref.train$cdf(op$distance)))
## Use all predictors with equal weights.
op.all <- outpro(vp, newdata = test.data, reduce = FALSE)
## Specify a subspace with equal weights.
op.subspace <- outpro(vp, newdata = test.data,
reduce = c("x1", "x2"))
## Specify relative weights. Squaring and normalization give 0.8 and 0.2.
op.weighted <- outpro(vp, newdata = test.data,
reduce = c(x1 = 2, x2 = 1))
print(op.weighted$distance.args$weights.used)
## Use forest-derived neighborhoods with a product distance.
op.prod <- outpro(vp, newdata = test.data, distancef = "prod")
print(head(op.prod$distance))
## Recompute the reference distribution when changing scoring settings.
ref.prod <- outpro.null(vp, nulldata = reference.data, distancef = "prod")
print(head(ref.prod$cdf(op.prod$distance)))
## Compare all five methods on the same test observations.
## AUC measures discrimination using raw distances, with larger values
## identifying shifted observations. Tied distances receive average ranks.
## At a 0.95 reference-percentile cutoff, report the false-positive rate
## (FPR) among unshifted observations and the true-positive rate (TPR)
## among shifted observations, using each reference strategy.
compare.performance <- function(op, ref, ref.train) {
distance <- op$distance
n1 <- sum(shifted)
n0 <- sum(!shifted)
auc <- (sum(rank(distance, ties.method = "average")[shifted]) -
n1 * (n1 + 1) / 2) / (n1 * n0)
flag.reference <- ref$cdf(distance) > 0.95
flag.training <- ref.train$cdf(distance) > 0.95
c(AUC = auc,
FPR.reference = mean(flag.reference[!shifted]),
TPR.reference = mean(flag.reference[shifted]),
FPR.training = mean(flag.training[!shifted]),
TPR.training = mean(flag.training[shifted]))
}
## Each reference distribution uses the same scoring settings as its
## corresponding test scores. Reuse the references already computed.
performance <- rbind(
KNN.varPro = compare.performance(op, ref, ref.train),
KNN.all = compare.performance(
op.all,
outpro.null(vp, nulldata = reference.data, reduce = FALSE),
outpro.null(vp, reduce = FALSE)),
KNN.signal = compare.performance(
op.subspace,
outpro.null(vp, nulldata = reference.data,
reduce = c("x1", "x2")),
outpro.null(vp, reduce = c("x1", "x2"))),
KNN.weighted = compare.performance(
op.weighted,
outpro.null(vp, nulldata = reference.data,
reduce = c(x1 = 2, x2 = 1)),
outpro.null(vp, reduce = c(x1 = 2, x2 = 1))),
Forest.product = compare.performance(
op.prod, ref.prod, outpro.null(vp, distancef = "prod"))
)
print(round(performance, 3))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.