I'm trying to perform a knn regression on a sales time series using the kknn package. I can't see how you can restrict the neighbours to past sales only though.


Product, Price, Sale Date
A,       £100,  2012-01-04
A,       £100,  2012-01-05
A,       £200,  2012-01-06

If I want to predict for the second row, it should only consider the first row as a neighbour -- not the third.

Is there a way to do this or am I going to have to write something custom?

If it's going to have to be custom, what's the most efficient way of doing this in R? My dataset is 200,000 rows so I imagine it could be slow going using a loop.

Any help you can offer is appreciated. Thanks

  • $\begingroup$ I've actually run into this exact same situation (with the same package). Unfortunately, the only solution I could find was to manually loop through the dataset. $\endgroup$
    – Zach
    Mar 5, 2012 at 20:46

1 Answer 1


Ended up implementing a custom function using sqldf to combine the train and test datasets, then ddply to get the means


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.