[R] Leave One Out Cross Validation

muddz mlaique at ryerson.ca
Sat Jun 20 01:45:02 CEST 2009


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
cvhfunc<-function(y,x,h){
ypred<-0
for (i in 1:n){
for (j in 1:n){
if (j!=i){
ypred<-ypred+(y[i]*k((x[j]-x[i])/h))/k((x[j]-x[i])/h)
}
}}
ypred

#CVh is a 
cvh<-0
cvh<-cvh+((1/n)*(y-ypred)^2
cvh
}
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.




More information about the R-help mailing list