Skip to contents

A wrapper for kknn::kknn() for use in nadir::super_learner() with binary outcomes. Predicted probabilities for the outcome being 1/TRUE are the (kernel-weighted) proportion of the k nearest neighbors with outcome 1/TRUE.

Usage

lnr_knn_binary(data, formula, k = 7, ...)

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 for the probability of the outcome being 1/TRUE (a numeric vector of values, one for each row of newdata).

Details

Note that kknn::kknn() does not support observation weights, so no weights argument is accepted here.

See also

binary_learners

Examples

lnr_knn_binary(mtcars, am ~ hp + mpg)(mtcars)
#>  [1] 0.63265306 0.63265306 0.48979592 0.57142857 0.18367347 0.30612245
#>  [7] 0.14285714 0.57142857 0.44897959 0.26530612 0.16326531 0.00000000
#> [13] 0.06122449 0.00000000 0.00000000 0.00000000 0.14285714 0.97959184
#> [19] 0.97959184 0.97959184 0.48979592 0.00000000 0.00000000 0.14285714
#> [25] 0.18367347 0.81632653 0.65306122 0.97959184 0.36734694 0.26530612
#> [31] 0.48979592 0.61224490