This is only a guess because I don't have your data:

sigma is must be positive in the dnorm function.  My guess is that optim may
attempt an iteration with a negative sigma.

You may want to see help(optim) for dealing with this constraint.
 Specifically see the lower  argument.

If you specify the constraint on sigma, you'll probably still get the same
answer as before, without the warnings.

-tgs

2010/9/9 Zhang,Yanwei <yanwei.zh...@cna.com>

> Dear all,
>
> I ran into problems with the function "optim" when I tried to do an mle
> estimation of a simple lognormal regression. Some warning message poped up
> saying NANs have been produced in the optimization process. But I could not
> figure out which part of my code has caused this. I wonder if anybody would
> help. The code is in the following and the data is in the attachment.
>
>
> da <- read.table("da.txt",header=TRUE)
>
> # fit with linear regression using log transformation of the response
> variable
> fit <- lm(log(yp) ~ as.factor(ay)+as.factor(lag),data=da)
>
> # define the log likelihood to be maximized over
> llk.mar <- function(parm,y,x){
>        # parm is the vector of parameters
>        # the last element is sigma
>        # y is the response
>        # x is the design matrix
>        l <- length(parm)
>        beta <- parm[-l]
>        sigma <- parm[l]
>        x <- as.matrix(x)
>        mu <- x %*% beta
>        llk <- sum(dnorm(y, mu, sigma,log=TRUE))
>        return(llk)
> }
>
> # initial values
> parm <- c(as.vector(coef(fit)),summary(fit)$sigma)
> y <- log(da$yp)
> x <- model.matrix(fit)
>
> op <- optim(parm, llk.mar, y=y,x=x,control=list(fnscale=-1,maxit=100000))
>
>
> After running the above code, I got the warning message:
> Warning messages:
> 1: In dnorm(x, mean, sd, log) : NaNs produced
> 2: In dnorm(x, mean, sd, log) : NaNs produced
>
>
> I would really appreciate if anybody would help to point out the problem
> with this code or tell me how to trace it down (using "trace"?)?
> Many thanks in advance.
>
>
>
>
>
>
>
> Wayne (Yanwei) Zhang
> Statistical Research
> CNA
>
>
>
>
>
> NOTICE:  This e-mail message, including any attachments and appended
> messages, is for the sole use of the intended recipients and may contain
> confidential and legally privileged information.
> If you are not the intended recipient, any review, dissemination,
> distribution, copying, storage or other use of all or any portion of this
> message is strictly prohibited.
> If you received this message in error, please immediately notify the sender
> by reply e-mail and delete this message in its entirety.
>
> ______________________________________________
> R-help@r-project.org 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.
>
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org 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.

Reply via email to