# [R] Minimizing a Function with three Parameters

voodooochild@gmx.de voodooochild at gmx.de
Thu Dec 1 15:55:21 CET 2005

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