Giovanni,

You can use the '...' for that, as in:

loocv <- function(data, fnc, ...) {
 n <- length(data.x)
 score <- 0
 for (i in 1:n) {
   x_i <- data.x[-i]
   y_i <- data.y[-i]
   yhat <- fnc(x=x_i,y=y_i, ...)
   score <- score + (y_i - yhat)^2
 }
 score <- score/n
 return(score)
}

scoreks <- loocv(data,gaussiankernel, h=0.5)


Regards,
Jan







On Mon, Apr 26, 2010 at 1:53 AM, Giovanni Azua <brave...@gmail.com> wrote:
> The beauty of trial and error ... if I leave the non x, y parameters i.e. h 
> as global parameters rather than formal parameters for gaussiankernel it 
> works fine basically I don't pass anymore h=0.5 to gaussiankernel but consume 
> it from a global variable. Ugly but works ...
>
> Best regards,
> Giovanni
>
> On Apr 26, 2010, at 1:38 AM, Giovanni Azua wrote:
>
>> Hello,
>>
>> I have the following function that receives a "function pointer" formal 
>> parameter name "fnc":
>>
>> loocv <- function(data, fnc) {
>>   n <- length(data.x)
>>   score <- 0
>>   for (i in 1:n) {
>>     x_i <- data.x[-i]
>>     y_i <- data.y[-i]
>>     yhat <- fnc(x=x_i,y=y_i)
>>     score <- score + (y_i - yhat)^2
>>   }
>>   score <- score/n
>>   return(score)
>> }
>>
>> I would like to use it like this:
>>
>> ##
>> ## Estimator function using Gaussian Kernel
>> ##
>> gaussiankernel <- function(x,y,h) {
>>   modelks <- ksmooth(x,y,kernel="normal",bandwidth=h,x.points=x)
>>   yhat <- modelks$y
>>   return(yhat)
>> }
>>
>> scoreks <- loocv(data,gaussiankernel(h=0.5))
>>
>> I expected this to work but it doesn't :( basically I wanted to take 
>> advantage of the named parameters so I could pass the partially specified 
>> function parameter "gaussiankernel" to loocv specifying only the h parameter 
>> and then let loocv specify the remaining parameters as needed ... can this 
>> be tweaked to work? The idea is to have loocv generic so it can work for any 
>> estimator implementation ...
>>
>> I have more than 6 books now in R and none explains this important concept.
>>
>> Thanks in advance,
>> Best regards,
>> Giovanni
>>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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.
>

______________________________________________
R-help@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.

Reply via email to