e-Mathematics > Probability and Statistics
for 3470-001 student.

Hypothesis Tests

Finding p-value. Once the test statistic

$\displaystyle T = \frac{\bar{X} - \mu_0}{S/\sqrt{n}}
$

is calculated, the p-value can be computed according to the choice of alternative hypothesis.

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 $ \mu$ and $ \sigma$ and the current choice of sample size $ n$, the test statistic $ T$ can be approximated by $ N(\delta,1)$ with

$\displaystyle \delta = \frac{\mu-\mu_0}{\sigma/\sqrt{n}}.
$

Then the power of test can be computed according to the choice of significance level $ \alpha$ and the alternative hypothesis.


© TTU Mathematics