Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Maximum Likelihood estimation

    Hi everyone,

    For my master's thesis I'm trying to do a maximum likelihood estimation. After working with my tutor, I've developed the do file, which I've saved with the name of Code:


    Code:
     use "DataML2.dta"
    sort id
     program drop _all
     program define MLSchool
     version 14.0
     args todo b lnf
     tempvar val lnsigma lnrho //where val = b*X_ij
     mleval `val' = `b', eq(1)
     mleval `lnsigma' = `b', eq(2)
     qui {
     replace sigma = exp(`lnsigma')
     by id : egen val1 = sum((school ==choice1 )*`val')
     by id : egen val2 = sum((school ==choice2 )*`val')
     by id : egen val3 = sum((school ==choice3 )*`val')
     by id : egen val4 = sum((school ==choice4 )*`val')
     by id : egen val5 = sum((school ==choice5 )*`val')
     by id : egen val6 = sum((school ==choice6 )*`val')
     by id : egen val7 = sum((school ==choice7 )*`val')
     by id : egen sigma1 = sum((school ==choice1 )*sigma)
     by id : egen sigma2 = sum((school ==choice2 )*sigma)
     by id : egen sigma3 = sum((school ==choice3 )*sigma)
     by id : egen sigma4 = sum((school ==choice4 )*sigma)
     by id : egen sigma5 = sum((school ==choice5 )*sigma)
     by id : egen sigma6 = sum((school ==choice6 )*sigma)
     by id : egen sigma7 = sum((school ==choice7 )*sigma)
     
     
     
     * Simulated log lihelihood
     
     forvalues t=1/10 {
     
     *no variable repetition
    
     
     *generate noises and total values
     replace epsilon = sigma*norm`t'
     replace totval= `val'+epsilon   //+outside*phi
     by id : egen totalval0 = 0
    
     
    
     
     *likelihood of NOT
     by id: replace likenot = sum(ignore* ln(normal((totalval0-`val')/sigma)))
     by id: replace likenot = exp(likenot[_N])
     
     by id: replace likenotn = sum(ignoren*ln(normal((totalval0-`val')/sigma)))
     by id: replace likenotn = exp(likenotn[_N])
    
    
    
    * Value and other things for 7th (last) option, naive and sophisticated
    drop crit7
     by id: egen crit7= max(prob7*(totval-totalval0)-((position<8)+ignore)*10000000000)
     replace like7 = normal(-(crit3/q3corr+totalval0-val7)/sigma7)
     replace e7= -sigma7*invnormal(u7`t'*like7)
     
     drop crit7n
     by id: egen crit7n=max(totval-((position<8)+ignore)*10000000000)
     replace like7n = normal(-(crit7n-val7)/sigma7)
     replace e7naive = -sigma7*invnormal(u7`t'*like7n)
     replace totval7=val7+e7
     replace totval7=totalval0 if roundnot<8
     replace res7=q7*totval7+(1-q7)*totalval0
     replace totval=totval7 if choice7==school
      
     replace totval7n=val7+e7naive
     replace totval7n=max(crit7n,totalval0) if roundnotn<8
     
    * Value and other things for 6th option
    
     drop crit6
     by id: egen crit6= max(prob6*(totval-res6)-((position<7)+ignore)*10000000000)
     replace like6 = normal(-(crit6/q6corr+res7-val6)/sigma6)
     replace e6= -sigma6*invnormal(u6`t'*like6)
     
     replace like6n = normal(-(totval7n-val6)/sigma6)
     replace e6naive= -sigma6*invnormal(u6`t'*like6n)
     
     replace totval6=val6+e6
     replace totval6=totalval0 if roundnot==6
     replace res6=q6*totval6+(1-q6)*res7
     replace totval=totval6 if choice6==school
     
     replace totval6n=val6+e6naive
     replace totval6n=max(crit7n,totalval0) if roundnotn==6
     
    * Value and other things for 5th option
    
     drop crit5
     by id: egen crit5= max(prob5*(totval-res6)-((position<6)+ignore)*10000000000)
     replace like5 = normal(-(crit5/q5corr+res6-val5)/sigma5)
     replace e5= -sigma5*invnormal(u5`t'*like5)
     
     replace like5n = normal(-(totval6n-val5)/sigma5)
     replace e5naive= -sigma5*invnormal(u5`t'*like5n)
     
     replace totval5=val5+e5
     replace totval5=totalval0 if roundnot==5
     replace res5=q5*totval5+(1-q5)*res6
     replace totval=totval5 if choice5==school
     
     replace totval5n=val5+e5naive
     replace totval5n=max(crit6n,totalval0) if roundnotn==5
    
    * Value and other things for 4th option
     drop crit4
     by id: egen crit4= max(prob4*(totval-res5)-((position<5)+ignore)*10000000000)
     replace like4 = normal(-(crit4/q4corr+res5-val4)/sigma4)
     replace e4= -sigma4*invnormal(u4`t'*like4)
     
     replace like4n = normal(-(totval5n-val4)/sigma4)
     replace e4naive= -sigma4*invnormal(u4`t'*like4n)
     
     replace totval4=val4+e4
     replace totval4=totalval0 if roundnot==4
     replace res4=q4*totval4+(1-q4)*res5
     replace totval=totval4 if choice4==school
     
     replace totval4n=val4+e4naive
     replace totval4n=max(crit5n,totalval0) if roundnotn==4
     
    * Value and other things for 3rd option
    
     drop crit3
     by id: egen crit3= max(prob3*(totval-res4)-((position<4)+ignore)*10000000000)
     replace like3 = normal(-(crit3/q3corr+res4-val3)/sigma3)
     replace e3= -sigma3*invnormal(u3`t'*like3)
     
     replace like3n = normal(-(totval4n-val3)/sigma3)
     replace e3naive= -sigma3*invnormal(u3`t'*like3n)
     
     replace totval3=val3+e3
     replace totval3=totalval0 if roundnot==3
     replace res3=q3*totval3+(1-q3)*res4
     replace totval=totval3 if choice3==school
     
     replace totval3n=val3+e3naive
     replace totval3n=max(crit4n,totalval0) if roundnotn==3
      
      
    * Value and other things for 2nd option
    
     drop crit2
     by id: egen crit2= max(prob2*(totval-res3)-((position<3)+ignore)*10000000000)
     replace like2 = normal(-(crit2/q2corr+res3-val2)/sigma2)
     replace e2= -sigma2*invnormal(u2`t'*like2)
     
     replace like2n = normal(-(totval3n-val2)/sigma2)
     replace e2naive= -sigma2*invnormal(u2`t'*like2n)
     
     replace totval2=val2+e2
     replace totval2=totalval0 if roundnot==2
     replace res2=q2*totval2+(1-q2)*res3
     replace totval=totval2 if choice2==school
     
     replace totval2n=val2+e2naive
     replace totval2n=max(crit3n,totalval0) if roundnotn==2
     
     
     * Value and other things for 1st option
     drop crit1
     by id: egen crit1= max(prob1*(totval-res2)-((position<2)+ignore)*10000000000)
     replace like1= normal(-(res2-val1+crit1/q1corr)/sigma1)
     replace like1n= normal(-(totval2n-val1)/sigma1)
     
     
     *total flow likelihood
     
     replace flowrat = like1*like2^(roundnot>2)* ///
     like3^(roundnot>3)*likenot^(roundnot<4)
     replace flownai = like1n*like2n^(roundnotn>2)* ///
     like3n^(roundnotn>3)*likenotn^(roundnotn<4)
     
     replace cummrat = cummrat+flowrat/10
     replace cummnai = cummnai+flownai/10
     
    }
     
      
      replace cumm = max(cummrat,cummnai)
      replace rat = (cummrat>=cummnai)
      replace cumm=1 if last ==0 | cumm>1
      replace cumm=0.0000000000000000001 if cumm<0.0000000000000000001
      mlsum `lnf'= ln(cumm) if last==1
      *drop val1 val2 val3 sigma1 sigma2 sigma3
      replace cummrat = 0
      replace cummnai = 0
      }
     end

    Afterwards, I type the following in Stata command:

    preserve
    do Code
    ml model d0 MLSchool (poverty monoparent sqdist dist1000 dist5000 change) /lnsigma
    (until here everything works fine).

    Finally, before starting the estimation, I type:
    ml check

    And I obtain the following error:

    Test 1: Calling MLSchool to check if it computes log likelihood and
    does not alter coefficient vector...
    FAILED; MLSchool returned error 133.

    And investigating further, I find (between a ton more of text):

    ---------------------------------------------------- end findfile ---
    - if (`"`r(fn)'"' == "") {
    = if (`""' == "") {
    - di as error "unknown egen function `fcn'()"
    = di as error "unknown egen function 0()"
    unknown egen function 0()
    - exit 133
    }
    }
    ---------------------------------------------------------- end egen ---

    It also appears some error 198, but since this is the one highlighted I imagine this is the problematic one.
    I've been trying to find a solution for it, but I haven't been able to. I would very much appreciate it if someone had any suggestions on what I should do.
    Thank you very much for your help.

    Best,

    Àlex
    Last edited by Alex Izquierdo; 20 Jun 2019, 04:08. Reason: Update Code

  • #2
    I have not gone thru your code in detail, but a quick search on -egen- found the following problem: "
    by id : egen totalval0 = 0 + epsilon " you have not included a function there and that is the reason for the message; looking at just that line, it appears that you want -gen- rather than -egen

    edited to fix typo

    Comment


    • #3
      That seems to solve the problem for now. There was a function originally in that line and I forgot to change it. Thank you very much.

      Comment


      • #4
        Hello experts
        I want to calculate beta coeffiecnt using maximum likelihood estimation and rolling ten-year windows for firm-year wise,but i dont know how to calculate it,could you please guide me how can i calculate it?
        for explaintion i am giving the detail below
        Following previous research (e.g., Lev 1983; Ali and Zarowin 1992) we measure earnings
        persistence as the slope coefficient estimate,
        Beta i,j, from an autoregressive model of order one (ARi) for annuals plit-adjustede arningsp er share (Xj,t) measured as firm j 's net income before extraordinary items in year t divided by the weighted average numberof outstanding shares during year t):
        XJ,t =constant +Beta1,jt Xj,t-1 + vj,t. (2)
        For each firm-year wise estimate Equation( 2) using maximum likelihood estimation and rolling ten-year windows. This procedure yields firm- and year-specific estimates of Beta1j
        which capture the persistence of earnings.
        .
        .
        looking forward for your guidence.
        thanks

        Comment

        Working...
        X