Learn R Programming

yaImpute (version 1.0-21)

newtargets: Finds K nearest neighbors for new target observations

Description

Finds nearest neighbor reference observations for a given set of target observations using an established (see yai) object. Intended use is to facilitate breaking up large imputation problems (see AsciiGridImpute).

Usage

newtargets(object,newdata,k=NULL,ann=NULL)

Arguments

object
an object of class yai.
newdata
a data frame or matrix of new targets for which neighbors are are found. Must include at least the X-variables used in the original call to yai.
k
if NULL, the value is taken from object, otherwise the number of nearest neighbors to find.
ann
if NULL, the value is taken from object. When TRUE ann is used to find neighbors, and when FALSE a slow exact search is used.

Value

  • An object of class yai, that is a copy of the first argument with the following elements replaced:
  • callthe call.
  • obsDroppeda list of the row names for observations dropped for various reasons (missing data).
  • trgRowsa list of the row names for target observations as a subset of all observations.
  • xallthe X-variables for all observations.
  • neiDstTrgsa matrix of distances between a target (identified by its row name) and the k references. There are k columns.
  • neiIdsTrgsa matrix of reference identifications that correspond to neiDstTrgs.
  • neiDstRefsset NULL as if noRefs=TRUE in the original call to yai.
  • neiIdsRefsset NULL as if noRefs=TRUE in the original call to yai.
  • kthe value of k, replaced if changed.
  • annthe value of the ann argument.

See Also

yai

Examples

Run this code
require (yaImpute)

data(iris)

# set the random number seed so that example results are consistant
# normally, leave out this command
set.seed(12345)

# form some test data
refs=sample(rownames(iris),50) # just the reference observations
x <- iris[refs,1:3]  # Sepal.Length Sepal.Width Petal.Length
y <- iris[refs,4:5]  # Petal.Width Species

# build a yai object using mahalanobis
mal <- yai(x=x,y=y,method="mahalanobis")

# get imputations for the target observations (not references)
malNew <- newtargets(mal,iris[!(rownames(iris) %in% rownames(x)),])

# output a data frame of observed and imputed values for
# the observations that are not in the original yai object

impute(malNew,vars=yvars(malNew))

# in this example, Y is not specified (not required for mahalanobis).
mal2 <- yai(x=x,method="mahalanobis")
identical(foruse(mal),foruse(mal2))

# here, method randomForest's unsupervised classification is used (no Y).
rf <- yai(x=x,method="randomForest")
# now get imputations for the targets in the iris data (those that are
# not references).
rfNew <- newtargets(rf,iris[!(rownames(iris) %in% rownames(x)),])

Run the code above in your browser using DataLab