Dear R community,and especially Giovanni Millo,
For my master's thesis i need to simulate a panel data with the fixed
effects correlated with the predicor, so i run the
the following code:
set.seed(1970)
#######################Panel data simulation with alphai correlated with
xi#####################################
n <- 5
t <- 4
nt <- n*t
pData <- data.frame(id = rep(paste("JohnDoe", 1:n, sep =
"_"), each t),time = rep(1981:1984, n))
rho <-0.95
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)
pData$yi <- 5 + pData$alphai + 5* pData$xcorrei + rnorm(nt)
##########################panel data
frame##################################
library(plm)
pData <- pdata.frame(pData, c("id", "time"))
pData
I think the panel is correctly generated, but my doubt is about the
simulation of the correlated variables:
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))])
This method is correct or is there a better way to do this?
Must generate a variable xi correlated with the alphai, for various
values of rho:
For example rho=(0,0.5,0.6,0.8,0.95,0.99)
how do I simulate the xi associated with each value of rho and put in
the data frame at once?
tried various ways without success.
Please give your opinion and suggestions to improve my simulation. Tank
you,
best regards
Carlos Brás
"Confidencialidade: Esta mensagem (e eventuais ficheiros anexos) é
destinada exclusivamente às pessoas nela indicadas e tem natureza confidencial.
Se receber esta mensagem por engano, por favor contacte o remetente e elimine a
mensagem e ficheiros, sem tomar conhecimento do respectivo conteúdo e sem
reproduzi-la ou divulgá-la.
Confidentiality Warning: This e-mail message (and any attached files) is
confidential and is intended solely for the use of the individual or entity to
whom it is addressed. lf you are not the intended recipient of this message
please notify the sender and delete and destroy all copies immediately."
[[alternative HTML version deleted]]