[R] Leave One Out Cross Validation

David Winsemius dwinsemius at comcast.net
Sat Jun 20 12:42:38 CEST 2009


On Jun 19, 2009, at 7:45 PM, muddz wrote:

>
> Hi Uwe,
>
> My apologies.
>
> Please if I can be guided what I am doing wrong in the code. I  
> started my
> code as such:
>
> # ypred is my leave one out estimator of x

Estimator of x? Really?

> cvhfunc<-function(y,x,h)    {
>     ypred<-0
>     for (i in 1:n){   #how did you define "n"? It's not in your  
> parameter list
>        for (j in 1:n){
>        if (j!=i){
>             ypred<-ypred +    (y[i]*k( (x[j]-x[i])/h ) )/
                                       k( (x[j]-x[i])/h )

At this point you are also using "k" as a function. Have you at any  
point defined "k"?

Also multiplication of the ratio of two identical numbers would  
generally give a result of y[i] for that second term. Unless, of  
course, any x[j] = x[i] in which case you will throw an error and  
every thing will grind to a halt.

It might help if you now explained what you think "CV" should be.

>                 }
>                   }   }
>     ypred     # not sure what that will do inside the function. If  
> it's there for debugging you may want to print(ypred)
>
> #CVh is a

# Yes? CVh is a ....what ?

>      cvh<-0
>      cvh<-cvh+((1/n)*(y-ypred)^2    # n again. R will still not know  
> what that is.
>      cvh

# ypred is a scalar, while y is a vector, so cvh will be a vector. Is  
that what you want?

>                              }

> test2<-cvhfunc(ydat,xdat,.2);test2
>
> #I was experimenting with the following data:
> library(datasets)
> data(faithful)
> ydat<-faithful$eruptions;ydat;plot(ydat);par(new=T)
> xdat<-faithful$waiting;xdat;plot(xdat,col="blue")
>
> # I want to minimize the CV function with respect to h. Thanks.
>
>
>
>
> Uwe Ligges-3 wrote:
>>
>> See the posting guide:
>> If you provide commented, minimal, self-contained, reproducible code
>> some people may be willing to help on the list.
>>
>> Best,
>> Uwe Ligges
>>
>>
>> muddz wrote:
>>> Hi All,
>>>
>>> I have been trying to get this LOO-Cross Validation method to work  
>>> on R
>>> for
>>> the past 3 weeks but have had no luck. I am hoping someone would  
>>> kindly
>>> help
>>> me.
>>>
>>> Essentially I want to code the LOO-Cross Validation for the 'Local
>>> Constant'
>>> and 'Local Linear' constant estimators. I want to find optimal h,
>>> bandwidth.
>>>
>>> Thank you very much!
>>> -M
>>>
>>>
>>
>> ______________________________________________
>> 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/Leave-One-Out-Cross-Validation-tp24025738p24120380.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