0

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))
}
  • Which error do you get? The one which says it can't find `mvrnorm`, or the one which says `np` doesn't exist? – lebelinoz Oct 06 '17 at 05:15
  • Question title is too broad – pogibas Oct 06 '17 at 06:46
  • I corrected the code. It should give two sets of multivariate normal having n number of observations for each set. And I want to do this process N times. So, eventually I should get NXk p values. What have I missed here? How can I get that? – user179439 Oct 06 '17 at 16:21

0 Answers0