Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: AR1

From   "Michael Blasnik" <>
To   <>
Subject   st: Re: AR1
Date   Mon, 03 Oct 2005 08:31:40 -0400

----- Original Message ----- From: "Dirk Nachbar" <>
To: "statalist" <>
Sent: Monday, October 03, 2005 4:43 AM
Subject: st: AR1

Dear all

I am trying to simulate an AR(1) process but it proves to be hard in Stata. My
idea was to declare a matrix and then do it in a loop, but it doesn't work.
Maybe someone can help.


set matsize 800
set obs 700
gen time=_n
tsset time
gen y=invnorm(uniform())
mkmat y
local i = 1
while `i' < `obs' {
matrix y[i+1,1]=1+0.5*y[i,1] +invnorm(uniform())
local i = `i' + 1
svmat y
arima y, arima(1,0,0)

Dirk Nachbar
1) It's not very helpful to say "but it doesn't work" -- if you want help you should say why it doesn't work -- did Stata give you an error? Were the results not what you expected? For the code you list, I'd guess the problem may be that `obs' is never defined as a local macro, so it is blank. The set obs command does not create a macro called obs.

2) You say that simulating AR(1) "proves to be hard in Stata". I'd say the correct sentence is "proves to be hard for me". Why do you create a variable, turn it into a matrix, use a loop to alter the matrix, and then turn the result back into a variable. All of this can be done just using a variable and save you many steps, avoid looping, and avoid matrix size constraints.

set obs 700
gen time=_n
gen y=invnorm(uniform())
replace y=1+0.5*y[_n-1] +invnorm(uniform()) in 2/l
tsset time

You may want to set seed at the beginning to have replicable results. Also, I left your formula as is, but I'm not sure you really want the first value to be smaller than the others (you might want to drop the 1+ at the beginning and multiply the random term by 0.5?)

Michael Blasnik

* For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index