Return the L_p norm distances of selected test observations relative to the training observations which the forest was trained on.

compute_lp(
  object,
  newdata,
  feature,
  p,
  scale = FALSE,
  aggregation = "average",
  trainingIdx = NULL
)

Arguments

object

A `forestry` object.

newdata

A data frame of test predictors.

feature

A string denoting the dimension for computing lp distances.

p

A positive real number determining the norm p-norm used.

scale

A boolean indicating whether or not we want to center + scale the features (based on the mean and sd of the training data) before calculating the L_p norm. This is useful for computing the detachment index, but can be less useful when we need to interpret the L_p distances.

aggregation

The aggregation used when the weightMatrix is calculated. This can be useful for calculating the lp distances on observations in the training data. This must be one of `average`, `oob`, or `doubleOOB`. When newdata has fewer rows than the training data, one must also pass the vector of training indices corresponding to the indices of the observations in the original data set. Default is `average`.

trainingIdx

This is an optional parameter that must be set when aggregation is set to `oob` or `doubleOOB` and the newdata is not the same size as the training data.

Value

A vector of the lp distances.

Examples


# Set seed for reproductivity
set.seed(292313)

# Use Iris Data
test_idx <- sample(nrow(iris), 11)
x_train <- iris[-test_idx, -1]
y_train <- iris[-test_idx, 1]
x_test <- iris[test_idx, -1]

rf <- forestry(x = x_train, y = y_train,nthread = 2)
predict(rf, x_test)
#>  [1] 6.655429 6.445297 4.960239 4.880679 6.060820 5.056067 5.105224 6.224275
#>  [9] 6.412462 5.743084 6.218823

# Compute the l2 distances in the "Petal.Length" dimension
distances_2 <- compute_lp(object = rf,
                          newdata = x_test,
                          feature = "Petal.Length",
                          p = 2)