knnreg
From caret v4.17
by Max Kuhn
k-Nearest Neighbour Regression
$k$-nearest neighbour clasregressionsification that can return the average value for the neighbours.
- Keywords
- multivariate
Usage
## S3 method for class 'default':
knnreg(x, ...)## S3 method for class 'formula':
knnreg(formula, data, subset, na.action, k = 5, ...)
## S3 method for class 'matrix':
knnreg(x, y, k = 5, ...)
## S3 method for class 'data.frame':
knnreg(x, y, k = 5, ...)
knnregTrain(train, test, y, k = 5, use.all=TRUE)
Arguments
- formula
- a formula of the form
lhs ~ rhs
wherelhs
is the response variable andrhs
a set of predictors. - data
- optional data frame containing the variables in the model formula.
- subset
- optional vector specifying a subset of observations to be used.
- na.action
- function which indicates what should happen when
the data contain
NA
s. - k
- number of neighbours considered.
- x
- a matrix or data frame of training set predictors.
- y
- a numeric vector of outcomes.
- ...
- additional parameters to pass to
knnregTrain
. - train
- matrix or data frame of training set cases.
- test
- matrix or data frame of test set cases. A vector will be interpreted as a row vector for a single case.
- use.all
- controls handling of ties. If true, all distances equal to the
k
th largest are included. If false, a random selection of distances equal to thek
th is chosen to use exactlyk
neighbours.
Details
knnreg
is similar to ipredknn
and knnregTrain
is a modification of knn
. The underlying
C code from the class
pacakge has been modifed to return average outcome.
Value
- An object of class
knnreg
. Seepredict.knnreg
.
Examples
data(BloodBrain)
inTrain <- createDataPartition(logBBB, p = .8)[[1]]
trainX <- bbbDescr[inTrain,]
trainY <- logBBB[inTrain]
testX <- bbbDescr[-inTrain,]
testY <- logBBB[-inTrain]
fit <- knnreg(trainX, trainY, k = 3)
plot(testY, predict(fit, testX))
Community examples
Looks like there are no examples yet.