I have the following assignment with R:
Simulate 5000 samples of size 15 from the distribution chi.sq with df=5. For each of these samples do the test H0: 𝜇 = 5 vs 𝐻1: 𝜇 <5 with alpha 3%, using the t-test for the mean of a normal population. Next, estimate the exact alpha of the test (ie find the percentage of incorrect rejections of H0). Is it close to 3%? If you had to "correct" the critical value you used in (a), what would it be? Hint: Use 𝑡 = (𝑋̅ - 𝜇0) √𝑛 as a statistical control function and find (by simulation) its distribution when the data comes from the chi square distribution(df=5). Then select an appropriate quantile of this distribution as the critical control point.
My attempt so far:
sims<-2000
alpha<-0.03
n<-15
mu1<-5
X<-matrix(rchisq(n*sims,mu1),ncol=sims,nrow=n)
xbars<-apply(X,2,mean)
stdevs<-apply(X,2,sd)
testTs<-(xbars-mu1)/(stdevs/sqrt(n))
hist(testTs,xlim=c(-4,4),col="salmon",xlab="",probability=T,
ylim=c(0,0.5),main="")
Ucrit<-qt(1-alpha,df=(n-1))
Lcrit<-qt(alpha,df=(n-1))
xs<-seq(-4,4,by=0.05)
ys<-dt(xs,df=(n-1))
lines(xs,ys,lwd=2,col="red")
lines(c(-4,4),c(0,0),lwd=3)
lines(c(Lcrit,Lcrit),c(0,dt(Lcrit,df=(n-1))),lwd=2)
lines(c(Ucrit,Ucrit),c(0,dt(Ucrit,df=(n-1))),lwd=2)
SPU<-length(testTs[testTs>Ucrit])/length(testTs)
SPL<-length(testTs[testTs<Lcrit])/length(testTs)
simPower<-SPU+SPL
simPower
Many thanks for your time!