Find nearest neighbor names from KKNN package
I have been trying to build this program or find out how to access what
KKNN does to produce its results. I am using the KKNN function and package
to help predict future baseball stats. It takes in 11 predictor variables
(previous 3 year stats, PA and level, along with age and another
predictor). The predictions work great but what I am hoping to do is when
I am predicting only one player (as this would be ridiculous while
predicting 100s of players), I would like to see maybe the 3 closest
neighbors to the player in question and their previous stats with what
they produced the next year. I am most concerned with the name of the
nearest neighbors as knowing which players are closest will give context
to the prediction that it makes.
I am fine with trying to edit the actual code to the function if that is
the only way to get at these. Even finding the indices would be helpful as
I can backsolve from there to get the names. Thank you so much for all of
your help!
Here is some sample code that should help:
name=c("McGwire,Mark","Bonds,Barry","Helton,Todd","Walker,Larry","Pujols,Albert","Pedroia,Dustin")
z
lag1=c(100,90,75,89,95,70)
lag2=c(120,80,95,79,92,90)
Runs=c(65,120,105,99,65,100)
full=cbind(name,lag1,lag2,Runs)
full=data.frame(full)
learn=full
learn
learn$lag1=as.numeric(as.character(learn$lag1))
learn$lag2=as.numeric(as.character(learn$lag2))
learn$Runs=as.numeric(as.character(learn$Runs))
valid=learn[5,]
learn=learn[-5,]
valid
k=kknn(Runs~lag1+lag2,learn,valid,k=2,distance=1)
summary(k)
fit=fitted(k)
fit
No comments:
Post a Comment