A wrapper for kknn::kknn() for use in nadir::super_learner().
Arguments
- data
A dataframe to train a learner / learners on.
- formula
A regression formula to use inside this learner.
- k
The number of nearest neighbors to use; see
?kknn::kknn.- ...
Any extra arguments that should be passed to the internal model for model fitting purposes.
Value
A prediction function that accepts newdata,
which returns predictions (a numeric vector of values, one for each row
of newdata).
Details
k-nearest neighbors is a "lazy" learner: no model is fit at training time.
Instead, the training data are stored and predictions for newdata
are formed as a (kernel-weighted) average of the outcomes of the k
nearest training observations in covariate space. As a purely local,
instance-based method, kNN occupies a very different corner of the
bias-variance landscape than the global regression methods
(lnr_lm, lnr_glmnet, etc.) and the tree ensembles
(lnr_rf, lnr_ranger, lnr_xgboost), making it a useful
addition to a super learner library.
Note that kknn::kknn() does not support observation weights, so no
weights argument is accepted here.
Examples
lnr_knn(mtcars, mpg ~ hp + disp + wt)(mtcars)
#> [1] 21.48355 21.15371 24.19447 18.54285 17.23687 19.48336 15.54095 22.45816
#> [9] 21.67080 19.47845 19.47845 16.22616 16.32269 16.17822 12.97915 12.97915
#> [17] 13.18156 29.02648 30.38538 30.19206 22.70112 16.36014 16.45530 15.41743
#> [25] 17.38422 29.67474 25.76415 28.94157 15.23930 20.33915 15.07922 21.53193
lnr_knn(mtcars, mpg ~ hp + disp + wt, k = 5)(mtcars)
#> [1] 21.18868 21.28226 24.00453 18.97999 17.48759 19.23515 14.93549 22.89197
#> [9] 22.04109 19.05178 19.05178 16.09824 16.26737 16.01425 12.23036 12.23036
#> [17] 12.71688 30.02910 30.85608 30.99467 22.66696 16.19127 16.04398 14.85095
#> [25] 17.81429 30.35791 25.00280 29.24267 14.93506 20.41661 14.98976 21.27345
