Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: maximum likelihood no observations error


From   andylaustata <andylaustata@126.com>
To   statalist@hsphsun2.harvard.edu
Subject   st: maximum likelihood no observations error
Date   Tue, 12 Mar 2013 16:41:16 +0800 (CST)

Dear Statalist,

Learning from Poi (2002)'s QUAIDS ml evaluator, I come up with my own ml evaluator for a new demand system that I'm currently working on.

But, while I do the ml check, it always gets stuck at Test 7 and reports no observations error (i.e. error 2000). I'm copying the code here. If anyone could give me some help, I would appreciate it very much. 

By the way, if you need some data to try this ml evaluator, the food data used in Poi (2002) might be a good choice, so basically, you just type "use food, clear".

Cheers,
Ou

program define RDS1

   version 11.1
   args todo b lnf
   //wi: shares; pi: prices; expfd: total expenditure   
   quietly{
   
          tempname gammasum tausum etasum alpha_v beta_v theta_v gamma_v tau_v eta_v alpha beta theta gamma tau eta
	  tempvar P1 P2 P3 R1 R2 R3
	  
	  local nm = $NEQN-1
	  
          scalar `alpha_v' = `b'[1,1]
	  scalar `beta_v' = `b'[1,2]
	  scalar `theta_v' = `b'[1,3]
	  matrix `gamma_v' = `b'[1,(3+1)..(3+`nm')]
	  matrix `tau_v' = `b'[1,(3+`nm'+1)..(3+2*`nm')]
	  matrix `eta_v' = `b'[1,(3+2*`nm'+1)..(3+3*`nm')]
	  
	  scalar `alpha' = exp(`alpha_v')
	  scalar `beta' = invlogit(`beta_v')
	  scalar `theta' = invlogit(`theta_v')
	  matrix `gamma' = J(1,$NEQN,1)
	  matrix `tau' = J(1,$NEQN,1)
	  matrix `eta' = J(1,$NEQN,1)
	  
	  scalar `gammasum' = 0 //the sum in the reparameterization of gamma
	  forvalues i = 1/`nm' {
	     scalar `gammasum' = `gammasum' + exp(`gamma_v'[1,`i'])
	  }
	  
	  matrix `gamma'[1,1] = 1/(1+`gammasum')
	  forvalues i = 2/$NEQN {
	     matrix `gamma'[1,`i'] = exp(`gamma_v'[1,(`i'-1)])/(1+`gammasum')
	  }
	  
	  scalar `tausum' = 0 //the sum in the reparameterization of tau
	  forvalues i = 1/`nm' {
	     scalar `tausum' = `tausum' + exp(`tau_v'[1,`i'])
	  }
	  
	  matrix `tau'[1,1] = 1/(1+`tausum')
	  forvalues i = 2/$NEQN {
	     matrix `tau'[1,`i'] = exp(`tau_v'[1,(`i'-1)])/(1+`tausum')
	  }
	  
	  
	  scalar `etasum' = 0 //the sum in the reparameterization of eta
	  forvalues i = 1/`nm' {
	     scalar `etasum' = `etasum' + exp(`eta_v'[1,`i'])
	  }
	  
	  matrix `eta'[1,1] = 1/(1+`etasum')
	  forvalues i = 2/$NEQN {
	     matrix `eta'[1,`i'] = exp(`eta_v'[1,(`i'-1)])/(1+`etasum')
	  }
	  
	  /* Form the price index variable P1.*/
	  gen double `P1' = p1^`gamma'[1,1]
	  forvalues i = 2/$NEQN{
	     replace `P1' = `P1'*(p`i'^`gamma'[1,`i'])
	  }
	  
	  /* Form the price index variable P2.*/
	  gen double `P2' = p1^`tau'[1,1]
	  forvalues i = 2/$NEQN{
	     replace `P2' = `P2'*(p`i'^`tau'[1,`i'])
	  }
	  
	  /* Form the price index variable P3.*/
	  gen double `P3' = p1*`eta'[1,1]
	  forvalues i = 2/$NEQN{
	     replace `P3' = `P3'+(p`i'*`eta'[1,`i'])
	  }
	  
	  /*gen R1*/
	  gen double `R1' = `alpha'*expfd^`alpha'*`P1'^(-`alpha'-1)
	  
	  /*gen R2*/
	  gen double `R2' = `beta'*expfd^(-`beta')*`P2'^(`beta'-1)
	  
	  /*gen R3*/
	  gen double `R3' = `theta'*expfd^(-`theta')*`P3'^(`theta'-1)
	  
	  /*Now generate the error terms.*/
	  forvalues i = 1/`nm' {
	     tempvar lnl_eps`i'
		 gen double `lnl_eps`i'' = w`i' - (`gamma'[1,`i']*`R1'*`P1'+`tau'[1,`i']*`R2'*`P2'+`eta'[1,`i']*`R3'*p`i')/(`R1'*`P1'+`R2'*`P2'+`R3'*`P3')
	  }
	  
	  local allofem ""
	  forvalues i = 1/`nm' {
	     local allofem "`allofem' `lnl_eps`i''"
	  }
	  
	  /*Form sigma.*/
	  matrix accum sigma = `allofem', noconstant
	  local nobs = r(N)
	  matrix sigma = sigma/`nobs'
	  
	   /* Finally, compute the likelihood function. */
      scalar `lnf' = -1*`nobs'/2*(`nm'*(1+ln(2*_pi)) + ln(det(sigma)))
   }
   
end


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index