user27808
user27808

Reputation: 121

Simulating AR(1) in Stata from last observation to the first

I want to simulate an AR(1) process, but start from the end. But my code does not work as expected:

clear
set obs 100
gen et=rnormal(0,1)
quietly gen yt= et in L
quietly replace yt=0.5*yt[_n+1]+et in 1/L-1

Your help is really appreciated.

Upvotes: 1

Views: 851

Answers (1)

Nick Cox
Nick Cox

Reputation: 37233

Just do it the normal way and then reverse order:

clear
set obs 100
gen obs = -_n 
gen et=rnormal(0,1)
quietly gen yt = et in 1
quietly replace yt = 0.5*yt[_n-1] + et in 2/L
sort obs 

The key is that Stata works in order of the observations. So, this code works as you would want in cascade, value for observation 2 depending on observation 1, 3 on 2, and so forth.

You won't get a cascade going the other direction.

Also, set seed for reproducibility.

Upvotes: 2

Related Questions