0

I would like to fix a variable number of parameters with different values for a given function, while the remaining parameters stay variable in R, i.e. assuming to have a function e.g.

fun <- function(par) {
par[1]+2*par[2]+3^(par[3])-exp(par[4])
}

I would like to call the function fun once, when e.g. par[1], par[4] are fixed with an value e.g. par[1]=3, par[4]=1 and par[2], par[3] should stay variable. Another time only one parameter e.g. par[3] should be fixed and the remaining parameters should be variable. Is it possible to define therefore a function so that I can choose the parameters which should be fixed with a certain value and the remaining parameters stay variable? How is this possible? I need this, because I have a function with lots of parameter e.g. 20, so the solution must be flexible. Thank you in advance!

2 Answers 2

1

I believe it would be more flexible for you to give the function a ... parameter. The ... creates a flexible list, all you have to do then is checking your list if something in the lists exists you currently need. The only question is how to unpack the ... see for example here:

...[[1]] resembles the first element in a long lost of parameters.

select_features <- function(...) {

    chrom_list <- lapply(...[[1]], as.numeric)
    chrom_transformations <- lapply(...[[2]], as.data.table)
}

The magic is, you give the function always this list, and define its structure like:

If you want to have fixed and variable parameters within this list always sturctured, go and make the first 10 list entries for variable and the last 10 entries for fixed variables. Thus you always know where your fixed and variables parameters reside. If then one parameter of yours is not served, the fixed list entry gets a 0 or None. e.g.

Par 1 2 3 0(None) 4 5 6 7 8 9 10

It is also possible to keep the whole list variable, thus sometimes you have something like 10 arameters in ... or 15.

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

Comments

0

The user interface was not specified in the question but here are some possibilities.

1) Define a one line wrapper function each time you want to change what is fixed. Only pass the 2 variable parameters to fun2.

fun2 <- function(par) fun(c(3, par, 1))

fun2(2:3)
## [1] 31.28172

fun(c(3, 2:3, 1))  # same
## [1] 31.28172

2) Another possibility is to define the function so that any parameters that are NA are obtained from the last call. In this case par must contain 4 values but some of them can be NA.

fun3 <- local({
  prev <- numeric(4)
  function(par) {
    isna <- is.na(par)
    par[isna] <- prev[isna]
    prev <<- par
    fun(par)
  }
})

fun3(1:4)
## [1] -22.59815

fun3(c(NA, 2:3, NA))  # par[1] and par[4] taken from last call
## [1] -22.59815

fun3(c(1, NA, NA, 4))  # par[2] and par[3] taken from last call
## [1] -22.59815

3) In this case there is a second parameter which is a named list where the names are the positions in the expanded parameter vector of the fixed parameters and the values are the corresponding parameter values. The variable parameters are passed in par.

fun4 <- function(par, fixed = NULL) {
   if (length(fixed) == 0) return(fun(par))
   n <- length(par) + length(fixed)
   p <- numeric(n)
   pos <- as.numeric(names(fixed))
   p[pos] <- unlist(unname(fixed))
   p[-pos] <- par
   fun(p)
}

fun4(2:3, list("1" = 1, "4" = 4))
## [1] -22.59815

fun(1:4)
## [1] -22.59815

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.