g<-function(x,y,n){ x**(n/2)*exp(-x/2)-y**(n/2)*exp(-y/2) } n<-5 c1<-0.1 f<-function(x){g(x,c1,n)} c2<-uniroot(f,interval=c(n,100))$root c2 p<-1-pchisq(c2,n)+pchisq(c1,n) myquantile<-function(alpha,n){ repp<-20000 res<-rep(0,repp) resx<-rep(0,repp) for (i in 1:repp){ c1<-0.001*i f<-function(x){g(x,c1,n)} c2<-uniroot(f,interval=c(n,100))$root p<-1-pchisq(c2,n)+pchisq(c1,n) res[i]<-p resx[i]<-0.01*i i<-ifelse(p-0.05<= 0,i,break) } return(c(c1,c2,p)) } myquantile2<-function(alpha,n){ repp<-20000 res<-rep(0,repp) resx<-rep(0,repp) for (i in 1:repp){ c1<-n-0.001*i f<-function(x){g(x,c1,n)} c2<-uniroot(f,interval=c(n,100))$root p<-1-pchisq(c2,n)+pchisq(c1,n) res[i]<-p resx[i]<-0.01*i i<-ifelse(p-0.05>= 0,i,break) } return(c(c1,c2,p)) }