Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: ML Programming


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: ML Programming
Date   Fri, 7 Jun 2013 00:05:09 +0100

You are feeding five arguments to your program, a numeric zero and
four tempnames. But your -args- statement specifies several arguments.
That's not illegal in itself but the consequence is that the sixth and
subsequent arguments named by -args- are undefined.

-beta2- is the sixth and it is undefined.

This is all there to read in what you show.

  mleval `beta2' = `b', eq(1)

fails because beta2 is undefined and thus evaluated as an empty string.

Nick
[email protected]


On 6 June 2013 19:41, Jonas <[email protected]> wrote:
> Thanks you very much for your feedback!
>
> So far I changes this and I looked in "maximum likelihood estimation with
> stata" (2ed.) on how to write the command to to let stata exercise my
> program.
>
> So far I use :
> ml model d0 ML (beta2: mage fage feduc meduc)(beta3: mage fage feduc meduc)
> /lnsig1 /lnsig2 /lnsig3
>
> If I use ml check, following message occurs.
> . ml check
>
> Test 1:  Calling ML to check if it computes log likelihood and
>          does not alter coefficient vector...
>          FAILED; ML returned error 198.
>
> Here is a trace of its execution:
> ------------------------------------------------------------------------------
> -> ML 0 __000009 __00000A __00000B __00000C
>         - `begin'
>         = capture noisily version 12: ML 0 __000009 __00000A __00000B
> __00000C
>
> -------------------------------------------------------------------------------------------
> begin ML ---
>           - args todo b lnf tempvar beta1 beta2 beta3 alpha1 alpha2 alpha3
> alpha4 alpha5 numer sum denom yhf yhm y
>> sq
>           - tempname random1 random2 random3 L1 sigma1 lnsig1 l11 sigma2
> lnsig2 l22 sigma3 lnsig3 l33
>           - mleval `beta2' = `b', eq(1)
>           = mleval  = __000009, eq(1)
> = invalid name
>
> ---------------------------------------------------------------------------------------------
> end ML ---
>         - `end'
>         = set trace off
> ------------------------------------------------------------------------------
> Fix ML.
> r(198);
>
> Here the new code:
>
>
> sort cupid Hc
> program ML
>     args todo b lnf tempvar beta1 beta2 beta3  alpha1 alpha2 alpha3 alpha4
> alpha5  numer sum denom yhf yhm ysq
>     tempname random1 random2 random3 L1 sigma1 lnsig1 l11 sigma2 lnsig2 l22
> sigma3 lnsig3 l33
>     mleval `beta2' = `b', eq(1)
>     mleval `beta3' = `b', eq(2)
>     mleval `lnsig1' = `b', scalar eq(3)
>     mleval `lnsig2' = `b', scalar eq(4)
>     mleval `lnsig3' = `b', scalar eq(5)
>     qui gen double `L1'=0
>
>     qui gen double `numer'=0
>     qui gen double `sum'=0
>     qui gen double `denom'=0
>     scalar `sigma1'=(exp(`lnsig1'))^2
>     scalar `sigma2'=(exp(`lnsig2'))^2
>     scalar `sigma3'=(exp(`lnsig3'))^2
>     matrix f=(`sigma1' , 0 , 0 \ 0 , `sigma2' , 0 \ 0 , 0 , `sigma3')
>     capture matrix U=cholesky(f)
>     scalar `l11'=U[1,1]
>     scalar `l22'=U[2,2]
>     scalar `l33'=U[3,3]
>     forvalues r=1/50{
>         qui gen double `random1'=random1_`r'*`l11'
>         qui gen double `random2'=random2_`r'*`l22'
>         qui gen double `random3'=random3_`r'*`l33'
>         qui gen double `yhf'=y*fh
>         qui gen double `yhm'=y*hm
>         qui gen double `ysq'=y^2
>
> *------------------------------------------------------------------------------------------------*
>         qui gen double
> `utility'=`alpha1'*`ysq'+`alpha2'*hfsq+`alpha3'*hmsq+`alpha4'*hfhm+`alpha5'*`yhf'
>
> +`alpha6'`yhm'+(`beta1'+`random1')*`y'+(`beta2'+`random2')*fh+(`beta3'+`random3')*hm
>         qui replace double ‘numer’=exp(`utility')
>         qui by cupid: replace double `sum'=sum(`numer') if didep==1
>         qui by cupid: replace double `denom'=sum[_N]
>         qui replace double `L1'=(`numer'/`denom')
>
>
> *-----------------------------------------------------------------------------------------------------*
>
>     } //forvalues r=1/50
>
>     mlsum `lnf'=ln(`L1'/50) if (`todo'==0 | `lnf'>=.) exit
>
> end
> ml model d0 ML (beta2: mage fage feduc meduc)(beta3: mage fage feduc meduc)
> /lnsig1 /lnsig2 /lnsig3
>
>
>
>
> On 06.06.2013 19:04, Nick Cox wrote:
>
> You don't declare
>
> tempname random1 random2 random3
>
> so the statements
>
> qui gen double `random1'=random1_`r'*`l11'
> qui gen double `random2'=random2_`r'*`l22'
> qui gen double `random3'=random3_`r'*`l33'
>
> will all fail. In fact, you have other bugs of the same kind.
>
> Also the statement
>
> qui gen double ‘numer’=exp(`utility')
>
> would fail because `numer' already exists.
>
> From your report, there are other bugs too.
>
>
>
> Nick
> [email protected]
>
>
> On 6 June 2013 14:10, Jonas Krüger <[email protected]> wrote:
>
> Hi everyone.
> I have some issues with my ML program. I try to estimate a structural
> labor supply model.
>
> Here is the Code first:
> ----------------------------------------------------------------------------------------------
> sort cupid Hc
> program ML
>         args todo b lnf tempvar beta1 beta2 beta3 L1 alpha1 alpha2 alpha3
> alpha4 alpha5  numer sum denom
>         tempname sigma1 lnsig1 l11 sigma2 lnsig2 l22 sigma3 lnsig3 l33
>         mleval `beta1'=`b', eq(1)
>         mleval `beta2'=`b', eq(2)
>         mleval `beta3'=`b', eq(3)
>         mleval `lnsig1'=`b',  scalar eq(4)
>         mleval `lnsig2'=`b',  scalar eq(5)
>         mleval `lnsig3'=`b',  scalar eq(6)
>         qui gen double `L1'=0, scalar
>         qui gen double `numer'=0
>         qui gen double `sum'=0
>         qui gen double `denom'=0
>         scalar `sigma1'=(exp(`lnsig1'))^2
>         scalar `sigma2'=(exp(`lnsig2'))^2
>         scalar `sigma3'=(exp(`lnsig3'))^2
>         matrix f=(`sigma1' , 0 , 0 \ 0 , `sigma2' , 0 \ 0 , 0 , `sigma3')
>         capture matrix U=cholesky(f)
>         scalar `l11'=U[1,1]
>         scalar `l22'=U[2,2]
>         scalar `l33'=U[3,3]
>         forvalues r=1/50{
>                 qui gen double `random1'=random1_`r'*`l11'
>                 qui gen double `random2'=random2_`r'*`l22'
>                 qui gen double `random3'=random3_`r'*`l33'
>                 qui gen double `yhf'=y*fh
>                 qui gen double `yhm'=y*hm
>                 qui gen double `ysq'=y^2
>
> *------------------------------------------------------------------------------------------------*
>                 qui gen double
> `utility'=`alpha1'*`ysq'+`alpha2'*hfsq+`alpha3'*hmsq+`alpha4'*hfhm+`alpha5'*`yhf'
>
> +`alpha6'`yhm'+(`beta1'+`random1')*`y'+(`beta2'+`random2')*fh+(`beta3'+`random3')*hm
>                 qui gen double ‘numer’=exp(`utility')
>                 qui by cupid: gen double `sum'=sum(`numer') if didep==1
>                 qui by cupid: gen double `denom'=sum[_N]
>                 qui gen double `L1'=(`numer'/`denom')
>
> *-----------------------------------------------------------------------------------------------------*
>
>         } //forvalues r=1/50
>
>         mlsum `lnf'=ln(`L1'/50) if (`todo'==0 | `lnf'>=.) exit
>
> end
> ml model d0 ML (`utility' = `beta1' `beta2' `beta3' `alpha1' `alpha2'
> `alpha3' `alpha4' `alpha5'), maximize
> -----------------------------------------------------------------------------------------------------
>
>
> The Problems are:
> 1. Stata gives the error: = invalid name
> r(198);
> ml check stops at:  mleval `beta2'=`b', eq(2)
>
>
>
> 2. How can I control which variables are used for: (beta1*x+random)
> etc.
> I want that stata is only using some variables there. E.g. Schooling,
> age etc.
>

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index