KlausVigo / kknn

Weighted k-Nearest Neighbors
http://klausvigo.github.io/kknn/
23 stars 10 forks source link

add type parameter in predict.kknn #1

Closed covoes closed 9 years ago

covoes commented 9 years ago

In the current version the predict function returns only the fitted values for the test data. Would it be possible to add a type parameter so the function could return the probabilities for each class? This would make it possible to obtain the probabilities using predict.train.kknn.

For instance:

predict.kknn <- function(object, type = 'raw', ...) 
{ 
    pred <- switch(type, raw = object$fit,
                         prob = object$prob,
                         stop('invalid type for prediction'))
    pred
}

predict.train.kknn <- function (object, newdata, ...) 
{
    if (missing(newdata)) 
        return(object$fit)
    res = kknn(formula(terms(object)), object$data, newdata, 
        k = object$best.parameters$k, kernel = object$best.parameters$kernel, 
        distance = object$distance)
   return(predict(res, ...))
}
KlausVigo commented 9 years ago

Hello @covoes, added your suggestions on github. Thanks, Klaus