Announcement

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

  • Xtmelogit, reffects, forvalues and predicted probabilities

    Dear Stata users,

    I'm trying to generate and graph predicted probabilities for multilevel models with random components, and I'm having some trouble. Can you help?

    To do so, I follow the advice offered on this page: http://www.ats.ucla.edu/stat/stata/f..._xtmelogit.htm

    I am using xtemelogit and predict, with the reffects option to generate best linear unbiased predictors of the random effects. I then manually code the formulae to express the predicted probabilities. So far, so good.

    The problem comes about three fifths down the page. To generate predicted probabilities for a variety of values of one variable and graph the results, the instructions suggest that I use a local macro and a forvalues loop. I have been using the code below, which has been carefully reproduced from the page above.

    Something is wrong with the code, and I can't figure out what. When I run it, the screen reads 'varlist required'. Can anyone spot what mistake I've made?


    use http://www.ats.ucla.edu/stat/data/hsbdemo, clear
    xtmelogit honors read female socst || cid: read, var covar(unstr)
    predict re*, reffects

    tempvar rxb0 rxb1 p0 p1
    generate `rxb0'=.
    generate `rxb1'=.
    generate `p0'=.
    generate `p1'=.
    matrix m=J(9,1,0)
    matrix f=J(9,1,0)
    matrix d=J(9,1,0)
    local i = 1

    forvalues j=30(5)70 {
    quietly replace `rxb0'=_b[female]*0+(_b[read]+re1)*`j'+_b[socst]*socst+_b[_cons]+re2
    quietly replace `rxb1'=_b[female]*1+(_b[read]+re1)*`j'+_b[socst]*socst+_b[_cons]+re2
    quietly replace `p0'=exp(`rxb0')/(1+exp(`rbx0'))
    quietly sum `p0'
    matrix m[`i',1]=r(mean)
    quietly replace `p1'=exp(`rxb1')/(1+exp(`rbx10'))
    quietly sum `p1'
    matrix f[`i',1]=r(mean)
    matrix d[`i',1]=f[`i',1]-m[`i',1]
    local i = `i' + 1
    }

  • #2
    I am dealing with the same issue. I also want to add the confidence interval to the graph. Do you know how to estimate the standard error for the probabilities that include random error and fixed error. -Thanks!

    Comment


    • #3
      The tempvars in the bolded lines below are incorrectly spelled:

      Code:
      use http://www.ats.ucla.edu/stat/data/hsbdemo, clear
      xtmelogit honors read female socst || cid: read, var covar(unstr)
      predict re*, reffects
      
      tempvar rxb0 rxb1 p0 p1
      generate `rxb0'=. 
      generate `rxb1'=. 
      generate `p0'=.
      generate `p1'=.
      matrix m=J(9,1,0)
      matrix f=J(9,1,0)
      matrix d=J(9,1,0)
      local i = 1
      
      forvalues j=30(5)70 {
      quietly replace `rxb0'=_b[female]*0+(_b[read]+re1)*`j'+_b[socst]*socst+_b[_cons]+re2
      quietly replace `rxb1'=_b[female]*1+(_b[read]+re1)*`j'+_b[socst]*socst+_b[_cons]+re2
      quietly replace `p0'=exp(`rxb0')/(1+exp(`rbx0'))
      quietly sum `p0'
      matrix m[`i',1]=r(mean)
      quietly replace `p1'=exp(`rxb1')/(1+exp(`rbx10'))
      quietly sum `p1'
      matrix f[`i',1]=r(mean)
      matrix d[`i',1]=f[`i',1]-m[`i',1]
      local i = `i' + 1
      }
      also for your first line of code reading in the data,this location is not working for me (using Stata 15), this works though:

      use https://stats.idre.ucla.edu/stat/data/hsbdemo, clear


      Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

      Comment

      Working...
      X