2

The code is the following

crossval <- function(data, lambda=0, ytrans=function(x) x) 

I wanna know what function (x) in a function parameter list mean?if calling the same function again and again or something? If so is it possible to call the function crossval itself?Isthe variable ytrans necessary?cannot we just call the function(x) in the function body?

0

2 Answers 2

2

@duffymo covered it, but here's a bit longer explanation.

What you see there is called an anonymous function. The function basically returns the same objects (= does nothing). This could also be written explicitly as a named function

newFunction <- function(x) {
    x
}

which would then be

crossval <- function(data, lambda=0, ytrans = newFunction)

This is the default value, like in lambda=0, except the default value is a function itself.

Sign up to request clarification or add additional context in comments.

4 Comments

Technically I think the default value in the OPs case is the return value from the anonymous function, not the function itself if I understand correctly. I don't well understand the use for it though and that would be a helpful explanation to have.
That's what I meant - it returns (the same) object as it was passed in.
Ok, I think I get it, let's say newFunction squares x then you could do foo <- function( x , y ) y(x) and foo( 2 , newFunction ) returns 4. +1
so its in case we dont supply a value for ytrans,the program executes that function to give it a value?
1

Default function is one that returns the x value that it's passed. If you don't supply one, that's the function that will be used.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.