[R] Optim

mouradster mouradster at gmail.com
Thu Feb 12 06:54:28 CET 2009

Dear R user I follow the steps defined in Modern applied statistics page(453)
to use optim. However, when I run the following code the parameters seems
way off and the third parameter(p3) stayed as the initial value.
below is the code:
## data

### initial values
pars=c(392.25, 507.25,   0.80)
### Objective function
mix.obj = function(p,x)
	e =p[3]*(pnorm((x+1/2-p[1])/sqrt(p[1]))-pnorm((x-1/2-p[1])/sqrt(p[1])))
 + (1-p[3])*(pnorm((x-1/2-p[2])/sqrt(p[2]))-pnorm((x-3/2-p[2])/sqrt(p[2])))
	if (any(e<=0)) Inf
	else -sum(log(e))

### Calculate the first derivatives of the objective function.
lmix2a = deriv(~
 + (1-p3)*pnorm((x-1/2-p2)/sqrt(p2))-pnorm((x-3/2-p2)/sqrt(p2))),
	   function(x,p1,p2,p3) NULL)

mix.gr = function(p,x)
	p3 = p[3]
	p2 = p[2]
	p1 = p[1]
### optim

Also, sometimes it gives me the following error:
Error in optim(pars, mix.obj, mix.gr, x = da, method = "L-BFGS-B", lower =
rep(0,  : 
  L-BFGS-B needs finite values of 'fn'

Thanks in advance for ur help.

View this message in context: http://www.nabble.com/Optim-tp21970390p21970390.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list