[R] integration within maximum likelihood

Cristian Angelo Guevara crguevar at mit.edu
Fri Dec 5 00:29:14 CET 2008


Hi:

I'm trying to estimate a latent variable model in mnl discrete choice
framework using R. I need to do first a uni dimensional integral
within each observation (row) in the database and then sum over
observations. I'm stacked in the point shown below. Apparently I have
a dimensionality problem in the definition of the integral. Maybe it
does not identify that what I need is only one row at a time. I'm no
sure.

I would appreciate any kind of help. Below is my code and the error report.

Thanks
Angelo


> setwd("C:/Users/Angelo/Documents/PhD/Montecarlo TVI")
> DATA<-read.table("Data.csv",header=TRUE,sep=",")
> mnl.lik<-function(theta,y){
+ th1<-theta[1]
+ th2<-theta[2]
+ tha<-theta[3]
+ thb<-theta[4]
+ thc<-theta[5]
+ thp<-theta[6]
+ thmu<-theta[7]
+ alfz<-theta[8]
+ alfp<-theta[9]
+ mu1<- alfz*y$z_a1 + alfp*y$p1
+ mu2<- alfz*y$z_a2 + alfp*y$p2
+ mu3<- alfz*y$z_a3 + alfp*y$p3
+ U1<- th1 +tha*y$a1 +thb*y$b1 +thc*y$c1  +thp*y$p1 +thmu*mu1
+ U2<- th2 +tha*y$a2 +thb*y$b2 +thc*y$c2  +thp*y$p2 +thmu*mu2
+ U3<-     +tha*y$a3 +thb*y$b3 +thc*y$c3  +thp*y$p3 +thmu*mu3
+ Usum<- (exp(U1)+exp(U2)+exp(U3))
+ arg1<-  function(mu1) {(exp(U1)/Usum)*dnorm(mu1- alfz*y$z_a1 -alfp*y$p1)}
+ arg2<-  function(mu2) {(exp(U2)/Usum)*dnorm(mu2- alfz*y$z_a2 -alfp*y$p2)}
+ arg3<-  function(mu3) {(exp(U3)/Usum)*dnorm(mu3- alfz*y$z_a3 -alfp*y$p3)}
+ int1<- integrate(arg1,-Inf,Inf)
+ int2<- integrate(arg2,-Inf,Inf)
+ int3<- integrate(arg3,-Inf,Inf)
+ logl<-sum(y$Ch1*int1$value+ y$Ch2*int2$value + y$Ch3*int3$value)
+ return(-logl)
+ }
>
> p<-optim(c(0,0,0,0,0,0,0,0,0),mnl.lik,y=DATA,method="BFGS",hessian=T)
Error in integrate(arg1, -Inf, Inf) :
  evaluation of function gave a result of wrong length
Calls: optim -> <Anonymous> -> fn -> integrate -> .External
In addition: Warning message:
In mu1 - alfz * y$z_a1 :
  longer object length is not a multiple of shorter object length
Execution halted



More information about the R-help mailing list