Learn R Programming

VIM (version 7.3.0)

gowerD: Computes the extended Gower distance of two data sets

Description

The function gowerD is used by kNN to compute the distances for numerical, factor ordered and semi-continous variables.

Usage

gowerD(
  data.x,
  data.y = data.x,
  weights = rep(1, ncol(data.x)),
  numerical = colnames(data.x),
  factors = vector(),
  orders = vector(),
  mixed = vector(),
  levOrders = vector(),
  mixed.constant = rep(0, length(mixed)),
  returnIndex = FALSE,
  nMin = 1L,
  returnMin = FALSE,
  methodStand = "range"
)

Arguments

data.x

data frame

data.y

data frame

weights

numeric vector providing weights for the observations in x

numerical

names of numerical variables

factors

names of factor variables

orders

names of ordered variables

mixed

names of mixed variables

levOrders

vector with number of levels for each orders variable

mixed.constant

vector with length equal to the number of semi-continuous variables specifying the point of the semi-continuous distribution with non-zero probability

returnIndex

logical if TRUE return the index of the minimum distance

nMin

integer number of values with smallest distance to be returned

returnMin

logical if the computed distances for the indices should be returned

methodStand

character either "range" or "iqr", iqr is more robust for outliers

Details

Numerical and semi-continuous (mixed) variables are standardised by their range (or IQR, see methodStand) before the distance is computed; for mixed variables the point mass (mixed.constant) is excluded from the range estimate and scaled along with the data.

Missing values in a distance variable are replaced by a sentinel (the pooled column maximum plus one) after standardisation. Two records both missing the same variable therefore have distance 0 on it, and a missing-vs-observed pair contributes up to about 2 (rather than a variable's nominal maximum of 1). This sentinel convention departs from Gower's (1971) omit-and-renormalise rule and biases nearest-neighbour selection towards records that share the same missingness pattern.

The distance computation is parallelised with OpenMP where R was built with OpenMP support. The number of threads is getOption("VIM.ncores"); if the option is unset, at most 2 threads are used while the package is checked (R CMD check sets _R_CHECK_LIMIT_CORES_; CRAN policy allows at most two cores) and OpenMP's default -- typically all cores, or OMP_NUM_THREADS -- otherwise. Results do not depend on the number of threads.

returnIndex=FALSE: a numerical matrix n x m with the computed distances returnIndex=TRUE: a named list with "ind" containing the requested indices and "mins" the computed distances

Examples

Run this code
data(sleep)
# all variables used as numerical
gowerD(sleep)

# split in numerical an
gowerD(sleep, numerical = c("BodyWgt", "BrainWgt", "NonD", "Dream", "Sleep", "Span", "Gest"),
  orders = c("Pred","Exp","Danger"), levOrders = c(5,5,5))

# as before but only returning the index of the closest observation
gowerD(sleep, numerical = c("BodyWgt", "BrainWgt", "NonD", "Dream", "Sleep", "Span", "Gest"),
  orders = c("Pred","Exp","Danger"), levOrders = c(5,5,5), returnIndex = TRUE)

Run the code above in your browser using DataLab