[R] "ipredknn" - How may I find values?

David Winsemius dwinsemius at comcast.net
Wed Oct 28 07:04:23 CET 2009


On Oct 27, 2009, at 8:37 PM, Grzes wrote:

>
> I'm sorry David, this is my code once again:
>
> 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)
>
> klasyfikatorKNN=ipredknn(diabetes~glucose 
> +insulin,data=dane,subset=zbior.uczacy,k=3)
>
> oceny=predict(klasyfikatorKNN,dane[-zbior.uczacy,],"class")
>
> df=data.frame(glucose=c(klasyfikatorKNN$learn$X[, 
> 1]),insulin=klasyfikatorKNN$learn$X[,2],diabetes=c(klasyfikatorKNN 
> $learn$y))
> df$diabetes=factor(df$diabetes)
>
> drawparti(df$diabetes, df$glucose, df$insulin, method = "sknn", prec  
> = 100,
> xlab = NULL, ylab = NULL)

If this had used lattice/grid graphics you would have gotten an  
object, but this was done with base graphics. If you type drawparti,   
you get the code. Seems fairly likely that this section could be  
modified to return the vector that has the information about a  
particular run:

  colorw <- grouping != khead
     err <- round(mean(colorw), 3)
     color <- ifelse(colorw, col.wrong, col.correct)

Why not try adding return(color) or return( matrix( c(color,grouping),  
ncol=2) ) after that section? I then get this

 > draw.obj <- drawparti(df$diabetes, df$glucose, df$insulin, method =  
"sknn", prec = 100,  xlab = NULL, ylab = NULL)
 > head(draw.obj)
      [,1]    [,2]
[1,] "red"   "1"
[2,] "black" "1"
[3,] "black" "1"
[4,] "black" "2"
[5,] "black" "1"
[6,] "red"   "1"

That seems to capture the information you are requesting. Appears that  
a red "1" is in one of the red areas, a black "2" is in red.

-- 
David

>
> But in my computer everything is ok. The "drawparti" is in "klaR"  
> package.
> Or maybe try like this:
>
> drawparti(klasyfikatorKNN$learn$y, df$glucose, klasyfikatorKNN$learn 
> $X[,2],
> method = "sknn", prec = 100, xlab = NULL, ylab = NULL)
>
>
>
>
>
> David Winsemius wrote:
>>
>>
>> On Oct 27, 2009, at 10:18 AM, Grzes wrote:
>>
>>>
>>> Yes, I want to know which points in my picture are in red or green
>>> area.
>>> For example:
>>> .............glucose..........insulin.....diabetes
>>> 609  0.95177272  1.13996901        1   - I want to know that it's  
>>> for
>>> example: black point in red area
>>
>> red area?
>>
>>
>>> 253 -1.05724970 -1.15881433        1   - it's for example: black
>>> point in
>>> green area
>>
>> green area?
>>
>>> 319 -0.24716002  0.18483054        1
>>> 302  0.69254402  0.13252965        2
>>>
>>> If it's impossible plese give me any package or function which can
>>> do it.
>>
>> I already asked what was different about your code that was able to  
>> do
>> plotting without error on your machine.
>>
>> -- 
>> David
>>>
>>>
>>> Max Kuhn wrote:
>>>>
>>>> I think we are having some difficulty understanding what you are
>>>> looking for. If you are looking to find which of the training  
>>>> samples
>>>> were closest to the prediction sample, I don't think that you can  
>>>> get
>>>> it from this function.
>>>>
>>>> If this is what you want, I use the dist function in the proxy
>>>> package.
>>>>
>>>> Max
>>>>
>>>> On Tue, Oct 27, 2009 at 8:46 AM, David Winsemius <dwinsemius at comcast.net
>>>>>
>>>> wrote:
>>>>>
>>>>> On Oct 27, 2009, at 6:02 AM, Grzes wrote:
>>>>>
>>>>>>
>>>>>> Hi everybody!
>>>>>>
>>>>>> I want to find a closer neighbourins observation. This is my  
>>>>>> 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)
>>>>>>
>>>>>>
>>>>>> klasyfikatorKNN=ipredknn(diabetes~glucose
>>>>>> +insulin,data=dane,subset=zbior.uczacy,k=3)
>>>>>>
>>>>>> oceny=predict(klasyfikatorKNN,dane[-zbior.uczacy,],"class")
>>>>>>
>>>>>> #data frames with my result from klasyfikatorKNN
>>>>>>
>>>>>> df=data.frame(glucose=c(klasyfikatorKNN$learn$X[,
>>>>>> 1]),insulin=klasyfikatorKNN$learn$X[, 
>>>>>> 2],diabetes=c(klasyfikatorKNN
>>>>>> $learn$y))
>>>>>> #And picture
>>>>>> drawparti(as.factor(df$diabetes), df$glucose, df$insulin,  
>>>>>> method =
>>>>>> "sknn",
>>>>>> prec = 100, xlab = NULL, ylab = NULL)
>>>>>
>>>>> I get an error: Error: could not find function "drawparti"
>>>>>
>>>>>>
>>>>>> ##########################
>>>>>> My question is:  How or where may I find correct or wrong values
>>>>>> which
>>>>>> were
>>>>>> drawn (found,classification) in this picture?
>>>>>
>>>>> No picture resulted.
>>>>>
>>>>>> It means I'm looking for  x, y
>>>>>> values.
>>>>>
>>>>> Not sure exactly what you are asking. Does this modification to df
>>>>> and
>>>>> fairly obvious the cross table help?
>>>>>
>>>>>>
>>>>>> df=data.frame(glucose=c(klasyfikatorKNN$learn$X[,
>>>>>> 1]),insulin=klasyfikatorKNN$learn$X[,
>>>>>> 2],pred.diabetes=klasyfikatorKNN$learn$y,
>>>>>> trueDiab=dane[,3])
>>>>> Warning message:
>>>>> In data.frame(glucose = c(klasyfikatorKNN$learn$X[, 1]), insulin =
>>>>> klasyfikatorKNN$learn$X[,  :
>>>>> row names were found from a short variable and have been discarded
>>>>>> with( df, table(pred.diabetes, trueDiab))
>>>>>            trueDiab
>>>>> pred.diabetes neg pos
>>>>>         neg 174  86
>>>>>         pos  88  44
>>>>>
>>>>>
>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> View this message in context:
>>>>>> http://www.nabble.com/%22ipredknn%22---How-may-I-find-values--tp26074994p26074994.html
>>>>>> Sent from the R help mailing list archive at Nabble.com.
>>>>>>
>>>>>> ______________________________________________
>>>>>> R-help at r-project.org mailing list
>>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>>> PLEASE do read the posting guide
>>>>>> http://www.R-project.org/posting-guide.html
>>>>>> and provide commented, minimal, self-contained, reproducible  
>>>>>> code.
>>>>>
>>>>> David Winsemius, MD
>>>>> Heritage Laboratories
>>>>> West Hartford, CT
>>>>>
>>>>> ______________________________________________
>>>>> R-help at r-project.org mailing list
>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>> PLEASE do read the posting guide
>>>>> http://www.R-project.org/posting-guide.html
>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>>
>>>>
>>>>
>>>>
>>>> -- 
>>>>
>>>> Max
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide
>>>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>>
>>>
>>> -- 
>>> View this message in context:
>>> http://www.nabble.com/%22ipredknn%22---How-may-I-find-values--tp26074994p26078505.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>> David Winsemius, MD
>> Heritage Laboratories
>> West Hartford, CT
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>
> -- 
> View this message in context: http://www.nabble.com/%22ipredknn%22---How-may-I-find-values--tp26074994p26087509.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list