Announcement

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

  • [Beginner question] Bayesian Hierarchical Model

    Dear everyone,

    I'm trying to replicate a Bayesian model similar to one found on page 47-51 of this file (https://www.stata.com/meeting/chicag...go16_balov.pdf).

    Code:
    use http://www.stata-press.com/data/r15/usmacro
    
    mswitch dr fedfunds
    
    generate id=_n
    
    help fvset
    
    fvset base none id
    
    set seed 12345
    
    bayesmh fedfunds = (({state:}==1)*{mu1}+({state:}==2)*{mu2}), ///
    likelihood(normal({sig2})) redefine(state:i.id) ///
    prior({state:}, index({p1}, (1-{p1}))) ///
    prior({p1}, uniform(0, 1)) ///
    prior({mu1} {mu2}, normal(0, 100)) ///
    prior({sig2}, igamma(0.1, 0.1)) ///
    init({p1} 0.5 {mu1} 1 {mu2} 1 {sig2} 1 {state:} 1) ///
    block({sig2}, gibbs) block({p1}) block({mu1}{mu2}) ///
    exclude({state:}) dots

    However, when I attempted to copy & paste the code given above in the example to Stata but it keeps returning this error
    Code:
    . bayesmh fedfunds = (({state:}==1)*{mu1}+({state:}==2)*{mu2}), ///
    invalid bayesmh specification
        To fit one of the supported models, you must specify a likelihood model in option likelihood() and a prior distribution in option prior().  To fit your own
        model, you must specify, for example, the name of your posterior-function evaluator in option evaluator().  See bayesmh and bayesmh evaluators for details.
    r(198);
    If I understand it correctly, I would be using the results from mswitch as input for the bayesmh model. So my initial attempts are to stored the results from the mswtich first. Afterward, I attempt to run the bayesmh again but it still returns the same error. My code:

    Code:
    mswitch dr fedfunds
    
    matrix list e(b)
    
    e(b)[1,5]
            State1:     State2:    lnsigma:        p11:        p21:
             _cons       _cons       _cons       _cons       _cons
    y1     3.70877   9.5567934   .74553174  -4.0045427   2.9369133
    
    
    #store the results in a matrix for further extraction
    matrix b = e(b)
    
    #extract and assign them accordingly to a variable
     gen mu1=b[1,1]
    
    gen mu2=b[1,2]
    
    gen sig1=b[1,3]
    
    gen sig2=b[1,4]
    
    gen p1=b[1,5]

    I would really appreciate it if you guys can point out my mistakes and help me replicate the example above, and kindly help me with these 3 questions:
    1. What do the state: as in this code mean?
      Code:
      bayesmh fedfunds = (({state:}==1)*{mu1}+({state:}==2)*{mu2}),
    2. As the 2 states follow First-Order Markov Chain (so that we need transition probabilities p11 and p22 for both state), why does the example have only 1 probability variable p1, when it should have 2?
    3. Assuming that I replicate and run the Bayesian model successfully, how I can extract the data of p11 and p22 to graph a chart similar to this one?
    Click image for larger version

Name:	chart.JPG
Views:	1
Size:	171.7 KB
ID:	1537897


    I'm aware that this is a very simple example, just that I'm a beginner to Stata, I would appreciate any help I can get! I'm using Stata15.

    Best,

  • #2
    You didn't get a quick answer. I must say that I don't think starting with the Bayesian hierarchical model is the what way to go for a beginner at Stata. This is much more complicated than some other models.

    I don't work with these models, but I I would suspect that somewhere you've made a minor difference between what was in the presentation and what you've posted. Are you sure there you have a space between the comma and the three slashes? Is there something funny at the beginning of the next line or is the next line blank?
    You might also find it better to start with an example from the manual. It is conceivable that there is something slightly wrong in the PowerPoint that may not be in the manual examples. The manual also seems to start the simpler examples when you're working with.

    Comment


    • #3
      However, when I attempted to copy & paste the code given above in the example to Stata but it keeps returning this error
      Your problem might be as simple as the actual cut and paste. Try copying the command into a .do file and running it from the .do file.

      Comment

      Working...
      X