3

I am investigating Knn regression methods and later Kernel Smoothing. I wish to demonstrate these methods using plots in R. I have generated a data set using the following code:

x = runif(100,0,pi)
e = rnorm(100,0,0.1)
y = sin(x)+e

I have been trying to follow a description of how to use "knn.reg" in 9.2 here: https://daviddalpiaz.github.io/r4sl/k-nearest-neighbors.html#regression

grid2=data.frame(x)
knn10 = FNN::knn.reg(train = x, test = grid2, y = y, k = 10)

My predicted values seem reasonable to me but when I try to plot a line with them on top of my x~y plot I don't get what I'm hoping for.

plot(x,y)
lines(grid2$x,knn10$pred)

RPlot

I feel like I'm missing something obvious and would really appreciate any help or advice you can offer, thank you for your time.

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
Ben
  • 33
  • 1
  • 5

1 Answers1

3

You just need to sort the x values before plotting the lines.

plot(x,y)
ORD = order(grid2$x)
lines(grid2$x[ORD],knn10$pred[ORD])

enter image description here

G5W
  • 36,531
  • 10
  • 47
  • 80
  • This has worked perfectly. But is there a reason why these values needed to be ordered? Have I used the other functions improperly or is this just an additional intermediate step? – Ben Dec 23 '17 at 16:53
  • They only need to be ordered to draw the chart. `lines` will draw lines between the points that you give it. If you use the points out of order it will (as you saw) draw lines back and forth across the screen. – G5W Dec 23 '17 at 16:58