[R] create picture (k -the nearest neighbours)

Uwe Ligges ligges at statistik.tu-dortmund.de
Sun Mar 14 18:27:02 CET 2010



On 09.03.2010 14:01, Grzesiek wrote:
>
> Hi
> I want to create a nice picture about my result of k -the nearest neighbours
> algorithm. Here is my easy code:
> #################################
> library(klaR)
> library(ipred)
> library(mlbench)
>
> data(PimaIndiansDiabetes2)
> dane=na.omit(PimaIndiansDiabetes2)[,c(2,5,9)]
> dane[,2]=log(dane[,2])
> dane[,1:2]=scale(dane[,1:2])
> zbior.uczacy=sample(1:nrow(dane),nrow(dane)/2,F)
>
> KNN=ipredknn(diabetes~glucose+insulin,data=dane,subset=zbior.uczacy,k=3)
> KNN=ipredknn(diabetes~glucose+insulin,data=dane,subset=zbior.uczacy,k=3)
>
> b= ifelse((a==1),'red','blue' )
> a=as.numeric(KNN$learn$y)
>
> plot(KNN$learn$X[,1], KNN$learn$X[,2],pch=a,col = ifelse((a==1),'red','blue'
> ))
>
> #################################
>
> and my problem is (it's difficult for me to explain it in english):
> What I should do if I want to get in my picture area (in different colors)
> which will be show my "clasters". Like in picture (dark blue and bright
> blue).
>
> http://n4.nabble.com/file/n1585911/dec.jpg


I guess you want to visualize both your data as well as the "learned" 
partitions of your classification method.

Function partimat() in klaR can do that, but currently not for knn but 
only for sknn (from the same package).
If you want knn() from e1071 or ipredknn() supported, please feel free 
to submit patches.

Example:

partimat(diabetes ~ glucose + insulin, data = dane,
          subset = zbior.uczacy, k = 3, method = "sknn")

Uwe Ligges



More information about the R-help mailing list