[R] R:Maximum likelihood estimation using BHHH and BFGS

chao gai chaogai at duineveld.demon.nl
Mon Apr 9 11:44:59 CEST 2007


Joey,

First of all, it is bad habit to call your data c. It can be done, but it may 
confuse you later on. Let's call it consdata.
My only experience is with optim.
In that case I would change fn a bit. I let scoping take care of consdata. 
Alternatively, pass it as last parameter:

fn<-function(psi,alpha) {
s1<-sum(for(i in 1:n){(consdata[i]-(psi^(-1/alpha)*(lag(consdata[i],-1))))^2*
(lag(consdata[i],-1)^((-2)*(alpha+1)))})
s2<- sum(for(m in 1:n){log(lag(consdata[m],-1)^(((2)*alpha)+2))})
constant-(74*s1)-(1/2)*s2
}

optim(c(1,0),fn, method="BFGS")

Alternatively, in case you want to pass the data explicitly, use the ... to 
pass the data
 fn < function(psi,alpha,consdata) {}
optim(c(1,0),fn, method="BFGS",consdata=consdata)

Kees


On Monday 09 April 2007 04:59, joey repice wrote:
> Dear R users,
>
> I am new to R. I would like to find *maximum likelihood estimators for psi
> and alpha* based on the following *log likelihood function*, c is
> consumption data comprising 148 entries:
>
> fn<-function(c,psi,alpha)
>
> {
>
> s1<-sum(for(i in 1:n){(c[i]-(psi^(-1/alpha)*(lag(c[i],-1))))^2*
>
> (lag(c[i],-1)^((-2)*(alpha+1))
>
> )});
>
> s2<- sum(for(m in 1:n){log(lag(c[m],-1)^(((2)*alpha)+2))});
>
> constant-(74*s1)-(1/2)*s2}
>
>
>
> In particular I want to use the BHHH and the BFGS algorithms. I have found
> the following packages that can be used for the task:  *mle{stats4},
> optim{stats}, maxBHHH{micEcon}, maxBFGS{micEcon}*. I have documented some
> of my attempts below ((a) package name (b) usage (c) my attempt and
> corresponding error). In all humility I apologise for any bad coding, and
> ask if anyone can *direct me in finding these estimators*.
>
>
>
> Yours sincerely.
>
>
>
>
>
> *(1a)  mle{stats4}
>
> (b)  Usage:
>
>      mle(minuslogl, start = formals(minuslogl), method = "BFGS",
>
>      fixed = list(), ...)
>
> (For this I use the negative of the log-likelihood function,bn)*
>
> *(c)  >mle(start=list(psi=1,alpha=0),fn, method="BFGS",fixed=list(c=c))*
>
> Error in optim(start, f, method = method, hessian = TRUE, ...) :
>
>         non-finite finite-difference value [1]
>
>
>
> *(2a)  optim{stats}
>
>
>
> (b)  optim(par, fn, gr = NULL,
>
>       method = c("Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN"),*
>
> *
>
>       lower = -Inf, upper = Inf,
>
>       control = list(), hessian = FALSE, ...)
>
>
>
> (c)  > optim(c(c,1,0),fn, method="BFGS", control=list(c=c))*
>
> Error: argument "psi" is missing, with no default
>
>
>
> *(3a)     maxBHHH{micEcon}
>
> (b)     Usage: maxBHHH(fn, grad = NULL, hess=NULL, theta, print.level
> = 0, iterlim = 100,   ...)*
>
> * *
>
> *(c) > maxBHHH(fn(c,psi,alpha),theta=list(psi=1,alpha=0))*
>
>  Error in psi^(-1/alpha) * (lag(c[i], -1)) :
>
>         time-series/vector length mismatch
>
>
>
> (4a)  maxBFGS{micEcon}
>
>
>
> (b)  Usage: maxBFGS(fn, grad = NULL, theta, print.level = 0, iterlim = 200,
>
>      tol = 1e-06, ... )
>
> *(ci) >  maxBFGS(fn(c,psi,alpha),theta=list(psi=1,alpha=0))*
>
> Error in psi^(-1/alpha) * (lag(c[i], -1)) :
>
>         time-series/vector length mismatch
>
>
>
> *(cii)> maxBFGS(fn,theta=list(psi=1,alpha=0))*
>
> Error: argument "psi" is missing, with no default
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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 and provide commented, minimal,
> self-contained, reproducible code.



More information about the R-help mailing list