优化R中的大量参数

时间:2016-02-24 10:50:07

标签: r optimization

我试图在R中线性地优化预测的准确性,并且我很难找到收敛和方便的答案。

我的想法如下:我有一组32个参数,我想优化。使用' rnorm'从正态分布中随机抽取这32个参数。

linCoeff <- rnorm(32,0,5)

(linCoeff与线性系数一样)。

这32个参数按以下方式组合:

myVal <-  (((clSigm*lCoeff[1])+lCoeff[2])*data[,1])+
          (((clSigm*lCoeff[3])+lCoeff[4])*data[,2])+
          (((clSigm*lCoeff[5])+lCoeff[6])*data[,3])+
          (((clSigm*lCoeff[7])+lCoeff[8])*data[,4])+
          (((clSigm*lCoeff[9])+lCoeff[10])*data[,5])+
          (((clSigm*lCoeff[11])+lCoeff[12])*data[,6])+
          (((clSigm*lCoeff[13])+lCoeff[14])*data[,7])+
          (((clSigm*lCoeff[15])+lCoeff[16])*data[,8])+
          (((clSigm*lCoeff[17])+lCoeff[18])*data[,9])+
          (((clSigm*lCoeff[19])+lCoeff[20])*data[,10])+
          (((clSigm*lCoeff[21])+lCoeff[22])*data[,11])+
          (((clSigm*lCoeff[23])+lCoeff[24])*data[,12])+
          (((clSigm*lCoeff[25])+lCoeff[26])*data[,13])+
          (((clSigm*lCoeff[27])+lCoeff[28])*data[,14])*data$indDV1+
          (((clSigm*lCoeff[29])+lCoeff[30])*data[,15])*data$indDV2+
          ((clSigm*lCoeff[31])+lCoeff[32])

我所拥有的地方:

  • clSigm,这是一个固定参数;

  • data [,i],这是我想要总结的data.frame上的值。

它最终具有16个元素之和的形式,它给出了一个数值:&#39; myVal&#39;。 然后我应用激活函数,它给出了

  • -1如果&#39; myVal&#39;是&gt; 0和
  • +1如果&#39; myVal&#39;是&lt; 0。

然后我将它与我的输入(这是-1和+1的列表)进行比较并输出平衡准确度。

我想线性优化这32个参数以找到最大的BACC,但是使用传统的R方法并没有给出正确的结果,因为我从不收敛...

举个例子,我给予optim的函数是:

retrieveVal <- function(lCoeff,data){
  clSigm <- 1/(1+exp(.5-(data$acc)))
  myVal <- (((clSigm*lCoeff[1])+lCoeff[2])*data[,1])+
          (((clSigm*lCoeff[3])+lCoeff[4])*data[,2])+
          (((clSigm*lCoeff[5])+lCoeff[6])*data[,3])+
          (((clSigm*lCoeff[7])+lCoeff[8])*data[,4])+
          (((clSigm*lCoeff[9])+lCoeff[10])*data[,5])+
          (((clSigm*lCoeff[11])+lCoeff[12])*data[,6])+
          (((clSigm*lCoeff[13])+lCoeff[14])*data[,7])+
          (((clSigm*lCoeff[15])+lCoeff[16])*data[,8])+
          (((clSigm*lCoeff[17])+lCoeff[18])*data[,9])+
          (((clSigm*lCoeff[19])+lCoeff[20])*data[,10])+
          (((clSigm*lCoeff[21])+lCoeff[22])*data[,11])+
          (((clSigm*lCoeff[23])+lCoeff[24])*data[,12])+
          (((clSigm*lCoeff[25])+lCoeff[26])*data[,13])+
          (((clSigm*lCoeff[27])+lCoeff[28])*data[,14])*data$indDV1+
          (((clSigm*lCoeff[29])+lCoeff[30])*data[,15])*data$indDV2+
          ((clSigm*lCoeff[31])+lCoeff[32])
  act <- c(lapply(myVal,FUN=activate))
  return(-BACC(inp,act))
}

然后:

optim(par=linCoeff,fn=retrieveVal,data=myData)

如果有人可以在这里提供帮助,我全都听到了!

提前致谢。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用其他优化程序吗?即包rgenoudRcppDE通常优于optim。我自己会用这些软件包为你做一些测试,但是因为你省略了data的任何值,所以这个例子不是真正可重复的。