Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to query an `R` function for the default value of its parameters?

Tags:

r

In most cases, the default value of function parameters is given in the documentation. However, in some cases the default value is computed from other parameters (including the data itself), so it cannot possibly be specified in the documentation.

For example, how might I discover the default lambda grid used for the function glmnet in the library glmnet? According to the documentation, the default lambda is computed based on nlambda, which defaults to 100, and lambda.min.ratio, which appears to be a data-derived value.

When I run this function with a given data set, I would like to know the value of lambda that it used. This is particularly useful when using cv.glmnet, because I want to know which lambda's it is picking among when I do not supply one.

Sample Input:

library(glmnet)

set.seed(1)
x=rnorm(100)
eps=rnorm(100)

y = 1 + x + x^2 + x^3 + eps

xmat=model.matrix(y~poly(x,10,raw=T),data=data.frame(x=x))

cv.out=cv.glmnet(xmat, y,alpha=0) # What is the lambda used here?
bestlam=cv.out$lambda.min
print(bestlam)


# When a grid is specified, the result is very different and sometimes worse.
grid=10^seq(10,-2,length=100)
cv.out=cv.glmnet(xmat, y,alpha=0, lambda=grid)
bestlam=cv.out$lambda.min
print(bestlam)

Sample Output (Notice they are very different):

0.3619167
0.04037017
like image 367
merlin2011 Avatar asked Nov 06 '13 02:11

merlin2011


People also ask

How do you pass a parameter to default value in R?

First time, function is called with single argument and default value is used for second argument. Function called successfully and produce a result. Second time, function is called with two arguments, default value of second argument is overridden.

What type of arguments can a function take in R?

Arguments are the parameters provided to a function to perform operations in a programming language. In R programming, we can use as many arguments as we want and are separated by a comma. There is no limit on the number of arguments in a function in R.

What does function () mean in R?

A key feature of R is functions. Functions are “self contained” modules of code that accomplish a specific task. Functions usually take in some sort of data structure (value, vector, dataframe etc.), process it, and return a result.


1 Answers

If the default values depend on the value of the other arguments, then I see no other solution than enter the function in debug mode when it is called. You can use debugonce for example:

> debugonce(cv.glmnet)
> 
> cv.out=cv.glmnet(xmat, y,alpha=0) # What is the lambda used here?
debugging in: cv.glmnet(xmat, y, alpha = 0)
[...]
Browse[2]> ls()
#  [1] "foldid"       "grouped"      "keep"         "lambda"       "nfolds"       "offset"      
#  [7] "parallel"     "type.measure" "weights"      "x"            "y"           
Browse[2]> lambda
NULL
Browse[2]> c
>

So for that first call, lambda is NULL. However, if you repeat that approach for your second call to cv.glmnet, you will see that in that case lambda is a numeric vector of length 100.

like image 186
flodel Avatar answered Oct 17 '22 04:10

flodel