#Parameters to simulate sample 1 from a normal population n1 _ 30 mu1 _ 100 sigma1 _ 10 #Parametets to simulate sample 2 from a normal population n2 _ 30 mu2 _ 110 sigma2 _ 10 #m denotes the number of simulated t-tests m _ 1000 #list of main variables alpha _ .05 type1 _ 0 type2 _ 0 beta _ 0 power _ 0 pvector _ c() tvector _ c() wpvector _ c() wvector _ c() for(i in 1:m) { x _ rnorm(n1,mu1,sigma1) y _ rnorm(n2,mu2,sigma2) t.output _ t.test(y, x, alternative = "two.sided",mu=0,paired=F, var.equal=T, conf.level=.95) w.output _ wilcox.test(y, x, alternative="two.sided", mu=0, paired=F, exact=T, correct=T) #creating vectors for results of the t-tests tstat _ t.output$statistic pvalue _ t.output$p.value tvector _ c(tvector,tstat) pvector _ c(pvector, pvalue) #creating vectors for the results of the wilcox tests wstat _ w.output$statistic wpvalue _ w.output$p.value wvector _ c(wvector, wstat) wpvector _ c(wpvector, wpvalue) } summary(tvector) print(sqrt(var(tvector))) summary(pvector) print(sqrt(var(pvector))) if(mu1-mu2 == 0) type1_length(pvector [pvector < alpha])/m else{type1_0} if(mu1-mu2 != 0) type2_length(pvector [pvector > alpha])/m else{type2_0} if(mu1-mu2 == 0) wil.type1_length(wpvector [wpvector < alpha])/m else{wil.type1_0} if(mu1-mu2 != 0) wil.type2_length(wpvector [wpvector > alpha])/m else{wil.type2_0} power_1-type2 print(type1) print(type2) print(power) #settings for t-test plots x.points_seq(-5,5,.01) y.points_dt(x.points,n1+n2-2) x.pts_x.points+mean(tvector) #plot 1 plot(c(-5,10),c(0,.7),type="n",bty="l",sub="width=1.5") lines(x.points,dt(x.points,n1+n2-2),lty=1) lines(x.pts,y.points,lty=1) #plot 2 hist(tvector,nclass=20,xlim=c(-5,12),ylim=c(0,.6),xlab="", probability=T,col=11) lines(x.points,dt(x.points,n1+n2-2),col=4) lines(x.pts,y.points,lty=1,col=4) legend(-5,.6,c("Simulation Results (alpha = .05)",""," beta =", ""," power =")) hist(wvector,nclass=20,col=5) qqnorm(tvector) qqnorm(wvector) summary(wvector) print(wil.type2) print(1-wil.type2)