I have a complicated combined model for which I can define a likelihood in a function, and I need to optimize the parameters. Problem is, the parameters go all directions if not restricted. Hence, I need to implement a restriction on the parameters, and the one proposed by the professor is that the sum of squared parameter values should equal 1.
I've been playing around with both the optim()
and nlm()
function, but I can't really get what I want. First idea was to use n-1 parameters and calculate the last one from the rest, but this doesn't work (as expected).
To illustrate, some toy data and function reflecting the core problem of what I want to achieve:
dd <- data.frame(
X1=rnorm(100),
X2=rnorm(100),
X3=rnorm(100)
)
dd <- within(dd,Y <- 2+0.57*X1-0.57*X2+0.57*X3+rnorm(100,0,0.2))
myfunc2 <- function(alpha,dd){
alpha <- c(alpha,sqrt(1-sum(alpha^2)))
X <- as.matrix(dd[,-4]) %*% alpha
m.mat <- model.matrix(~X)
mod <- glm.fit(m.mat,dd$Y)
Sq <- sum(resid(mod)^2)
return(Sq)
}
b <- c(1,0)
optim(b,myfunc2,dd=dd)
This results obviously in :
Error: (subscript) logical subscript too long
In addition: Warning message:
In sqrt(1 - sum(alpha^2)) : NaNs produced
Anybody an idea on how to implement restrictions on parameters in optimization processes?
PS: I am aware of the fact that this example code doesn't make sense at all. It's just for demonstration purposes.
Edit : Solved it! - See Mareks answer.