I am trying to maximize the number N_ent through a 1x42 weighting vector (weight).
N_ent
is calculated with the following function:
N_ent <- exp(-sum((((solve(pca$rotation[])) %*% t(weight))^2)*
(pca$sdev^2)/(sum((((solve(pca$rotation[])) %*% t(weight))^2)*
(pca$sdev^2)))*log((((solve(pca$rotation[])) %*% t(weight))^2)*
(pca$sdev^2)/(sum((((solve(pca$rotation[])) %*% t(weight))^2)*(pca$sdev^2))))))
Though it looks quite complicated, the equation works fine and supplies me with N_ent = 1.0967
when equal weights of 0.0238 (1/42 = 0.0238) are used.
Further, none of the weights may be below -0.1 or above 1.
I am new to R have struggled to use both the optim()
(ignoring my constraints) and constrOptim()
functions, encountering the error
Error in match.arg(method) : 'arg' must be of length 1
when optim()
was used and
Error in ui %*% theta : non-conformable arguments
when constrOptim()
was used.
Any help on how to set up the code for such an optimization problem would be greatly appreciated.