[R] Optim

Ravi Varadhan RVaradhan at jhmi.edu
Thu Feb 12 15:47:35 CET 2009

The problem seems to be the fact that your mixture components are not
well-separated for your data, i.e. your data does not suggest two
components.  The third parameter, mixture proportion, converges to 1 (or 0)
from any sensible starting value.  



Ravi Varadhan, Ph.D.

Assistant Professor, The Center on Aging and Health

Division of Geriatric Medicine and Gerontology 

Johns Hopkins University

Ph: (410) 502-2619

Fax: (410) 614-9625

Email: rvaradhan at jhmi.edu

Webpage:  http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html



-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of mouradster
Sent: Thursday, February 12, 2009 12:54 AM
To: r-help at r-project.org
Subject: [R] Optim

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
below is the code:
## data

### initial values
pars=c(392.25, 507.25,   0.80)
### Objective function
mix.obj = function(p,x)
 + (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:
Sent from the R help mailing list archive at Nabble.com.

R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list