Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# Re: st: Program to simulate AR(1) time series and return autocorrelations

 From Robson Glasscock To statalist@hsphsun2.harvard.edu Subject Re: st: Program to simulate AR(1) time series and return autocorrelations Date Fri, 24 Feb 2012 22:35:20 -0500

```Brandon,
I think the code below should do it. It records each element in the AC
matrix for each iteration. Admittedly, the code is clunky so there are
no style points for me tonight.

program define simarmat, rclass

drop _all
tempvar simy
sim_arma simy, nobs(20) arcoef(.45) time(time) spin(2000)
tsset time
corrgram simy, lags(13) noplot

tempvar ac
tempvar q1
tempvar q2
tempvar q3
tempvar q4
tempvar q5
tempvar q6
tempvar q7
tempvar q8
tempvar q9
tempvar q10
tempvar q11
tempvar q12
tempvar q13
matrix ac = r(AC)
return scalar q1= ac[1,1]
return scalar q2= ac[1,2]
return scalar q3= ac[1,3]
return scalar q4= ac[1,4]
return scalar q5= ac[1,5]
return scalar q6= ac[1,6]
return scalar q7= ac[1,7]
return scalar q8= ac[1,8]
return scalar q9= ac[1,9]
return scalar q10= ac[1,10]
return scalar q11= ac[1,11]
return scalar q12= ac[1,12]
return scalar q13= ac[1,13]
end

simulate q1= r(q1) q2=r(q2) q3= r(q3) q4= r(q4) q5= r(q5) q6=r(q6) q7= r(q7) ///
q8= r(q8) q9= r(q9) q10= r(q10) q11= r(q11) q12=r(q12) q13= r(q13),
reps(20): simarmat, obs(20)

best,
Robson Glasscock

On Fri, Feb 24, 2012 at 6:59 PM, Nick Cox <njcoxstata@gmail.com> wrote:
> You can't put a matrix into a value of a variable.
>
> Nick
>
> On Fri, Feb 24, 2012 at 7:02 PM, brandon lebeau <lebebr01@gmail.com> wrote:
>> I am attempting to write a program that simulates an AR(1) time series
>> using the sim_arma command
>> and computes autocorrelations with the corrgram function.  I'm having
>> difficulties accessing the stored
>> results from the corrgram function, specifically the matrix r(AC).I
>> have also tried just accessing r(ac1),
>> r(ac2), etc. and that works, but does not work when I try to use the
>> simulate command.
>>
>> Here is my program so far:
>>
>> program define simarmaT, rclass
>>   sim_arma simy, nobs(20) arcoef(.45) time(time) spin(2000)
>>   tsset time
>>   corrgram simy, lags(13) noplot
>>   return matrix autoC = r(AC)
>> end
>>
>> Then I'd like to use the simulate function to replicate this about
>> 10,000 times.
>> Here is a version of my simulate command:
>>
>> simulate autoC = r(AC), reps(20): simarmaT
>>
>>
>> I'm hoping their is a simple solution to this as I'm a new user to
>> Stata, I primarily use R,
>> but need to replicate my simulation done in R with another program.
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```