I am trying to figure out how to run maximum likelihood in R. Here is my situation: I have the following equation: equation<-(1/LR-(exp(-k*T)*LM)*(1-exp(-k))) LR, T, and LM are vectors of data. I want to R to change the value of k to maximize the value of equation. My attempts at optim and optimize have been unsuccessful. Are these the recommended functions that I should use to maximize my equation? With optim I wanted the function to be maximized so I had to make the fnscale negative. Here is what I put: L<-optim(k,equation,control=(fnscale=-1)) My result: Error: could not find function "fn" Here is what I put for optimize: L<-optimise(equation,k,maximum=TRUE) My result: Error: 'xmin' not less than 'xmax' Any advise would be greatly appreciated. Mike
Do you want to do a nonlinear least-squares estimation (which is MLE if the errors are Gaussian)? If so, you have to define a function that takes the parameter (k) and data matrix (LR, T, LM), as arguments, and returns a scalar, which is the residual sum of squares. Then you can optimize (minimize) that function. Ravi. -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of mlarkin at rsmas.miami.edu Sent: Friday, October 01, 2010 4:40 PM To: r-help at r-project.org Subject: [R] maximum likelihood problem I am trying to figure out how to run maximum likelihood in R. Here is my situation: I have the following equation: equation<-(1/LR-(exp(-k*T)*LM)*(1-exp(-k))) LR, T, and LM are vectors of data. I want to R to change the value of k to maximize the value of equation. My attempts at optim and optimize have been unsuccessful. Are these the recommended functions that I should use to maximize my equation? With optim I wanted the function to be maximized so I had to make the fnscale negative. Here is what I put: L<-optim(k,equation,control=(fnscale=-1)) My result: Error: could not find function "fn" Here is what I put for optimize: L<-optimise(equation,k,maximum=TRUE) My result: Error: 'xmin' not less than 'xmax' Any advise would be greatly appreciated. Mike ______________________________________________ R-help at 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.
Hi: Is equation written as a function? As in, say, eq <- function(k) 1/LR - (exp(-k * T) * LM) * (1 - exp(-k)) I believe the log of the likelihood would be a simpler expression to maximize... On Fri, Oct 1, 2010 at 1:39 PM, <mlarkin@rsmas.miami.edu> wrote:> I am trying to figure out how to run maximum likelihood in R. Here is my > situation: > > I have the following equation: > equation<-(1/LR-(exp(-k*T)*LM)*(1-exp(-k))) >This is a numeric vector, given LR, T, LM and k. If this is supposed to be a function of k, then you have to define it as such. See above.> > LR, T, and LM are vectors of data. I want to R to change the value of k > to maximize the value of equation. > > My attempts at optim and optimize have been unsuccessful. Are these the > recommended functions that I should use to maximize my equation? > > With optim I wanted the function to be maximized so I had to make the > fnscale negative. Here is what I put: > > L<-optim(k,equation,control=(fnscale=-1)) > > My result: Error: could not find function "fn" > > This is because equation is not a function, given the definition youprovided above.> > Here is what I put for optimize: > > L<-optimise(equation,k,maximum=TRUE) > > My result: Error: 'xmin' not less than 'xmax' > > Any advise would be greatly appreciated. >HTH, Dennis Mike> > ______________________________________________ > 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]]
Ravi has already responded about the possibility of using nls(). He and I also have put up the optimx package which allows a control 'maximize=TRUE' because of the awkwardness of using fnscale in optim. (optimx still lets you use optim()'s tools too, but wrapped with this facility.) There are a number of optimizers available through optimx. If you can compute gradients analytically, you will find methods that can use these much more efficient, and your subsequent Hessian estimates better too. You should be aware that optimise() is for 1D problems i.e., 1 parameter to optimize. John Nash> Message: 81 > Date: Fri, 1 Oct 2010 16:39:58 -0400 (EDT) > From: mlarkin at rsmas.miami.edu > To: r-help at r-project.org > Subject: [R] maximum likelihood problem > Message-ID: > <3675.129.171.104.122.1285965598.squirrel at webmail.rsmas.miami.edu> > Content-Type: text/plain;charset=iso-8859-1 > > I am trying to figure out how to run maximum likelihood in R. Here is my > situation: > > I have the following equation: > equation<-(1/LR-(exp(-k*T)*LM)*(1-exp(-k))) > > LR, T, and LM are vectors of data. I want to R to change the value of k > to maximize the value of equation. > > My attempts at optim and optimize have been unsuccessful. Are these the > recommended functions that I should use to maximize my equation? > > With optim I wanted the function to be maximized so I had to make the > fnscale negative. Here is what I put: > > L<-optim(k,equation,control=(fnscale=-1)) > > My result: Error: could not find function "fn" > > > Here is what I put for optimize: > > L<-optimise(equation,k,maximum=TRUE) > > My result: Error: 'xmin' not less than 'xmax' > > Any advise would be greatly appreciated. > Mike > > > > ------------------------------ >