我正在尝试使用R中的optim()函数来解决一个简单的问题,但我在如何实现它时面临一些问题:
e=tot_obs/(sum(Var1)+sum(Var2)+sum(Var3)+sum(Var4))
output=(Var1+Var2+Var3+Var4)*e
我知道所有观察结果和所有变量。
# Fake datasets
# Considering that this are the observations c(1000,250,78,0,0,90)
#Known data
total_observations=1418
var1=c(1,0.3,0.5,0.01,0.05,0.6)
var2=c(500,40,40,0,0,100)
var3=c(1,0.1,0.2,0,0.1,0)
var4=c(2,0.04,0.003,0.003,0,0.05)
#Function
e=total_observations/(sum(var1)+sum(var2)+sum(var3)+sum(var4))
output=(var1+var2+var3+var4)*e
我可以在观察和输出之间做一个简单的相关,结果很好(~0.90)。这个给了我0.97。
但现在我想测试为每个变量分配不同权重的效果。
e=tot_obs/(sum(w1*Var1)+sum(w2*Var2)+sum(w3*Var3)+sum(w4*Var4))
output=(w1*Var1+w2*Var2+w3*Var3+w4*Var4)*e
where w1+w2+w3+w4=1
and cor(observations,output)~1
我试图使用optim()函数,但是我完全迷失了。如果有人可以帮助我,或者给我一些关于如何做到的好参考,我将不胜感激。
答案 0 :(得分:3)
您需要在包solnp
中使用函数Rsolnp
,因为它允许基于相等的约束。
我们的想法是构建一个函数来最小化约束的等式函数。
Fun <- function(param){
e <- total_observations/(sum(param[1]*var1)+sum(param[2]*var2)+sum(param[3]*var3)+sum(param[4]*var4))
output <- (param[1]*var1 + param[2]*var2 + param[3]*var3 + param[4]*var4)/e
-cor(output, observations) #We want to maximize cor and therefore minimize -cor
}
eqn <- function(param){sum(param)}
使用您的示例数据:
observations <- c(1000,250,78,0,0,90)
total_observations=1418
var1=c(1,0.3,0.5,0.01,0.05,0.6)
var2=c(500,40,40,0,0,100)
var3=c(1,0.1,0.2,0,0.1,0)
var4=c(2,0.04,0.003,0.003,0,0.05)
您的优化:
solnp(c(.1,.2,.3,.4),fun=Fun, eqfun=eqn, eqB=1)
Iter: 1 fn: -0.9793 Pars: 0.1395748 0.0008403 0.3881053 0.4714796
Iter: 2 fn: -0.9793 Pars: 0.1395531 0.0008406 0.3881409 0.4714653
solnp--> Completed in 2 iterations
$pars
[1] 0.1395530843 0.0008406453 0.3881409239 0.4714653466
$convergence
[1] 0
$values
[1] -0.9729894 -0.9793458 -0.9793458
$lagrange
[,1]
[1,] 2.521018e-06
$hessian
[,1] [,2] [,3] [,4]
[1,] 0.4843670 5.0498894 -0.08329380 0.39560040
[2,] 5.0498894 699.5317385 -2.38763807 -0.65610831
[3,] -0.0832938 -2.3876381 0.91837245 -0.09486495
[4,] 0.3956004 -0.6561083 -0.09486495 0.43979850
$ineqx0
NULL
$nfuneval
[1] 709
$outer.iter
[1] 2
$elapsed
Time difference of 0.2371149 secs
如果将其保存到变量res
中,那么您要查找的内容将存储在res$pars
中。