Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: function parameters inheritance

So i have the following code:

library(semPLS)
data(ECSImobi)

#exponent vector for transformation of the dataset
#model of type plsm
#data 
#returns a list of models with length of exponent vector
#list items are of class sempls
CalcModels <- function(exponent,model,data){

  #initialize result as list
  result <- vector("list")

  #estimate models with different distances
  for(i in seq_along(exponent)){
    result[[i]] <- sempls(model=model, data = data^exponent[i])
  }

  return(result)

}

#calculate ecsi models with exponent 0.1, 0.2, ..., 2.0
ecsiModels <- CalcModels(c(1:20/10),ECSImobi,mobi)

The sempls() function has numerous other parameters. Is there any way I can give them to the CalcModels function, so I can call the CalcModels function with the additional parameter wscheme="pw" of the sempls function. I could write them all in the parameters of both functions, but I think there must be a smarter way that I'm missing.

So I want something like that:

library(semPLS)
data(ECSImobi)

#exponent vector for transformation of the dataset
#model of type plsm
#data 
#returns a list of models with length of exponent vector
#list items are of class sempls
CalcModels <- function(exponent,model,data,wscheme){

  #initialize result as list
  result <- vector("list")

  #estimate models with different distances
  for(i in seq_along(exponent)){
    result[[i]] <- sempls(model=model, data = data^exponent[i],wscheme=wscheme)
  }

  return(result)

}

#calculate ecsi models with exponent 0.1, 0.2, ..., 2.0
ecsiModels <- CalcModels(c(1:20/10),ECSImobi,mobi,"pw")

but instead of writing each parameter in both functions do some kind of inheritance of the parameters without overwriting the function.

like image 454
Jannic Beck Avatar asked Oct 23 '25 15:10

Jannic Beck


1 Answers

Would you like to use ellipsis (see here for more details)? This is a very useful technique to pass arguments to another function. All you would need to do is to add ... to the definition of function:

CalcModels <- function(exponent,model,data,...){

  #initialize result as list
  result <- vector("list")

  #estimate models with different distances
  for(i in seq_along(exponent)){
    result[[i]] <- sempls(model=model, data = data^exponent[i],...)
  }

  return(result)

}

Ellipsis can pass any number of parameters.

like image 171
Marat Talipov Avatar answered Oct 26 '25 05:10

Marat Talipov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!