# [R] How to simulate a variable Xt=Wit+0.5Wit-1 with

Millo Giovanni Giovanni_Millo at Generali.com
Tue Jan 25 15:30:21 CET 2011

```Dear Carlos,

consider that code is hard to read and people might not have the time to

As I told you privately in response to your message on 18/1,

> Re: generating correlated effects, I tried this only once, but I
> didn't get it right. Simulations using this are, e.g., Hansen (2007)
> http://faculty.chicagobooth.edu/christian.hansen/research/panel_cov_t.
> pdf
> and Drukker (2003)
> http://ideas.repec.org/a/tsj/stataj/v3y2003i2p168-177.html
> I suggest you take inspiration from what they did.

so the references are here for the possible benefit (?) of the list too.
In the meantime I looked at your code and it looks more or less OK to
me. I would have generated the correlated effects simply as
x_i=something random + gamma*u_i, but your Choleski approach is much
better. Sorry but a thorough check is beyond my time availability now.

arima.sim() may be a good way to go, I can't tell. The simplest way is
just to use loops: generating (in pseudo-R)

> w <- runif(n, <parameters>)
> x <- <random>

and then

> for(i in 2:n) x[i] <- 0.5*x[i-1]+w[i]

PS remember to generate a sufficiently long sequence of x's *before*
those in your sample, in order to make the initial conditions
irrelevant.

Let me suggest Ch. 7.1 of Kleiber and Zeileis, "Applied Econometrics
with R" for a nice and organized approach to (econometric) simulations
in R.
Other than this, it's just basic R, trying it out and seeing if it
works.

Best wishes,
Giovanni

----------- original message -------------------

Message: 18
Date: Mon, 24 Jan 2011 14:38:43 +0000
From: carpan at sapo.pt
To: r-help <r-help at r-project.org>
Subject: [R] How to simulate a variable Xt=Wit+0.5Wit-1 with
Wit~U(0,2)
Message-ID: <20110124143843.10742vvdq8rvqcxf at mail.sapo.pt>
Content-Type: text/plain; charset=ISO-8859-15; DelSp="Yes";
format="flowed"

Dear all

I simulate a panel data:

n <- 10
t <- 5
nt <- n*t
pData <- data.frame(id = rep(paste("JohnDoe", 1:n, sep = "."), each =
t),time = rep(1981:1985, n))
rho <-0.99#simulate alphai corelated with the xi
print(rho)
alphai <- rnorm(n,mean=0,sd=1)#alphai simulation
x<- as.matrix(rnorm(nt,1))#xi simulation
akro <- kronecker(alphai ,matrix(1,t,1))#kronecker of alphai
cormat<-matrix(c(1,rho,rho,1),nrow=2,ncol=2)#correlation matrix
cormat.chold <- chol(cormat)#choleski transformation of correlation
matrix
akrox <- cbind(akro,x)
ax <- akrox%*%cormat.chold
ai <- as.matrix(ax[,1])
pData\$alphai<-as.vector(ai)
xcorr <- as.matrix(ax[,2:(1+ncol(x))])
pData\$xcorrei<-as.vector(xcorr)
library(plm)
pData<-plm.data(pData, index = c("id", "time"))
pData

But now i need a variable Xt=Wit+0.5Wit-1 with Wit~U(0,2),  the code i
Try to use is:

for (i in 1:n) {
p <- i*t
m <- (i-1)*t+1
for (j in m:p){
xt<-arima.sim(n=nt, list(ar=c(0.5)))               }
}
Is this the correct  way to simulate the AR(1), without the assumption
Wit~U(0,2)? How i simulate the variable with the assumption
Wit~U(0,2), and put it in my dataframe correctly?

regards

Carlos Br?s
Statistics Portugal-INE

---------- end original message -------------

Giovanni Millo
Research Dept.,
Assicurazioni Generali SpA
Via Machiavelli 4,
34132 Trieste (Italy)
tel. +39 040 671184
fax  +39 040 671160

Ai sensi del D.Lgs. 196/2003 si precisa che le informazi...{{dropped:13}}

```