Write an R function that will simulate 2 data sets from gamma distributions (``rgamma'' function, this gives us skewed samples) then does a standard t-test comparing the 2 means (``t.test'' function) and returns the p-value. The function should have 2 sample sizes and 2 sets of parameters as input.
Now use the function to simulate a case with small sample sizes and the null hypothesis being true (equal means) and see how the type I error rate is affected by the skewness.
Run some simulations to find a combination of sample sizes and parameter values that will give you between 80% and 95% power.
Run the code below in R:
set.seed(1001)
test <- function(m,n,par1,par2){
sample1 <- rgamma(m,par1)
sample2 <- rt(n,par2)
test <- t.test(sample1,sample2)
return(test$p.value)
}
thres <- 0.05
test.result <- function(pvalue){
ifelse(pvalue<thres,1,0)
}
nnMC <- 2500
power <- function(m,n,par1,par2){
res <- numeric(nnMC)
for(i in 1:nnMC){
t <- test(m,n,par1,par2)
res[i] <- test.result(t)
}
return(mean(res))
}
power(25,35,2,2)
par2.range <- seq(0.5,10,by = 0.05)
power.test <- numeric(length(par2.range))
for(k in 1:length(par2.range)){
power.test[k] <- power(25,35,par2.range[k],2)
}
par2.range[which.max(power.test>0.8)]
par2.range[which.max(power.test>0.95)]
plot(par2.range,power.test)
#Thus in the range(1.3,2.2) the given conditions hold where power
is in (0.80,0.95)
Get Answers For Free
Most questions answered within 1 hours.