Reputation: 2425
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.
Upvotes: 4
Views: 1891
Reputation: 13304
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.
Upvotes: 4