[R] kknn::predict and kknn$fitted.values

Jonathan Henkelman jonathan.henkelman at usask.ca
Sat Aug 29 00:21:22 CEST 2015


I am noticing that there is a difference between the fitted.values returned
by train.kknn, and the values returned using predict with the same model and
dataset. For example:

> data (glass)
> tmp <- train.kknn(Type ~ ., glass, kmax=1, kernel="rectangular",
> distance=1)
> tmp$fitted.values
[[1]]
  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1
 [62] 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 1 2 1 2 2 1 2 2 5 2 2 2 6 2 2 2 2 2 2 2 2 2 2 2 2
[123] 2 2 2 2 3 2 2 2 5 5 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 2 3 3
3 3 3 3 2 3 7 5 5 5 5 5 5 5 5 5 5 2 5 6 6 6 6 6 6 6
[184] 2 6 7 7 2 6 7 7 7 7 7 7 7 7 7 7 7 7 5 7 7 7 7 7 7 7 7 7 7 7 7
attr(,"kernel")
[1] rectangular
attr(,"k")
[1] 1
Levels: 1 2 3 5 6 7

> predict (tmp,glass)
  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 [62] 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
[123] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6
[184] 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
Levels: 1 2 3 5 6 7

When I check the confusion matricies for these I see that fitted.values is
giving some confusion, that is, like it is a true fit, whereas predict is
returning the exact answers.

> table (tmp$fitted.values[[1]],glass$Type)
     1  2  3  5  6  7
  1 69  4  0  0  0  0
  2  1 67  2  1  1  1
  3  0  1 15  0  0  0
  5  0  3  0 11  0  1
  6  0  1  0  0  8  1
  7  0  0  0  1  0 26

> table (predict(tmp,glass),glass$Type)   
     1  2  3  5  6  7
  1 70  0  0  0  0  0
  2  0 76  0  0  0  0
  3  0  0 17  0  0  0
  5  0  0  0 13  0  0
  6  0  0  0  0  9  0
  7  0  0  0  0  0 29

Can anyone clarify what fitted.values and predict actually do? I would have
expected they would give the same output.

Thanks... Jonathan



--
View this message in context: http://r.789695.n4.nabble.com/kknn-predict-and-kknn-fitted-values-tp4711625.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list