Announcement

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

  • Rewrite MLE code to Bayes code in Stata using bayesmh

    I have a simple MLE estimation in Stata 14, which I want to move towards a Bayesian estimation using `bayesmh`.


    Code:
    program define mylikelihood
    args lnf Xb1 Xb2
    qui replace `lnf' = ln(1-normal(`Xb1'))+ln(1-normal(`Xb2')) if $ML_y1==0
    qui replace `lnf' = ln(normal(`Xb1'))+ln(1-normal(`Xb1'))+ln(1-normal(`Xb2')) if $ML_y1==1 & $ML_y2==0
    qui replace `lnf' = ln(1-(1-normal(`Xb1')*normal(`Xb1'))*(1-normal(`Xb2'))) if $ML_y1==1 & $ML_y2==1
    end
    MLE is quite easy then:

    Code:
    ml model lf mylikelihood (link=treatment x1 x2 x3 x4 x5) (bilink=treatment x5 x6 x7), ///
    vce(cluster clustvar)
    Now I want to move towards simple Bayesian analysis. For starters I wanted to try out the simplest possible version with flat priors:

    By attempt so far:

    Code:
    bayesmh (link=treatment x1 x2 x3 x4 x5) (bilink=treatment x5 x6 x7) ,///
    llevaluator(mylikelihood) ///
    prior({eco_link: treatment x1 x2 x3 x4 x5} {bilink: treatment x5 x6 x7},flat)
    Which gives me the error:
    Code:
    ==0 invalid name
    mylikelihood could not be evaluated
    I suspect I need to specify the parameters passed on the mylikelihod somehow, but I'm not sure how.
    Last edited by Simon Heß; 12 May 2016, 08:26.

  • #2
    I think I solved it. Turns out that I overlooked that bayesmh takes the likelihood in a different form (not by obs. but overall). So I rewrote:
    Code:
    program define mylikelihoodevaluator
        args lnf Xb1 Xb2
        tempvar lnfj
        qui gen `lnfj' = .
        qui replace `lnfj' = ln(1-normal(`Xb1'))+ln(1-normal(`Xb2'))                        if $MH_y1==0         
        qui replace `lnfj' = ln(normal(`Xb1'))+ln(1-normal(`Xb1'))+ln(1-normal(`Xb2'))        if $MH_y1==1 & $MH_y2==0
        qui replace `lnfj' = ln(1-(1-normal(`Xb1')*normal(`Xb1'))*(1-normal(`Xb2')))            if $MH_y1==1 & $MH_y2==1
        summarize `lnfj' if $MH_touse, meanonly
        if r(N) < $MH_n {
            scalar `lnf'=.
            exit
        }
        scalar `lnf' = r(sum)
    end
    Now I get a lot of:
    Variable omissions (I didn't get these when doing plain ML):
    Code:
    note: parameter eco_link:7.i_enum is omitted
    note: parameter eco_link:2.j_enum is omitted
    note: parameter eco_link:3.j_enum is omitted
    note: parameter eco_link:4.j_enum is omitted
    note: parameter eco_link:5.j_enum is omitted
    note: parameter eco_link:6.j_enum is omitted
    note: parameter eco_link:7.j_enum is omitted
    note: parameter eco_link:2.i_ethnicity is omitted
    note: parameter eco_link:3.i_ethnicity is omitted
    note: parameter eco_link:4.i_ethnicity is omitted
    note: parameter eco_link:5.i_ethnicity is omitted
    note: parameter eco_link:6.i_ethnicity is omitted
    And after some quite outlandish parameter estimates (posterior summary statistics) I get the following remark:

    Code:
    Note: There is a high autocorrelation after 500 lags.
    Note: Adaptation tolerance is not met.
    Any clues as to what might be going on here?
    Last edited by Simon Heß; 12 May 2016, 09:15.

    Comment


    • #3
      Simon,

      The model parameters {eco_link:} are dropped because they are not used anywhere else; specifically, they are not used in your likelihood specification. Maybe you want to specify a prior for the {link:} parameters?

      The latter notes you receive after running bayesmh simply mean that your model has not converged. Of course, in such cases the posterior estimates are useless. It is either a problem with the likelihood or with your prior. Using flat prior is generally not recommended.

      Nikolay

      Comment


      • #4
        Thank you for you reply Nikolay. Unfortunatel the difference between {eco_link:} and {link:} only arose when I was simplifying my code for posting it here. In my code they are the same.

        The parameters which are dropped are those for the factor variables i.foo, I ask about this in a separate question however: here

        OT: When/why/where was it decided that past posts can not be edited? Can I vote against this somewhere?

        Comment

        Working...
        X