Two tailed Z-test

The Z-test is used when you want to compare the means of two large samples (>30 observations). In a two sided Z test, you test the null-hypothesis that µ1 = µ2. Contrary, in a one sided test Z test, you test the null-hypothesis µ1 ≥ µ2 or µ1 ≤ µ2.

You need to check the following assumptions before proceeding with the Z-test:

  1. The observations are independent
  2. The samples have the same variance
  3. That the Central Limit Theorem holds true (it does if the sample sizes > 30)

The Z-test relies on the test statistic Z, which is calculated by:


where  \overline{x}_1 and \overline{x}_2 are the sample means,   s_1^2 and s_2^2  are the sample variances, and  n_1 and n_2 are the sample sizes of sample 1 and 2, respectively.

The null hypothesis is rejected when Z > 1.96 and 2.58 at a significance level of α = 0.05 and 0.01, respectively. That is, you are certain at a degree of 95 and 99 %, respectively, that the null-hypothesis can be rejected, i.e. the means differ.


You want to test if the monthly salaries of industrial workers differ between China and India.

  1. Construct the null-hypothesis

H0: the mean salary in China and India do not differ (µChina = µIndia)

Take a random sample of at least 30 salaries of industrial workers from China and India.

  1. Calculate the mean (\overline{x}) and variance (s^2) for each sample, in this case:

3. Check that the variances are equal
– Perform a F-test, calculate the F statistic

– Calculate the degrees of freedom (v)
v_{china} = 45 - 1 = 44
v_{india} = 32 - 1 = 31

– Check the critical value for F at α=0.05 where v1 = 44 and v2 = 31 in a table of critical F values: Fα=0.05 = 1.8-2.01

– Compare the calculated F statistic with Fα=0.05

F < Fα=0.05 = 1.14 < 1.8

– Reject H0 or H

H0 can’t be rejected; the assumption of equal variances holds true.

4. Calculate the Z statistic:

z test example
– Look up the critical value for Z at α = 0.05

In the case of critical values for Z we don’t have to check a table it is simply Zα=0.05 = 1.96, independent of the degrees of freedom of the samples as the test relies on the Central Limit Theorem.

5. Compare the calculated Z statistic with Zα=0.05

Z > Zα=0.05 = 12.3 > 1.96

Z is even greater than Zα=0.01 = 2.58

6. Reject H0 or H1

H0 can be rejected; the mean salary in China and India differ (µChina ≠ µIndia)

7. Interpret the result

We are more than 99 % certain that the salaries of industrial workers in China are higher compared to India.

How to do it in R

#Check the assumption of equal variances using F-test
       f.test<-function(var.max,var.min) var.max/var.min


#Function to calculate the Z statistic




Two tailed Z-test in depth

The theory behind the Z-test is relies on the Central Limit Theorem and is quite straightforward.

The Central Limit Theorem says that estimations of a population parameter from a large number of samples, with size n, conform to a normal distribution. The population parameter can for example be the mean and the standard deviation. Other parameters can also be considered such as the difference between the means of two populations (d).

In the Z-test you want to know if there is a real difference between the mean of two populations, µpop1 and µpop2.

As the calculated means from samples are estimations and therefore could differ from the true means (µpop1 and µpop2) due to sampling error we need to perform a statistical test (a Z-test). We simply need to find out whether the difference between the estimated means are a true difference or due to chance.

If there is no difference between the means of two populations then d = µpop1 – µpop2 = 0

Every time you estimate a mean from two populations, you can calculate a difference (d) between the means. Just as in the case with means, a large number of d estimated from samples conforms to a normal distribution according to the Central Limit Theorem.

If there is no real difference between the populations, if d = µpop1 – µpop2 = 0, the distribution of d is centered around µ=0 with standard deviation σ. Due to sampling error the estimated d from samples deviates from 0. But, the variability decreases with sample size. Sounds, familiar? Yes, the standard deviation of this distribution is the same as the standard error, the standard error of d (SEd).


Now, this is what you want to find out: does my estimated difference (d) between the means of my two samples belong to a population of d’s where µd=0? i.e. a population of d’s where there is no real difference between the means, where the difference is due to chance.

How do we do that?

We simply calculate the number of standard errors (SEd) between the estimated d and µd = 0.

  1. Calculate the distance between the estimated d and µd = 0.

The bars mean that we take the absolute value of the difference, i.e. we ignore any minus sign. This is because we are not interested in which mean that is greater than the other, just that there is a difference.

2. Divide this distance with the standard error of d (SEd):

se distance z      3. To calculate the number of standard errors between d and µd = 0 we get:

If the Z value exceeds 1.96, the estimated d is not very probable to have been obtained if there is no difference between the means. In other words, the estimated d is far from 0. The chance is less than 5 % that the estimated d belongs to a population of d’s with mean 0.

How to produce the graph in R

	mnorm<-function(my, sigma,x){


	for(i in 1:length(x)) {

	plot(x,p,ylab="",xlab="Difference between means",type="n",las=1,bty="l",pch=19,yaxt="n")



mnorm_plot(0,0.35) #The first argument is the difference between means and the second is the standard error