Hi All, Thanks for all of your help. I hadn't posted error messages, because I had tried many different formulations of those functions and each one gave a different error message. However, using your suggestions, I managed to make it work.
You were right in that I only needed to call the constant parameters, c and z. I wanted to define s within the nll() function. Mean and Var are variables that are attached to the workspace in a dataframe. This worked: Predict <- function(Mean,c,z) { v = c*Mean^z return(v) } nll <- function(c,z){ n<-length(Mean) s<-(sum(Var-Predict(Mean,c,z))^2)/n logl<- -0.5*n*log(2*pi) -0.5*n*log(s) - (1/(2*s))*sum((Var-Predict(Mean,c,z))^2) return(-logl) } Mean <- rexp(130) Var <- runif(130) mle(nll,start=list(c=0.01,z=2.1)) It also worked for my actual data. It gives similar estimates for c and z as nls, which is also a good sign, although, given normal errors, I believe nls and maximum likelihood estimation should give the same parameter estimates. Thanks again. Anita. On Thu, Jul 8, 2010 at 6:48 AM, Ravi Varadhan <rvarad...@jhmi.edu> wrote: > Hi, > > When you report errors please send a reproducible example that will allow > us to better help you. At a minimum, you should cut and paste the actual > call and the error message. > > I think the problem is that you should only have parameters as arguments, > i.e. only c and z should be in the argument list to nll(). > > This works: > > Predict <- function(M,c,z){ > v = c*M^z > return(v) > } > > nll <- function(c,z){ > n<-length(M) > logl<- -.5*n*log(2*pi) -.5*n*log(s) - (1/(2*s))*sum((V-Predict(M,c,z))^2) > return(-logl) > } > > M <- rexp(130) > V <- runif(130) > s <- 200 > > mle(nll,start=list(c=0.01,z=2.1)) > > > mle(nll,start=list(c=0.01,z=2.1)) > > Call: > mle(minuslogl = nll, start = list(c = 0.01, z = 2.1)) > > Coefficients: > c z > 0.48547691 -0.01018601 > > > > Hope this helps, > Ravi. > ____________________________________________________________________ > > Ravi Varadhan, Ph.D. > Assistant Professor, > Division of Geriatric Medicine and Gerontology > School of Medicine > Johns Hopkins University > > Ph. (410) 502-2619 > email: rvarad...@jhmi.edu > > > ----- Original Message ----- > From: Anita Narwani <anitanarw...@gmail.com> > Date: Wednesday, July 7, 2010 9:11 pm > Subject: [R] Using nlm or optim > To: r-help@r-project.org > > > > Hello, > > I am trying to use nlm to estimate the parameters that minimize the > > following function: > > > > Predict<-function(M,c,z){ > > + v = c*M^z > > + return(v) > > + } > > > > M is a variable and c and z are parameters to be estimated. > > > > I then write the negative loglikelihood function assuming normal errors: > > > > nll<-function(M,V,c,z,s){ > > n<-length(Mean) > > logl<- -.5*n*log(2*pi) -.5*n*log(s) - > (1/(2*s))*sum((V-Predict(Mean,c,z))^2) > > return(-logl) > > } > > > > When I put the Mean and Variance (variables with 136 observations) > > into this > > function, and estimates for c,z, and s, it outputs the estimate for the > > normal negative loglikelihood given the data, so I know that this works. > > > > However, I am unable to use mle to estimate the parameters c, z, and > > s. I do > > not know how or where the data i.e. Mean (M) and Variance (V) should > > enter > > into the mle function. I have tried variations on > > > > mle(nll,start=list(c=0.01,z=2.1,s=200)) including > > mle(nll,start=list(M=Mean,V=Var, c=0.01,z=2.1,s=200)) > > > > I keep getting errors and am quite certain that I just have a syntax > > error > > in the script because I don't know how to enter the variables into MLE. > > > > Thanks for your help, > > Anita. > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help@r-project.org mailing list > > > > PLEASE do read the posting guide > > 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.