Hypothesis Tests
Finding p-value. Once the test statistic

- If
(alternative = "two.sided") then
2 * pt(abs(T), n-1, lower.tail=F)
- If
(alternative = "greater") then
pt(T, n-1, lower.tail=F)
- If
(alternative = "less") then
pt(T, n-1, lower.tail=T)
Critical point and p-value. The critical point represents how unlikely it is for the test statistic to go beyond the point under the null hypothesis.
alternative = "two.sided"; n=28; t.statistic = -1.79; alpha = 0.05; par(mfrow=c(2,1)); range = c(-4,4); x = seq(range[1], range[2], length=100); y = dt(x,n-1); plot(x, y, type='l', lwd=1, frame.plot=F, main=paste('Critical point with', alpha)); if(alternative == "two.sided"){ cvalue = qt(alpha/2,n-1,lower.tail=F); xx = seq(range[1], -cvalue, length = 50); polygon(c(xx,max(xx),min(xx)), c(dt(xx,n-1),0,0), col = 'green'); xx = seq(cvalue, range[2], length = 50); polygon(c(xx,max(xx),min(xx)), c(dt(xx,n-1),0,0), col = 'green'); }else if(alternative == "greater"){ cvalue = qt(alpha, n-1, lower.tail=F); xx = seq(cvalue, range[2], length = 50); polygon(c(xx,max(xx),min(xx)), c(dt(xx,n-1),0,0), col = 'green'); }else{ cvalue = qt(alpha,n-1,lower.tail=T); xx = seq(range[1], cvalue, length = 50); polygon(c(xx,max(xx),min(xx)), c(dt(xx,n-1),0,0), col = 'green'); } points(t.statistic, 0, pch=17, col='red') text(t.statistic,0.1, t.statistic, col='red');
The p-value represents the probability that the test statistic could be the particular value under the null hypothesis.
plot(x, y, type='l', lwd=1, frame.plot=F, main=NULL); if(alternative == "two.sided"){ pvalue = 2 * pt(abs(t.statistic), n-1, lower.tail=F) xx = seq(range[1], -abs(t.statistic), length = 50); polygon(c(xx,max(xx),min(xx)), c(dt(xx,n-1),0,0), col = 'blue'); xx = seq(abs(t.statistic), range[2], length = 50); polygon(c(xx,max(xx),min(xx)), c(dt(xx,n-1),0,0), col = 'blue'); points(-t.statistic, 0, pch=17, col='red') }else if(alternative == "greater"){ pvalue = pt(t.statistic, n-1, lower.tail=F); xx = seq(t.statistic, range[2], length = 50); polygon(c(xx,max(xx),min(xx)), c(dt(xx,n-1),0,0), col = 'blue'); }else{ pvalue = pt(t.statistic, n-1, lower.tail=T); xx = seq(range[1], t.statistic, length = 50); polygon(c(xx,max(xx),min(xx)), c(dt(xx,n-1),0,0), col = 'blue'); } points(t.statistic, 0, pch=17, col='red') title(main=paste('P-value is', round(pvalue, digits=5)));
Sample R code. You can download pvalue.R, and run it.
Calculating the power of test.
Given the current estimate of and
and the current choice of sample size
,
the test statistic
can be approximated by
with


- If
then
c.point = qt(alpha/2, n-1, lower.tail=F) pnorm(-c.point, mean=delta, lower.tail=T) + pnorm(c.point, mean=delta, lower.tail=F)
- If
then
c.point = qt(alpha, n-1, lower.tail=F) pnorm(c.point, mean=delta, lower.tail=F)
- If
then
c.point = -qt(alpha, n-1, lower.tail=F) pnorm(c.point, mean=delta, lower.tail=T)
© TTU Mathematics