[R] Maximum likelihood estimation
Jurica Brajković
jbrajkovic at eihp.hr
Tue Aug 12 09:32:11 CEST 2008
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
More information about the R-help
mailing list