0

I am having issues in storing the results of auto-correlations:

sysuse sp500.dta
tsset date
corrgram open

di `r(ac10)'
di `r(ac11)'

As you see, the corrgram command opens a table with AC, PAC, Q etc. I wish to store the data, but somehow I cannot access all the information.

Why can I get data from r(ac10) and not r(ac11)? I mean, the information is already there, can someone enlighten me?

2 Answers2

1

The corrgram command returns certain results such as r(ac1) - r(ac10) as scalars:

. return list

scalars:
               r(lags) =  40
                r(q10) =  997.7976424554661
              r(pac10) =  .
               r(ac10) =  .4660537633851876
                 r(q9) =  941.2145380745385
               r(pac9) =  .
                r(ac9) =  .4926995142415946
                 r(q8) =  878.2410054272498
               r(pac8) =  .
                r(ac8) =  .6798652708950988
                 r(q7) =  758.8350022301894
               r(pac7) =  .
                r(ac7) =  .8497950586730207
                 r(q6) =  573.0532520717032
               r(pac6) =  .
                r(ac6) =  .7058394210650882
                 r(q5) =  445.4128558881126
               r(pac5) =  .
                r(ac5) =  .537862961940051
                 r(q4) =  371.600563372601
               r(pac4) =  -.123160137402293
                r(ac4) =  .5238127059274379
                 r(q3) =  301.8811184740619
               r(pac3) =  .0903652372655368
                r(ac3) =  .5378744937938151
                 r(q2) =  228.6682359982045
               r(pac2) =  -.0172170443544806
                r(ac2) =  .5633576757209979
                 r(q1) =  148.6802035217271
               r(pac1) =  .9912569913768637
                r(ac1) =  .769625068645877

However, it also returns everything as matrices:

matrices:
                  r(Q) :  1 x 40
                r(PAC) :  1 x 4
                 r(AC) :  1 x 40

You can access what you need from the respective returned matrix e.g. r(Q), r(PAC) or r(AC).

For ac11 specifically:

. display el(r(AC), 1, 11)
.46352669

or

. matrix A = r(AC)
. display A[1,11]
.46352669

To save the returned results directly as a Stata variable with the name ac:

mata: st_store((1::40), st_addvar("double","ac"), colshape(st_matrix("r(AC)"), 1))

. list ac in 1 / 40

     +-----------+
     |        ac |
     |-----------|
  1. | .76962507 |
  2. | .56335768 |
  3. | .53787449 |
  4. | .52381271 |
  5. | .53786296 |
     |-----------|
  6. | .70583942 |
  7. | .84979506 |
  8. | .67986527 |
  9. | .49269951 |
 10. | .46605376 |
     |-----------|
 11. | .46352669 |
 12. | .47980565 |
 13. | .62691605 |
 14. | .76459033 |
 15. | .59986479 |
     |-----------|
 16. | .43064803 |
 17. | .41606848 |
 18. |  .4143769 |
 19. | .42979786 |
 20. | .55313291 |
     |-----------|
 21. |  .6518829 |
 22. | .50817027 |
 23. | .35552032 |
 24. | .34330162 |
 25. | .34298527 |
     |-----------|
 26. | .34803055 |
 27. | .44354181 |
 28. | .52455024 |
 29. | .40499719 |
 30. | .28776225 |
     |-----------|
 31. |  .2736146 |
 32. | .27286332 |
 33. | .27667982 |
 34. | .34373118 |
 35. | .40664714 |
     |-----------|
 36. | .30633385 |
 37. | .21286866 |
 38. | .19836486 |
 39. | .20569954 |
 40. | .20306142 |
     +-----------+
1

I think it is easier to store these as variables rather than messing with the matrices or scalars:

. webuse air2, clear
(TIMESLAB: Airline passengers)

. corrgram air, lags(20)

                                          -1       0       1 -1       0       1
 LAG       AC       PAC      Q     Prob>Q  [Autocorrelation]  [Partial Autocor]
-------------------------------------------------------------------------------
1        0.9480   0.9589   132.14  0.0000          |-------           |------- 
2        0.8756  -0.3298   245.65  0.0000          |-------         --|        
3        0.8067   0.2018   342.67  0.0000          |------            |-       
4        0.7526   0.1450   427.74  0.0000          |------            |-       
5        0.7138   0.2585    504.8  0.0000          |-----             |--      
6        0.6817  -0.0269    575.6  0.0000          |-----             |        
7        0.6629   0.2043   643.04  0.0000          |-----             |-       
8        0.6556   0.1561   709.48  0.0000          |-----             |-       
9        0.6709   0.5686   779.59  0.0000          |-----             |----    
10       0.7027   0.2926   857.07  0.0000          |-----             |--      
11       0.7432   0.8402   944.39  0.0000          |-----             |------  
12       0.7604   0.6127   1036.5  0.0000          |------            |----    
13       0.7127  -0.6660     1118  0.0000          |-----        -----|        
14       0.6463  -0.3846   1185.6  0.0000          |-----          ---|        
15       0.5859   0.0787   1241.5  0.0000          |----              |        
16       0.5380  -0.0266     1289  0.0000          |----              |        
17       0.4997  -0.0581   1330.4  0.0000          |---               |        
18       0.4687  -0.0435     1367  0.0000          |---               |        
19       0.4499   0.2773   1401.1  0.0000          |---               |--      
20       0.4416  -0.0405   1434.1  0.0000          |---               |        

. ac air , lag(20) gen(ac)

. pac air, lag(20) gen(pac)

. list t ac pac in 1/20, clean noobs

     t          ac          pac  
     1   .94804734    .95893198  
     2   .87557484   -.32983096  
     3   .80668116     .2018249  
     4   .75262542    .14500798  
     5   .71376997    .25848232  
     6    .6817336   -.02690283  
     7   .66290439    .20433019  
     8   .65561048    .15607896  
     9   .67094833    .56860841  
    10   .70271992    .29256358  
    11   .74324019     .8402143  
    12   .76039504    .61268285  
    13   .71266087   -.66597616  
    14   .64634228   -.38463943  
    15   .58592342     .0787466  
    16   .53795519   -.02663483  
    17   .49974753   -.05805221  
    18   .46873401   -.04350748  
    19   .44987066    .27732556  
    20    .4416288   -.04046447  
dimitriy
  • 9,077
  • 2
  • 25
  • 50