Comments on Homework 2
task: simulate data from a contaminated normal compute power of t and wilcoxon tests vary contamination (p) and sample size (n)
labels?
smoothness?
comparison?
getPower <- function (n, p, N=1000) { tcounter<-wcounter<-0 for (i in 1:N){ mydata <- getData(n,p) T <- t.test(mydata, alternative=c("two.sided")) W <- wilcox.test(mydata, alternative=c("two.sided")) if (T$p.value <= 0.05){ tcounter <-tcounter+1;} if (W$p.value <= 0.05){ wcounter <-wcounter+1;} } #for loop return(list(TPower=tcounter/N, WPower=wcounter/N))}
getData<-function(n,p) { contam <- rnorm(n,3*rbinom(n,1,p),1) return(contam)}
pvector<- seq (0, 1 , by=0.1)
m<-matrix(NA, nrow=length(pvector), ncol=2)
for(i in 1:length(pvector)) { m[i,]<- as.numeric(getPower(50, pvector[i]))}
plot(pvector, m[,1], type="l", xlab="p", ylab="Power", main="n=50", lty=1)
lines(pvector, m[,2], lty=2)
legend("right", legend=c("T-test","Wilcox"), lty=c(1,2))