Announcement

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

  • Help on How to Divide exp(Matrix) by (1+exp(Matrix)) in Mata

    I am trying to build my Mata skill set and would appreciate help with what I expect is a relatively simple programming task.

    In the following toy example (using data from the Stata 15 sem.pdf manual), I would like to learn how to combine STEPS 3-5 in my Mata code into a single step, if that is possible. I've used verbose matrix names in my example code just for clarity.
    Code:
    use http://www.stata-press.com/data/r15/gsem_lca1
    gsem (accident play insurance stock <- ), logit lclass(C 2)
    mat B = e(b)'
    
    mata
     mataB = st_matrix("B")                       /* STEP1: Import logistic coefficients matrix from Stata */
     mataB                                        /* STEP2: Display logistic coefficients matrix in Mata */
     mataNUMERATOR = exp(mataB[1...,1..1])        /* STEP3: Create Odds matrix */
     mataDENOMINATOR = exp(mataB[1...,1..1]) :+ 1 /* STEP4: Create (1 + Odds) matrix */
     mataPROBS = mataNUMERATOR :/ mataDENOMINATOR /* STEP5: Create probabilities matrix as Odds/(1+Odds) */
     mataPROBS                                    /* STEP6: Display item response probabilities matrix */
     st_matrix("PROBS", mataPROBS)                /* STEP7: Export item response probabilities matrix to Stata */
    end
    I'm not interested in the substance of this particular example and know that I can obtain the item response probabilities matrix directly with the postestimation command -estat lcmean, post-. I am only using this example to illustrate the programming task I'm trying to learn how to perform with Mata.

    Thanks very much for any advice you may offer.

    Red Owl
    (yes, my real name)
    Stata/IC 15 with Windows 10

  • #2
    Hi Red Owl,

    What you want to use the invlogit() function, which does exactly that:

    Code:
    . mata: x=(1,2,3)
    
    . mata: exp(x) :/ (1 :+ exp(x))
                     1             2             3
        +-------------------------------------------+
      1 |  .7310585786    .880797078   .9525741268  |
        +-------------------------------------------+
    
    . mata: invlogit(x)
                     1             2             3
        +-------------------------------------------+
      1 |  .7310585786    .880797078   .9525741268  |
        +-------------------------------------------+
    To see more details check help mf_logit


    Best,
    Sergio

    Comment


    • #3
      Sergio,

      Thanks so much.

      Your mata code snippet is exactly what I wanted to know as a general skill -- and the -invlogit- command is an extra bonus!

      Red Owl
      Last edited by Red Owl; 20 Jun 2017, 21:39.

      Comment

      Working...
      X