I'm working on a non-linear optimization, with the constrOptim.nl
from the alabama
package. However, my problem is more related to passing arguments (and the dot-dot-dot (ellipis/"...") and maybe do.call
)- so I give first a general example and later refer to the constrOptim.nl
function.
Suppose, I have the following functions - from which I only can edit the second
and third
but not the first
.
first<-function (abc, second, third, ...){
second(abc,...)
third(abc,...)
}
second<- function(abc, ttt='nothing special'){
print(abc)
print(ttt)
}
third<- function(abc, zzz="default"){
print(abc)
print(zzz)
}
The output I want is the same I would get when I just run
second("test", ttt='something special')
third("test", zzz="non-default")
This is
"test"
"something special"
"test"
"non-default"
However, the code below doesn't work to do this.
first("test",second=second, third=third, ttt='something special',zzz="non-default")
How can I change the call or the second
and third
function to make it work?
http://www.r-bloggers.com/r-three-dots-ellipsis/
here I found some advice that do.call
could help me but at the moment I'm not capable of understanding how it should work.
I cannot change the first
function since this is the constrOptim.nl
in my particular problem - and it is designed to be capable of passing more arguments to different functions. However, I can change the second
and third
function - as they are the restrictions and the function that I want to minimize. Obviously I can also change the call of the function.
So to be more particular, here is my specific problem: I perform a maximum likelihood estimation with non-linear restrictions:
minimize <- function(Param,VARresiduals){
#Blahblah
for (index in 1:nrow(VARreisduals)){
#Likelihood Blahbla
}
return(LogL)
}
heq<-function(Param,W){
B<-Param[1:16]
restriction[1]<-Lrestriction%*%(diag(4)%x%(solve(W))%*%as.vector(B))
restriction[2:6]<-#BlablaMoreRestrictions
return(restriction)
}
Now I call the constrOptim.nl
...
constrOptim.nl(par=rnorm(20), fn=minimize,hin=NULL heq=heq,VARresiduals,W)
...but get the same error, as I receive when I call the first
function above - something like: "Error in second(abc, ...) : unused argument (zzz = "non-default")".
How can I change minimize
and heq
or the call? :) Thanks in Advance
Update after the post got marked as a duplicate:
The answer to the related post changes the first
function in my example - as it implements a do.call there, that calls the other functions. However, I cannot change the first function in my example as I want to keep the constrOptim.nl
working a variety of different functions. Is there another way?
The solution I came up with is not very elegant but it works.
second_2<- function(abc, extras){
a<-extras[[1]]
print(abc)
print(a)
}
third_2<- function(abc, extras){
a<-extras[[2]]
print(abc)
print(a)
}
extras<-list()
extras[[1]]<-'something special'
extras[[2]]<-"non-default"
extras
first("test",second=second_2, third=third_2, extras)
surprisingly also the following code works, but with a slightly different outcome
first("test",second=second, third=third, extras)
after all, setting default values is now a little clumsy but not infeasible.