Toby - 1 year ago 83

R Question

You'll need the

`DEoptim`

`install.packages("DEoptim")`

library(DEoptim)

From the

`help(DEoptim)`

`Rosenbrock <- function(x){`

x1 <- x[1]

x2 <- x[2]

100 * (x2 - x1 * x1)^2 + (1 - x1)^2

}

lower <- c(-10,-10)

upper <- -lower

## run DEoptim and set a seed first for replicability

set.seed(1234)

DEoptim(Rosenbrock, lower, upper)

Now this works fine:

However, if I provide an initial population

`Npop`

`dim(Npop)[1] < 10*length(lower)`

`xini <- cbind(runif(10),runif(10))`

ran <- abs(lower - upper)

Npop <- apply(xini,2,function(x) x*ran+lower)

DEoptim(Rosenbrock, lower, upper,DEoptim.control(initialpop = Npop))

I get the following error:

`Error in DEoptim(Rosenbrock, lower, upper, DEoptim.control(initialpop = Npop)) :`

Initial population is not a matrix with dim. NP x length(upper).

How can I do this?

Related to this question: R DEoptim() function: How to select parameters to be opimised?

where e.g. 1 parameter is not estimated and I do not have to account for it in the

`DEoptim.control(initialpop)`

Answer Source

Here's the workaround I got from the author of `DEoptim()`

```
library(DEoptim)
#From help(DEoptim)
Rosenbrock <- function(x){
x1 <- x[1]
x2 <- x[2]
100 * (x2 - x1 * x1)^2 + (1 - x1)^2
}
lower <- c(-10,-10)
upper <- -lower
## run DEoptim and set a seed first for replicability
set.seed(1234)
DEoptim(Rosenbrock, lower, upper)
n <- length(lower) * 10
# n <- length(lower) * 5
xini <- cbind(runif(n),runif(n))
ran <- upper - lower
Npop <- as.matrix(apply(xini,2,function(x) x*ran+lower))
DEoptim(Rosenbrock, lower, upper, DEoptim.control(NP=n,initialpop = Npop))
```