Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Simulating an AR(1) in Stata
From
Syed Basher <[email protected]>
To
[email protected]
Subject
Re: st: Simulating an AR(1) in Stata
Date
Thu, 24 Feb 2011 09:30:44 -0800 (PST)
Nick,
Perhaps replacing -gen- with -scalar- ensures rho and s as scalars. Maybe I
should write:
scalar `rho' = a+(b-a)*runiform()
scalar `s '= c+(d-c)*runiform()
However, using ` ' reports invalid name, while excluding ` ' works fine.
Syed
----- Original Message ----
From: Nick Cox <[email protected]>
To: [email protected]
Sent: Thu, February 24, 2011 3:08:36 PM
Subject: Re: st: Simulating an AR(1) in Stata
If you want to write this program yourself, you need to read up about
scalars and about programs taking arguments. The latter part of [U]
documents most of what you need to know.
If you want someone else to write this program for you, then another
person on the list may be willing to do it, or you should ask people
to approach you privately about undertaking consultancy.
Nick
On Thu, Feb 24, 2011 at 11:36 AM, Syed Basher <[email protected]> wrote:
> Thank you again Nick. So what modifications can be done to get -rho- and -s-
as
> scalars, as well as generating multiple series? I truly appreciate your help.
>
Nick Cox
> I don't think the rephrasing makes any difference.
>
> As said, your rho and SD vary within a single realisation of this
> process. That's doesn't sound like what you want at all.
>
> What you should do if you are unclear about this is -list- the data
> after running this once. Then you will see that -rho- and -s- are
> variables in the Stata sense and in the statistical sense. You need to
> rewrite your program so they are scalars.
>
> Also, you need to extend this so it generates multiple series, not one.
> On Thu, Feb 24, 2011 at 10:33 AM, Syed Basher <[email protected]> wrote:
>> Thank you Nick. Here is a rephrasing of my intent:
>>
>> I want to generate sequences of first-order autoregressive processes with
> AR(1)
>> parameters randomly drawn from the [0.48,0.83] interval, and disturbances
> drawn
>> from a zero-mean normal distribution where the standard deviation was
randomly
>> selected from the [0.005,0.014] interval. I am using the following steps for
> my
>> purpose. Does this look okay? Thank you.
>>
>> **************************************
>> clear
>> version 11.1
>> quietly set obs 100
>> set seed 10101
>> gen t=_n
>> scalar a = 0.48
>> scalar b = 0.83
>> scalar c = 0.005
>> scalar d = 0.014
>> gen rho = a+(b-a)*runiform()
>> gen s = c+(d-c)*runiform()
>> gen et = rnormal(0, s)
>> quietly gen yt= et in 1
>> quietly replace yt=rho*yt[_n-1]+et in 2/L
>> exit
>> **************************************
Nick Cox
>> This program does not produce 500 series. It creates a single series
>> with 500 observations in which rho varies from observation from
>> observation, and the SD of the normal distribution varies ditto. So,
>> I guess it does not match your intent.
>>
>> Also note that the loop over observations is unnecessary:
>>
>> quietly gen yt= et in 1
>> quietly replace yt=rho*yt[_n-1]+et in 2/L
>>
>> would I imagine match your intent.
On Thu, Feb 24, 2011 at 6:36 AM, Syed Basher <[email protected]> wrote:
>>> I am using Stata 11.1. I am trying to generate 500 sequences of first-order
>>> autoregressive processes with AR(1) parameters randomly drawn from the
>>> [0.48,0.83] interval, and disturbances drawn from a zero-mean normal
>>> distribution where the standard deviation was randomly selected from the
>>> [0.005,0.014] interval. With the help of earlier Stata posts, I have come up
>>> with the following:
>>>
>>> ****************************************
>>> clear
>>> version 11.1
>>> quietly set obs 500
>>> set seed 10101
>>> gen t=_n
>>> scalar a = 0.48
>>> scalar b = 0.83
>>> scalar c = 0.005
>>> scalar d = 0.014
>>> gen rho = a+(b-a)*runiform()
>>> gen s = c+(d-c)*runiform()
>>> gen et = rnormal(0, s)
>>> gen yt=.
>>> replace yt=et in 1
>>> forvalues i=2(1)500 {
>>> quietly replace yt=rho*yt[`i'-1]+et[`i'] in `i'
>>> }
>>> line yt t
>>> ****************************************
>>>
>>> I would appreciate if someone kindly proofread the above program for any
>>> mistake(s). Eventually I will use the AR(1) processes to simulate 5%
> critical
>>> values of a statistic for which I will later seek Statalist's help. Thank
> you.
*
* 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/