[R] Maximum likelihood estimation
Peter Dalgaard
P.Dalgaard at biostat.ku.dk
Fri Aug 15 16:19:16 CEST 2008
Jurica Brajković wrote:
> Hello,
>
> I am struggling for some time now to estimate AR(1) process for commodity price time series. I did it in STATA but cannot get a result in R.
>
> The equation I want to estimate is: p(t)=a+b*p(t-1)+error
> Using STATA I get 0.92 for a, and 0.73 for b.
>
> Code that I use in R is:
> p<-matrix(data$p) # price at time t
> lp<-cbind(1,data$lp) # price at time t-1
>
>
> mle <- function(theta) {
> sigma2<-theta[1]
> b<- theta[-1]
> n<-length(p)
> e<-p-lp%*%b
> logl<- -(n/2)*log(sigma2)-((t(e)%*%e)/(2*sigma2))
> return(-logl)
> }
>
>
> out <- optim(c(0,0,0),mle, method = "L-BFGS-B",
> lower = c(0, -Inf, -Inf),
> upper = c(Inf, Inf, Inf))
>
> The "result" I get is: " Error in optim(c(0, 0, 0), mle, method = "L-BFGS-B", lower = c(0, -Inf,:L-BFGS-B needs finite values of 'fn'"
>
> Can somebody spot the mistake?
>
> Many thanks,
>
> Jurica Brajkovic
>
>
As far as I can see, the first element of the vector of starting values
(sigma2) is 0 and you are taking log of it....
> ______________________________________________
> 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.
>
--
O__ ---- Peter Dalgaard Øster Farimagsgade 5, Entr.B
c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list