Re: [R] Minimizing a Function with three Parameters

From: Sundar Dorai-Raj <sundar.dorai-raj_at_pdf.com>
Date: Fri 02 Dec 2005 - 02:01:32 EST

voodooochild@gmx.de wrote:
> Hi,
>
> I'm trying to get maximum likelihood estimates of \alpha, \beta_0 and
> \beta_1, this can be achieved by solving the following three equations:
>
> n / \alpha + \sum\limits_{i=1}^{n} ln(\psihat(i)) -
> \sum\limits_{i=1}^{n} ( ln(x_i + \psihat(i)) ) = 0
>
> \alpha \sum\limits_{i=1}^{n} 1/(psihat(i)) - (\alpha+1)
> \sum\limits_{i=1}^{n} ( 1 / (x_i + \psihat(i)) ) = 0
>
> \alpha \sum\limits_{i=1}^{n} ( i / \psihat(i) ) - (\alpha + 1)
> \sum\limits_{i=1}^{n} ( i / (x_i + \psihat(i)) ) = 0
>
> where \psihat=\beta_0 + \beta_1 * i. Now i want to get iterated values
> for \alpha, \beta_0 and \beta_1, so i used the following implementation
>
> # first equation
> l1 <- function(beta0,beta1,alpha,x) {
> n<-length(x)
> s2<-length(x)
> for(i in 1:n) {
> s2[i]<-log(beta0+beta1*i)-log(x[i]+beta0+beta1*i)
> }
> s2<-sum(s2)
> return((n/alpha)+s2)
> }
>
> # second equation
> l2 <- function(beta0,beta1,alpha,x) {
> n<-length(x)
> s1<-length(x)
> s2<-length(x)
> for(i in 1:n) {
> s1[i]<-1/(beta0+beta1*i)
> s2[i]<-1/(beta0+beta1*i+x[i])
> }
> s1<-sum(s1)
> s2<-sum(s2)
> return(alpha*s1-(alpha+1)*s2)
> }
>
> #third equation
> l3 <- function(beta0,beta1,alpha,x) {
> n<-length(x)
> s1<-length(x)
> s2<-length(x)
> for(i in 1:n) {
> s1[i]<-i/(beta0+beta1*i)
> s2[i]<-i/(x[i]+beta0+beta1*i)
> }
> s1<-sum(s1)
> s2<-sum(s2)
> return(alpha*s1-(alpha+1)*s2)
> }
>
> # all equations in one
> gl <- function(beta0,beta1,alpha,x) {
> l1(beta0,beta1,alpha,x)^2 + l2(beta0,beta1,alpha,x)^2 +
> l3(beta0,beta1,alpha,x)^2
> }
>
> #iteration with optim
> optim(c(1,1,1),gl,x)
>
> i get always an error massage. Is optim anyway the 'right' method to get
> all three parameters iterated at the same time?
>
> best regards
> Andreas
>

Hi, Andreas,

You've misread the help file for ?optim.

  fn: A function to be minimized (or maximized), with first

           argument the vector of parameters over which minimization is
           to take place.  It should return a scalar result.

So, your objective function should look like

gl <- function(beta, x) {

   beta0 <- beta[1]
   beta1 <- beta[2]
   alpha <- beta[3]
   v1 <- l1(beta0, beta1, alpha, x)^2

   v2 <- l2(beta0, beta1, alpha, x)^2
   v3 <- l3(beta0, beta1, alpha, x)^2
   v1 + v2 + v3
}

Also, are you aware of ?mle in the stats4 package?

HTH, --sundar



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Dec 02 02:08:09 2005

This archive was generated by hypermail 2.1.8 : Fri 02 Dec 2005 - 04:30:25 EST