Announcement

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

  • Syntax error in a while loop with mata

    I'm sorry to post this problem, but I'm getting increasingly desperate ...

    I'm using a loop to get rid of autocorrelated residuals, but I run in a syntax error with the following code:

    PHP Code:
    regress variable1_variable2
    drop resid
    predict double resid
    res

    local xtwelfthpv 
    0
    local xlags 
    0

    while `xtwelfthpv' < 0.05 {
    quietly regress D.resid L.resid L(0/
    `xlags')D.resid, nocons
    quietly actest, lags(12) robust

    mata  {
    b = st_matrix("r(results)")
    m = b[.,4]
    st_numscalar("twelfthpv", m[12])
    m[12]
          }
    local xtwelfthpv = `twelfpv'
    local xlags = `xlags' + 1
                             }

    di 
    `xtwelfthpv'
    di `xlags' 
    The problem seems to be that
    PHP Code:
    st_numscalar("twelfthpv"m[12]) 
    does not transfer the value of m[12] to xtwelfthpv, since di `xtwelfthpv' yield "0" while m[12] equals 1.

    Can anybody tell me, where my syntax error lies? Thanks a lot!
    Last edited by Nora Kaimann; 03 Jul 2018, 03:35.

  • #2
    This just looks like confusion between scalars and locals and a series of typos. You assign a scalar called twelfthpv and then look for it in a local macro called twelfpv. If you have a scalar, there is no need to, and no advantage in, putting it in a scalar that can be guessed from your code.

    It is difficult to be sure because other typos appear in your code. The first regress statement looks wrong for a start.
    Last edited by Nick Cox; 03 Jul 2018, 05:55.

    Comment


    • #3
      Hello Nick! Sorry for the typos, which were the result of a "diagnostic" check of the names. The regression command works. Eliminating the typos and using only scalars does not solve the problem - even though "xtwelfthpv" and "xlags" have now the correct values. The error message reads now "} is not a valid command name" The corrected and modified code looks now like this:

      PHP Code:
      regress ln_CPI_GER ln_CPI_FRA ln_nat_curr_GER_FRA if  month >= month <= 156
      drop resid
      predict double resid
      res

      scalar xtwelfthpv 
      0
      scalar xlags 
      0

      while `xtwelfthpv' < 0.05 {
      quietly regress D.resid L.resid L(0/
      `xlags')D.resid, nocons
      quietly actest, lags(12) robust

      mata  {
      b = st_matrix("r(results)")
      m = b[.,4]
      st_numscalar("twelfthpv", m[12])
      m[12]
            }
      scalar xtwelfthpv = twelfthpv
      scalar xlags = xlags + 1
                               }

      di xtwelfthpv
      di xlags 
      What could be the problem? Thanks for any help!

      Comment


      • #4
        Still confusion between scalars and locals: for example, you define a scalar xlags and then refer to an undefined local xlags.

        I can't test anything (no data example; do please read FAQ Advice #12) but with some further simplifications this may be closer to what you seek. I am a big fan of Mata but I can't see that you need it here.


        Code:
        regress ln_CPI_GER ln_CPI_FRA ln_nat_curr_GER_FRA if inrange(month, 0, 156) 
        drop resid
        predict double resid, res
        
        scalar twelfthpv = 0
        local xlags = 0
        
        quietly while twelfthpv < 0.05 {
           regress D.resid L.resid L(0/`xlags')D.resid, nocons
           actest, lags(12) robust
           mat b = r(results)
           scalar twelfthpv = b[12, 4]
           local ++xlags
        }
        
        di twelfthpv

        Comment


        • #5
          To add to Nick's advice, let me explicitly call to your attention that references to scalars are not to be enclosed in quotation marks (`'). Compare the while command in post #3 with that in post #4. I think Nick considered that a typo but I suspect it's a misunderstanding of the syntax.

          Comment


          • #6
            Thanks a lot Nick! Now it works. The program starts with zero lags and adds one additional lag to an Engle-Granger cointegration test until H0 of no serial correlation (according to the Cumby-Huizinga test) over a rage of 12 month cannot be rejected a 5%. The data for the example are as follows:

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input float(month ln_CPI_GER ln_CPI_FRA ln_nat_curr_GER_FRA)
             0  3.203153 2.3360198 1.0447346
             1 3.1978564  2.338917 1.0472068
             2 3.1978564  2.338917 1.0475808
             3  3.203153  2.339881 1.0482538
             4  3.205993 2.3379521 1.0488515
             5  3.205993 2.3369865 1.0486274
             6  3.205993  2.340844 1.0488515
             7  3.205993 2.3504224 1.0488515
             8  3.203153 2.3513753 1.0488515
             9  3.205993 2.3532782 1.0486912
            10 3.2088256 2.3561258 1.0488406
            11 3.2088256 2.3580198 1.0484669
            12 3.2172744 2.3589654 1.0499076
            13 3.2172744 2.3599102 1.0488515
            14 3.2172744 2.3589654  .9995538
            15 3.2172744 2.3580198  .9989347
            16  3.225653 2.3561258  .9995538
            17 3.2311995 2.3542283 1.0013821
            18 3.2311995 2.3617969 1.0030102
            19 3.2311995 2.3674362 1.0029819
            20 3.2311995 2.3730435  1.003411
            21 3.2311995  2.384165 1.0031863
            22  3.236716  2.394252 1.0064545
            23  3.236716  2.397895 1.0062741
            24 3.2476575  2.404239 1.0059005
            25 3.2503746 2.4051416 1.0068873
            26  3.253084 2.4114394 1.0058656
            27  3.255786 2.4132316 1.0075009
            28  3.255786  2.415914  1.005253
            29  3.255786  2.420368 1.0050489
            30  3.258481  2.423917 1.0062741
            31  3.253084  2.423031 1.0072963
            32  3.253084  2.427454 1.0075009
            33  3.255786  2.429218 1.0103692
            34  3.258481  2.435366 1.0083195
            35  3.263849  2.438863 1.0066829
            36  3.277145 2.4466856 1.0081148
            37  3.285038  2.452728 1.0072963
            38  3.285038  2.455306 1.0056614
            39  3.287655  2.457878 1.0050489
            40  3.285038  2.461297 1.0028065
            41  3.279783 2.4680996 1.0021958
            42  3.279783  2.472328 1.0034176
            43  3.277145  2.475698 1.0023993
            44  3.279783 2.4824035 1.0019717
            45  3.287655  2.484073 1.0013821
            46  3.290266 2.4874036 1.0005691
            47 3.2980566  2.489065  1.000602
            48  3.303217  2.492379 1.0005691
            49  3.305787 2.4932055 1.0001956
            50  3.305787  2.494857 1.0009756
            51  3.305787  2.495682 1.0007724
            52  3.305787  2.496506 1.0005691
            53  3.303217  2.498152 1.0009756
            54  3.305787  2.500616 1.0011789
            55  3.305787  2.503074 1.0011789
            56  3.305787  2.507157 1.0007724
            57  3.308351  2.510412 1.0009756
            58 3.3134584  2.510412 1.0011789
            59 3.3210714  2.510412 1.0013821
            60  3.323596  2.516082 1.0015855
            61  3.323596   2.51689 1.0007514
            62 3.3286266  2.519308 1.0015855
            63 3.3311324  2.520917 1.0011911
            64  3.333632  2.524127 1.0060698
            65 3.3410935  2.540026 1.0079101
            66  3.343568  2.531313 1.0099589
            67  3.343568  2.528126 1.0103502
            68  3.343568  2.531313 1.0089126
            69 3.3484995  2.532903 1.0071273
            70  3.353407   2.53449 1.0070722
            71   3.35829 2.5384474 1.0081507
            72 3.3631494  2.541602 1.0105923
            73   3.36557 2.5431755 1.0105554
            74  3.367985  2.545531 1.0113955
            75 3.3751955 2.5486636 1.0118064
            76 3.3751955  2.552565 1.0095853
            77 3.3751955  2.551786 1.0070918
            78 3.3751955  2.554899 1.0048448
            79  3.372798  2.555676 1.0025882
            80  3.372798 2.5587766  .9977664
            81 3.3751955  2.560323  .9932367
            82  3.384729  2.562639  .9909847
            83 3.3870986  2.565718   .990997
            84  3.389462  2.569554  .9898063
            85  3.389462 2.5703194  .9910139
            86   3.39182  2.573375    .99084
            87   3.39182  2.574138  .9936731
            88  3.394173  2.575661   .999503
            89  3.394173  2.575661 1.0036757
            90  3.394173 2.5787005 1.0073123
            91  3.389462  2.581731   1.00625
            92  3.389462  2.585506 1.0072927
            93   3.39182  2.590017  1.007946
            94  3.394173  2.597491 1.0019639
            95  3.394173  2.598979 1.0052426
            96  3.403528  2.608598 1.0046728
            97  3.405853  2.609334 1.0032475
            98  3.405853   2.61007  .9982824
            99  3.405853 2.6130066  .9969072
            end
            format %tm month
            The program code is:

            PHP Code:
            scalar twelfthpv 0
            local xlags 
            0
            quietly 
            while twelfthpv 0.05 {
               
            egranger ln_CPI_GER ln_CPI_FRA ln_nat_curr_GER_FRA if inrange(month0156), lags(`xlags') regress
               actest, lags(12) robust
               mat b = r(results)
               scalar twelfthpv = b[12, 4]
               local ++xlags

            Omitting "quietly" shows, how adding lags from 0 to 2 increases the corresponding p-value from 0.0030 over 0.0394 to 0.0848. Maybe this is useful for others too.

            But what was the problem with my code? As I see, you have omitted my "mata inlay". I thought mata in a while-loop should work? I admit my problems with local variables. I tried to work myself through the corresponding passages in the reference manual, but found them difficult to understand. Most people learn faster with good examples. I find the examples in the reference manual typically quite complex. As a textbook writer, I always start with simple examples and than add some complexities...

            Comment


            • #7
              I don't think you can go

              Code:
              mata {
              
              }

              Comment


              • #8
                Hi
                I would run Stata inside Mata and I would avoid mixing Stata and Mata code.
                Like (code is not tested, code from #4 since it seems to be working):

                Code:
                regress ln_CPI_GER ln_CPI_FRA ln_nat_curr_GER_FRA if inrange(month, 0, 156)
                drop resid
                predict double resid, res
                
                mata:
                    twelfthpv = 0
                    xlags = 0
                    while ( twelfthpv < 0.05 ) {
                        stata(sprintf("regress D.resid L.resid L(0/%f)D.resid, nocons", xlags))
                        stata(sprintf("actest, lags(12) robust"))
                        twelfthpv = st_matrix("r(results)")[12,4]
                        ++xlags
                    }
                    st_numscalar("twelfthpv", twelfthpv)
                end
                I have used
                Code:
                mata{ }
                inside a Stata while loop
                Kind regards

                nhb

                Comment

                Working...
                X