# [R] Optimization and simulation

From: Jin Huang <crystal_huangjin_at_yahoo.com.cn>
Date: Wed 04 Apr 2007 - 01:30:25 GMT

Dear all,

I would need to maximize a self-defined 'target' function(see below) with respect to theta, where v follows a log-normal distribution with mean 'mu(x)' and a constant variance. For each v drawn from its distribution, one maximized value and optimal theta are produced. I'd like to do this repeatedly and store the maximized value and corresponding theta. I wrote the following code that can produce a result. But the problem is that the result doesn't seem to be the optimized one because if I arbitrarily choose theta=c(4,27), I will get much bigger value than those simulated ones. I am not sure where is wrong. Could anyone help me with this? Thank you in advance! Here is the code:

rdt<-matrix(0,10,3)
for (i in 1:10){
#Define the target function
target<-function(theta){
d0=theta[1]
T0=theta[2]
mu<-function(x,d=d0,ta=23.86,ti=11.067,ppt=1.321,a=-12.31, S=5.62, L=3.83, b=c(0.338,-0.0055,0.113,-0.00466,-0.008,-0.205,-0.044,0.266,1.719,-0.169)){ return(a+sum(b*c(x,x^2,d,d^2,S,L,ta,ti,ppt,ti*ppt)))}

```  v<-function(x,d=d0){
return(rlnorm(1,mean=mu(x),sd=0.835^0.5))}
p<-function(x,d=d0){
```

if(8.179+0.00023*(5.29+0.16*x-0.21*d)^5>45){return(45)} return(8.179+0.00023*(5.29+0.16*x-0.21*d)^5)
}

Y3<-function(x,d=d0){
return(p(x,d)*v(x,d))
}

r<-Y3(T0)
return(r)
}
```  result<-optim(c(2,10),target,lower=c(0.1,0.5),upper=c(66,50),
method="L-BFGS-B",control=list(maxit=100000,fnscale=-1))
rdt[i,1]<-result\$value
```

rdt[i,2:3]<-result\$par }

[[alternative HTML version deleted]]

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed Apr 04 11:40:34 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 04 Apr 2007 - 04:30:54 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.