# [R] outputs of KNN prediction

Liaw, Andy andy_liaw at merck.com
Mon Feb 23 14:19:49 CET 2004

```Sounds like you are trying to do k-NN regression (i.e., predict numeric
variable given others) with 12 predictors, some of which are categorical.
If so, you're quite possibly out of luck.  k-NN methods depend on distances,
and you need to have a distance that make sense for your problem and works
for mix of both numeric and categorical variables.  There are some (I
believe in the daisy() function in the `cluster' package has one), but no
knn functions that use anything but Euclidean distances (which only make
sense if all variables are numeric).

Even if all your predictors are numeric, you're probably going to have a
hard time finding a k-NN regressor that works in 12 dimensions (at least I
don't know of any implementation, in any language).

Cheers,
Andy

> From: Qin Liu
>
> Hello there:
>
> I got 13 variables in my training/target set, the first 12
> variables are
> mixture of numerical and categorical variables. The last one
> is the one I
> need
> to predict, and it is a numerical variable.
>
> >cl<-factor(train[,13])
> >pred<-knn(train, test, clk=3, prob=TRUE)
> >pred
> I got output on the screen as following
>
>  8.10832229  8.831127635 7.592870288 8.521782644 6.376726948
> 6.914730893
>  9.71534911  8.658171785 6.104793232 7.247080585 6.298949247
> 5.105945474
>  7.508238775 7.65396918  8.831127635 7.912056888 6.381816017
> 7.211556733
>
>  276 levels ............
>
> Then I tried to calculate a formula:(actual-pred)/actual,
> which includes
> pred
> values. However, I cannot do this even I convert the factors
> in pred to
> vectors. Can anybody help??
>
> Thank you very much indeed,
>
> Qin
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help