User Tools

Site Tools


lab:zhang:simulation_codes_on_2010-10-25

normal data

R<-1000
pc<-0

pmu1<-0
pmu2<-0

pS1<-1
pS2<-1

pSX<-1
rho<-.5

sm<-1.1521
sy<-1.175234

set.seed(20100402)
  dir.create('compdata')
for (r in 1:R){
for (ab in c(0, .14, .39, .59)){
	pa<-ab
	pb<-ab
	## Generate complete data
    X<-rnorm(500)
  M<-pmu1+pa*X+rnorm(500)
  Y<-pmu2+pb*M+pc*X+rnorm(500)	
	rho<-.5
  ## auxiliary data
  by<-sqrt(rho^2/(1-rho^2)/sy)
  bm<-sqrt(rho^2/(1-rho^2)/sm)
  V<-bm*M+rnorm(500)
  W<-by*Y+rnorm(500)
      
  ## all data together
  dset<-cbind(X, M, Y, V, W)
  write.table(dset, paste('compdata/500-','ab-',ab,'-r',r,'.txt', sep=''), row.names=F, col.names=F)
  
  ## select data with a fixed sample size
  for (N in c(50,100,200)){  
  ## MCAR data
  rate<-c(0.1,0.2,0.4)
  X<-dset[1:N,1]  
  for (i in 1:3){
    M<-dset[1:N,2]
    Y<-dset[1:N,3]
    M[sample(1:N, N*rate[i])]<-NA
    Y[sample(1:N, N*rate[i])]<-NA
    
    ## create a folder    
    folder.name<-paste('ab',ab,'N',N,'MCAR','M',rate[i],sep='')
    if (!file.exists(folder.name)) dir.create(folder.name)
    ## no auxiliary variables
    out.name<-paste(folder.name,'/data-noav-',r,'.txt',sep='')
    write.table(cbind(X,M,Y), out.name, row.names=F, col.names=F, na='99999')
    ## with auxiliary variables
    out.name<-paste(folder.name,'/data-av-',r,'.txt',sep='')
    write.table(cbind(X,M,Y,dset[1:N,4:5]), out.name, row.names=F, col.names=F, na='99999')    
  }
  ##End of MCAR data
  ## Start of MAR data
  rate<-c(9,4,1.5)
  mrate<- -log(rate)
  X<-dset[1:N,1]
  
  for (i in 1:3){
    M<-dset[1:N,2]
    Y<-dset[1:N,3]
    rate.m<-exp(mrate[i]+X)/(1+exp(mrate[i]+X))
    rate.y<-exp(mrate[i]+X)/(1+exp(mrate[i]+X))
    M[runif(N)<rate.m]<-NA
    Y[runif(N)<rate.y]<-NA
        ## with auxiliary variables V & W
    folder.name<-paste('ab',ab,'N',N,'MAR','M',rate[i],sep='')
    if (!file.exists(folder.name)) dir.create(folder.name)
    ## no auxiliary variables
    out.name<-paste(folder.name,'/data-noav-',r,'.txt',sep='')
    write.table(cbind(X,M,Y), out.name, row.names=F, col.names=F, na='99999')
    ## with auxiliary variables
    out.name<-paste(folder.name,'/data-av-',r,'.txt',sep='')
    write.table(cbind(X,M,Y,dset[1:N,4:5]), out.name, row.names=F, col.names=F, na='99999')
    }
   ## End of MAR data
   ## Start of MNAR1 data
   rate<-c(.1,.2,.4)
  	X<-dset[1:N,1]
  
  for (i in 1:3){
    M<-dset[1:N,2]
    Y<-dset[1:N,3]
    M1<-M
    Y1<-Y
  
    M[M1<quantile(M1, rate[i])]<-NA
    Y[Y1<quantile(Y1, rate[i])]<-NA
        ## with auxiliary variables V & W
    folder.name<-paste('ab',ab,'N',N,'MNAR1','M',rate[i],sep='')
    if (!file.exists(folder.name)) dir.create(folder.name)
    ## no auxiliary variables
    out.name<-paste(folder.name,'/data-noav-',r,'.txt',sep='')
    write.table(cbind(X,M,Y), out.name, row.names=F, col.names=F, na='99999')
    ## with auxiliary variables
    out.name<-paste(folder.name,'/data-av-',r,'.txt',sep='')
    write.table(cbind(X,M,Y,dset[1:N,4:5]), out.name, row.names=F, col.names=F, na='99999')    
  }
   ## End of MNAR1
   ## Start of MNAR2
   X<-dset[1:N,1]

  U<-dset[1:N,4]
  V<-dset[1:N,5]
  
  for (i in 1:3){
    ## with auxiliary variables U & V
    M<-dset[1:N,2]
    Y<-dset[1:N,3]
    M[U<quantile(U, rate[i])]<-NA
    Y[V<quantile(V, rate[i])]<-NA
    
    folder.name<-paste('ab',ab,'N',N,'MNAR2','M',rate[i],sep='')
    if (!file.exists(folder.name)) dir.create(folder.name)
    ## no auxiliary variables
    out.name<-paste(folder.name,'/data-noav-',r,'.txt',sep='')
    write.table(cbind(X,M,Y), out.name, row.names=F, col.names=F, na='99999')
    ## with auxiliary variables
    out.name<-paste(folder.name,'/data-av-',r,'.txt',sep='')
    write.table(cbind(X,M,Y,dset[1:N,4:5]), out.name, row.names=F, col.names=F, na='99999')    
  }
   ## End of MNAR2
  	}
	}
}


runone<-function(filename, par){
    temp<-read.table(filename, fill=T)
    par<-par*par
    if (par==0){
      bias<-100*(mean(temp[,7],na.rm=T)-par)
    }else{
      bias<-100*(mean(temp[,7],na.rm=T)-par)/par
    }
    n<-dim(temp)[1]
    cvg<-mean((temp[,5]<par) & (temp[,9]>par), na.rm=T)*100
    power<-mean((temp[,5]>0) | (temp[,9]<0), na.rm=T)*100
    c(bias, cvg, power)
}




for (ab in c(0, .14, .39, .59)){
	for (N in c(50,100,200)){
		for (miss in c('MCAR','MNAR1', 'MNAR2')){
			for (rate in c(.1,.2,.4) ){
				for (av in c('av','noav')){
		res.name<-paste('ab',ab,'N',N,miss,'M',rate,av,'-em-results.txt',sep='')
		if (file.exists(res.name)){
				cat(ab, N, miss, rate, av, runone(res.name, ab),'\n')    }          
				}
			}
			}
			
		for (miss in 'MAR'){
			for (rate in c(1.5, 4, 9) ){
			    for (av in c('av','noav')){
		res.name<-paste('ab',ab,'N',N,miss,'M',rate,av,'-em-results.txt',sep='')
				if (file.exists(res.name)){
				cat(ab, N, miss, rate, av, runone(res.name, ab),'\n')    }  
				
				}              
				}
			}	
		}
	
	}


Lognormal data

R<-200
pc<-0

pmu1<-0
pmu2<-0

pS1<-1
pS2<-1

pSX<-1
rho<-.5

sm<-1.1521
sy<-1.175234

set.seed(20100402)
  dir.create('compdata')
for (r in 1001:1100){
for (ab in c(0, .14, .39, .59)){
	pa<-ab
	pb<-ab
	## Generate complete data
    X<-rlnorm(500)
  M<-pmu1+pa*X+rlnorm(500)
  Y<-pmu2+pb*M+pc*X+rlnorm(500)	
	rho<-.5
  ## auxiliary data
  by<-sqrt(rho^2/(1-rho^2)/sy)
  bm<-sqrt(rho^2/(1-rho^2)/sm)
  V<-bm*M+rlnorm(500)
  W<-by*Y+rlnorm(500)
      
  ## all data together
  dset<-cbind(X, M, Y, V, W)
  write.table(dset, paste('compdata/500-','ab-',ab,'-r',r,'.txt', sep=''), row.names=F, col.names=F)
  
  ## select data with a fixed sample size
  for (N in c(50,100,200)){  
  ## MCAR data
  rate<-c(0.1,0.2,0.4)
  X<-dset[1:N,1]  
  for (i in 1:3){
    M<-dset[1:N,2]
    Y<-dset[1:N,3]
    M[sample(1:N, N*rate[i])]<-NA
    Y[sample(1:N, N*rate[i])]<-NA
    
    ## create a folder    
    folder.name<-paste('ab',ab,'N',N,'MCAR','M',rate[i],sep='')
    if (!file.exists(folder.name)) dir.create(folder.name)
    ## no auxiliary variables
    out.name<-paste(folder.name,'/data-noav-',r,'.txt',sep='')
    write.table(cbind(X,M,Y), out.name, row.names=F, col.names=F, na='99999')
    ## with auxiliary variables
    out.name<-paste(folder.name,'/data-av-',r,'.txt',sep='')
    write.table(cbind(X,M,Y,dset[1:N,4:5]), out.name, row.names=F, col.names=F, na='99999')    
  }
  ##End of MCAR data
  ## Start of MAR data
  rate<-c(9,4,1.5)
  mrate<- -log(rate)
  X<-dset[1:N,1]
  
  for (i in 1:3){
    M<-dset[1:N,2]
    Y<-dset[1:N,3]
    rate.m<-exp(mrate[i]+X)/(1+exp(mrate[i]+X))
    rate.y<-exp(mrate[i]+X)/(1+exp(mrate[i]+X))
    M[runif(N)<rate.m]<-NA
    Y[runif(N)<rate.y]<-NA
        ## with auxiliary variables V & W
    folder.name<-paste('ab',ab,'N',N,'MAR','M',rate[i],sep='')
    if (!file.exists(folder.name)) dir.create(folder.name)
    ## no auxiliary variables
    out.name<-paste(folder.name,'/data-noav-',r,'.txt',sep='')
    write.table(cbind(X,M,Y), out.name, row.names=F, col.names=F, na='99999')
    ## with auxiliary variables
    out.name<-paste(folder.name,'/data-av-',r,'.txt',sep='')
    write.table(cbind(X,M,Y,dset[1:N,4:5]), out.name, row.names=F, col.names=F, na='99999')
    }
   ## End of MAR data
   ## Start of MNAR1 data
   rate<-c(.1,.2,.4)
  	X<-dset[1:N,1]
  
  for (i in 1:3){
    M<-dset[1:N,2]
    Y<-dset[1:N,3]
    M1<-M
    Y1<-Y
  
    M[M1<quantile(M1, rate[i])]<-NA
    Y[Y1<quantile(Y1, rate[i])]<-NA
        ## with auxiliary variables V & W
    folder.name<-paste('ab',ab,'N',N,'MNAR1','M',rate[i],sep='')
    if (!file.exists(folder.name)) dir.create(folder.name)
    ## no auxiliary variables
    out.name<-paste(folder.name,'/data-noav-',r,'.txt',sep='')
    write.table(cbind(X,M,Y), out.name, row.names=F, col.names=F, na='99999')
    ## with auxiliary variables
    out.name<-paste(folder.name,'/data-av-',r,'.txt',sep='')
    write.table(cbind(X,M,Y,dset[1:N,4:5]), out.name, row.names=F, col.names=F, na='99999')    
  }
   ## End of MNAR1
   ## Start of MNAR2
   X<-dset[1:N,1]

  U<-dset[1:N,4]
  V<-dset[1:N,5]
  
  for (i in 1:3){
    ## with auxiliary variables U & V
    M<-dset[1:N,2]
    Y<-dset[1:N,3]
    M[U<quantile(U, rate[i])]<-NA
    Y[V<quantile(V, rate[i])]<-NA
    
    folder.name<-paste('ab',ab,'N',N,'MNAR2','M',rate[i],sep='')
    if (!file.exists(folder.name)) dir.create(folder.name)
    ## no auxiliary variables
    out.name<-paste(folder.name,'/data-noav-',r,'.txt',sep='')
    write.table(cbind(X,M,Y), out.name, row.names=F, col.names=F, na='99999')
    ## with auxiliary variables
    out.name<-paste(folder.name,'/data-av-',r,'.txt',sep='')
    write.table(cbind(X,M,Y,dset[1:N,4:5]), out.name, row.names=F, col.names=F, na='99999')    
  }
   ## End of MNAR2
  	}
	}
}
lab/zhang/simulation_codes_on_2010-10-25.txt · Last modified: 2016/01/24 09:48 by 127.0.0.1