# [R] How to simulate a variable Xt=Wit+0.5Wit-1 with Wit~U(0,2)

carpan at sapo.pt carpan at sapo.pt
Mon Jan 24 15:38:43 CET 2011

```

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?