Announcement

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

  • Mata loop stops in middle

    Hi all,
    I'm new to Mata, and trying to run a loop in Stata MP/13.0. Code is below. It does not give an error message. However, when I look for bw8 and above, they are missing. I've included the contents of the matrix "test2" below the code -- I would have uploaded it but I don't know how. Any help much appreciated.

    corr speak respf respm disc respil respfa shame harmo assist rejec hide stigma t_f13 t_f14 t_f15 t_f16 t_f17 t_f18
    mat test = r(C)
    mat test2 = inv(test)
    mata:
    bMata = st_matrix("test2")
    x=0
    for (x=0; x<=18; x++)
    {
    bw`x' = bMata[`x',.]
    bww`x'=sum(bw`x')
    st_numscalar("myx"`x', bww`x')
    st_local("myx"`x', strofreal(bww`x'))
    }

    *Contents of the matrix "test2"
    symmetric test2[18,18]
    speak respf respm disc respil respfa shame harmo assist rejec
    speak 1.1449438
    respf -.05861383 3.5864421
    respm -.01396554 -2.5474273 3.5963856
    disc -.11323005 -.01706234 -.17073948 1.7802785
    respil .01510875 -.29979395 -.0198282 -.17446063 2.5856913
    respfa -.07990326 -.27034611 -.32274989 .07699831 -.90372342 2.5282429
    shame -.10352454 .00709932 -.02694958 -.29055528 -.05335061 .01439121 1.3277868
    harmo -.00185129 -.1446008 -.21368752 -.05280928 -.08688783 -.17957743 -.02482806 1.7879554
    assist -.06881689 .02980834 -.06912532 -.05341773 .00546659 -.14749428 .10116636 -.2797105 1.2199673
    rejec -.02708673 -.13494236 .18616334 -.47564234 .00163865 -.12841345 -.07760704 -.05687969 -.11569788 1.6681108
    hide -.03656623 .06784794 -.13616168 -.16104943 -.0345305 -.03033742 -.21335266 .02173528 .0063602 -.30609534
    stigma -.07344086 .06421698 -.17085051 -.3095343 -.06195304 .00860739 -.14443521 .02585021 -.08419101 -.35729268
    t_f13 .1256622 -.07480094 .10943784 -.05232722 .31528515 -.99735939 .07459647 .05470117 .06564656 .10691419
    t_f14 -.02521333 .16567063 -.13401783 -.07491902 -1.2854098 .35276219 .00313094 .14679995 .04501756 .02395684
    t_f15 -.04640304 -.12795739 .00781926 .04991707 -.04146008 .19192202 -.05724094 -.730132 -.0320698 .03269322
    t_f16 -.08524581 -.09008086 .16631674 -.03487574 .19586186 -.02389981 -.07492815 .08052118 -.23099092 -.12163936
    t_f17 -.04566726 -.01979998 -.10322988 .13261982 .02585315 -.25583255 -.06045586 .05962644 .18767641 .10548484
    t_f18 .00646318 -.05415423 -.12884979 -.06167901 -.10143759 -.06441443 .03211477 -.17574592 .05984403 -.09951785

    hide stigma t_f13 t_f14 t_f15 t_f16 t_f17 t_f18
    hide 1.499419
    stigma -.32167384 1.6139319
    t_f13 .14473691 -.02861942 2.0978614
    t_f14 -.12081722 -.10739484 -.84592242 2.4483702
    t_f15 -.01724869 .00853528 -.23167426 -.1903741 2.3591727
    t_f16 .01559302 .06540792 -.0924284 -.19349248 -.39598566 1.7084448
    t_f17 .04573754 -.06467399 -.14862766 -.09912891 -.01341883 -.32909489 1.7465248
    t_f18 -.06982325 .09263613 -.01695125 -.094581 -.81998646 -.42093022 -.64287297 2.451067
    Last edited by John Quattrochi; 28 Jun 2016, 08:09.

  • #2
    I'm surprised that it executed without error. You're mixing Stata's local macros and Mata's looping variable.

    Code:
    mata:
    bMata = st_matrix("test2")
    x=0 // Not needed; x is initialized inside the loop
    for (x=0; x<=18; x++)
         {
         bw`x' = bMata[`x',.] // `x' is for Stata, and not Mata; refer to the looping variable as x and not `x'
         bww`x'=sum(bw`x') // Mata's variables are not macro variables and cannot be used to append to another token
         st_numscalar("myx"`x', bww`x')
         st_local("myx"`x', strofreal(bww`x'))
    }
    As a first step, my recommendation is to
    Code:
    mata set matastrict on
    It will help trap many common errors.

    Also, update to Release 13.1; it's free.

    Comment


    • #3
      You cannot use Stata's local macro in Mata the same way as with Stata.

      you cannot code

      Code:
      for (x=0; x<=18; x++)
      {
      bw`x' = bMata[`x',.]
      }
      local `x' is in this case empty

      see this article for more details

      Comment


      • #4
        Josephe, Christophe, thank you for the quick response. I'm now using this code with a minimum of Mata: ​corr speak respf respm disc respil respfa shame harmo assist rejec hide stigma t_f13 t_f14 t_f15 t_f16 t_f17 t_f18 mat test = r(C) mat test2 = inv(test) mata : st_matrix("bMata", rowsum(st_matrix("test2"))) forvalues i=1/18{ scalar x`i' = bMata[`i',1] gen w`i' = x`i' }

        Comment


        • #5
          Josephe, Christophe, thank you for the quick response. I'm now using this code with a minimum of Mata:
          ​corr speak respf respm disc respil respfa shame harmo assist rejec hide stigma t_f13 t_f14 t_f15 t_f16 t_f17 t_f18
          mat test = r(C)
          mat test2 = inv(test)
          mata : st_matrix("bMata", rowsum(st_matrix("test2")))
          forvalues i=1/18{
          scalar x`i' = bMata[`i',1]
          gen w`i' = x`i'
          }

          Comment

          Working...
          X