I want to simulate two sets of data from multivariate normal distribution with some mean vector and covariance matrix. I want N(=3000) simulation with n(=30) number of values for each of k(=100) variables. So there will be two sets of 100 variables having 30 observations. Then I want to do t test between this two sets. I want to do this same process N times. So I should get N p values for each variable i.e p will be a matrix of 2000X100. How can I do this in r? I have a r code but it does not work.
library('MASS')
N=3000
n=30
k=100
cov <- matrix(.5,ncol=k,nrow=k);diag(cov) <- 1
mu1=rep(0,k)
mu2=rep(0,k)
for (j in 1:N){
x=mvrnorm(n, mu1, cov, tol = 1e-6, empirical = FALSE)
y=mvrnorm(n, mu2, cov, tol = 1e-6, empirical = FALSE)
xm<- apply(x,2,mean)
ym<- apply(y,2,mean)
xv<- apply(x,2,sd)
yv<- apply(y,2,sd)
s=sqrt((xv^2*(n-1)+yv^2*(n-1))/(2*n-2))
t=(xm-ym)/(s*sqrt(2/n))
p=2*(1-pt(abs(t),df=2*n-2))
}