R DEoptim()函数:如何选择要优化的参数?

时间:2014-03-21 16:56:22

标签: r mathematical-optimization

我希望估算以下示例的参数:

但是如何才能使DEoptim只优化3个参数中的2个呢? - 有直接的方法吗?

rm(list=ls())
t <- seq(0.1,20,length=100)

Hobs <- 20 + 8*exp(-0.05*t)
Hsim <- function(p,t) {p[1] + p[2]*exp(-p[3]*t)}
upper <- c(30,10,1)
lower <- -upper

resFun <- function(p, t, Hobs) {
          r <- Hobs - Hsim(p,t)
          return(t(r)%*%r)
}

DEoptim(resFun, lower, upper, Hobs = Hobs, t = t, 
                      DEoptim.control(NP = 80, itermax = 200, F = 1.2, CR = 0.7, trace =FALSE))

我在下面找到了一个解决方法。但是有更优雅的方式吗?

rm(list=ls())
t <- seq(0.1,20,length=100)

Hobs <- 20 + 8*exp(-0.05*t)
#Hkorr <- rnorm(100,0,0.2)
Hsim <- function(p,t) {p[1] + p[2]*exp(-p[3]*t)}
upper <- c(20,10,1)
lower <- c(1,1,0.001)
sel = c(0,1,1)
ini = c(20,NA,NA)
# correct upper and lower to selected parameters
upper <- upper[which(sel==1)]
lower <- lower[which(sel==1)]

resFun <- function(par,t,Hobs, sel, ini) {
          p <- rep(NA,3)
          p[which(sel == 0)] <- ini[which(sel==0)]
          p[which(sel == 1)] <- par
          r <- Hobs - Hsim(p,t)
          return(t(r)%*%r)
}
DEoptim(resFun, lower, upper, Hobs = Hobs, t = t, sel = sel, ini = ini,
        DEoptim.control(NP = 80, itermax = 200, F = 1.2, CR = 0.7, trace =FALSE))

1 个答案:

答案 0 :(得分:4)

您可以将upperlower参数值设置为相等,以保持您想要保持不变的参数。

# Hold the first parameter constant at 20
opt <- DEoptim(resFun, c(20, -10, -1), c(20, 10 ,1), Hobs=Hobs, t=t,
               DEoptim.control(NP=80, itermax=200, F=1.2, CR=0.7))