0% found this document useful (0 votes)
28 views

QM2 Tutorial 3

1) The document provides instructions for students to complete Tutorial 3 exercises in the ECON20003 quantitative methods course, including downloading data files and completing assessments by certain deadlines. 2) The first exercise asks students to estimate the mean hours per week high school students spend working part-time jobs using a 95% confidence interval, based on sample data of 200 students. 3) Checking the assumption of normality is important when constructing confidence intervals for the population mean. Students are instructed to check normality visually with histograms and QQ plots of the sample data, and numerically with descriptive statistics.

Uploaded by

ducminhlniles
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

QM2 Tutorial 3

1) The document provides instructions for students to complete Tutorial 3 exercises in the ECON20003 quantitative methods course, including downloading data files and completing assessments by certain deadlines. 2) The first exercise asks students to estimate the mean hours per week high school students spend working part-time jobs using a 95% confidence interval, based on sample data of 200 students. 3) Checking the assumption of normality is important when constructing confidence intervals for the population mean. Students are instructed to check normality visually with histograms and QQ plots of the sample data, and numerically with descriptive statistics.

Uploaded by

ducminhlniles
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

ECON20003 – QUANTITATIVE METHODS 2

TUTORIAL 3

Download the t3e1, t3e2, t3e3, t3e5 and t3e7 Excel data files from the subject website and
save them to your computer or USB flash drive. Read this handout and complete the tutorial
exercises before your tutorial class so that you can ask help during the tutorial if necessary.

After the tutorial class attempt the “Exercises for assessment”. For each assessment
exercise type your answer in the corresponding box available in the Quiz. If the exercise
requires you to use R, insert the relevant R/RStudio script and printout in the same Quiz box
below your answer. To get the tutorial mark on week 4, you must submit your answers to
these exercises in the Tutorial 3 Canvas Quiz by 10am Wednesday in week 4 and attend
Tutorial 4.
.

Introduction to Statistical Inference

In the next two exercises we are going to explore how to construct an interval estimate of a
population mean and to test hypotheses about a population mean.1

Exercise 1 (Selvanathan, p. 402, ex. 10.62)

A growing concern for educators in Australia is the number of teenagers who have part-time
jobs while they attend high school. It is generally believed that the amount of time teenagers
spend working is deducted from the amount of time devoted to schoolwork. To investigate
this problem, a school guidance counsellor took a random sample of 200 15-year-old high
school students and asked how many hours per week each worked at a part-time job (WH).
The results are stored in the t3e1.xlsx file. Estimate with 95% confidence the mean amount
of time all 15-year-old high school students devote per week to part-time jobs.

Launch RStudio, create a new project and script, and name both t3e1. Import the data saved
in the t3e1 Excel data file to RStudio, save the data set in an RData file and attach it to the
project.

The population standard deviation is unknown. However, the sample size is quite large (n =
200) so, unless the sampled population is extremely non-normal, we can develop the
confidence interval for the population mean of the amount of time teenagers spend working
using the following formula:

x  t /2,n1sx

1
You are supposed to be familiar with these basic inferential statistical procedures as you learnt about them
in QM1. Read the Review 2 handout if you are uncertain whether your knowledge is still up to date.
1
ECON20003 - Tutorial 3
To make sure that we do not use this formula erroneously, we need to focus on normality
first.2 In the lectures on week 2 we discussed that normality can be checked (i) visually by
graphs like e.g. a histogram and a (normal) quantile-quantile (QQ) plot, (ii) numerically by
considering descriptive measure, namely the mean and the median, skewness and kurtosis,
and their standard errors, and (iii) by performing tests of normality, such as the Shapiro-Wilk
test. Let’s consider these checks one-by-one.

(i) Checking normality visually with a histogram and/or QQ plot

In Tutorial 2 you learnt how to develop a histogram with RStudio. Following similar steps
as in Exercise 3 of Tutorial 2, you should be able to create the histogram on the top of
the next page.

We can make this plot more informative by plotting the relative frequencies, rather than
the frequencies. The hist function has an argument, called freq, that we have not
considered yet. It is a true/false logical argument for plotting the frequencies (freq =
TRUE) or the relative frequencies (freq = FALSE).

Histogram of WH
70
60
50
Frequency

40
30
20
10
0

0 5 10 15 20

WH

By default, the hist command assumes that we are interested in the frequencies, so to
display the relative frequencies (density), you need to use the freq = FALSE argument.
The

hist(WH, freq = FALSE, col = "cyan")

command returns the following plot:

2
Although the t distribution provides an adequate approximate sampling distribution of the sample mean when
the population standard deviation is unknown and the population is moderately non-normal, we cannot
overlook this problem by simply assuming that the population of WH is only moderately non-normal.
2
ECON20003 - Tutorial 3
Histogram of WH

0.15
0.10
Density

0.05
0.00

0 5 10 15 20

WH

These two histograms look pretty much the same but notice that on the first the counts
(Frequency) are measured on the vertical axis, while on the second the relative
frequencies (Density).

In addition, it is useful to overlay the new histogram with a normal curve that is
characterised by the same mean and standard deviation than the sample data. This can
be achieved by the combination of three R functions. The first is lines(), that you already
learn about in Tutorial 2.

The second is:

seq(from, to, by)

generates a regular sequence of numbers, where ‘from’ is the first number, ‘to’ is the last
number and ‘by’ is the increment of the sequence.

In this case, we need to cover the range of WH values in the sample. To do so, we can
rely on the horizontal axis of the histogram generated by R and set from and to equal to
0 and 20, respectively. As for the increment of the sequence, in order to obtain a smooth
normal curve, it is recommended to generate a few hundreds of numbers, so we can set
by equal to 0.1.

The third function is:

3
ECON20003 - Tutorial 3
dnorm(x, mean, sd)

returns the value of the probability density function (pdf) of the normal distribution for random
variable x with mean equal to ‘mean’ (the default value is 0) and standard deviation equal to
‘sd’ (the default value is 1).

Hence, the

lines(seq(0, 20, by = 0.1),


dnorm(seq(0, 20, by = 0.1), mean(WH), sd(WH)),
col= "red")

command instructs R to (i) consider the sequence of numbers from 0 to 20 with


increment 0.1, (ii) plot a normal curve over this range assuming that the mean and the
standard deviation are equal to those of WH, and (iii) colour this normal curve red.

This command produces the plot below. It shows that the sample of WH is skewed to
the right, so it is not normally distributed. Consequently, there is no reason to believe
that the population of WH is normal.

Histogram of WH
0.15
0.10
Density

0.05
0.00

0 5 10 15 20

WH

An alternative graphical tool for normality is the so-called quantile-quantile (QQ) plot. It
is basically a scatter plot that depicts the cumulative relative frequency distribution of
the sample data against some known cumulative probability distribution. When it is used
for checking normality, the reference distribution is a (standard) normal distribution and
if the sample data is normally distributed, the points on the scatter plot lie on a straight
line.
4
ECON20003 - Tutorial 3
In R,

qqnorm(x)

produces a normal QQ plot for variable x.

To make the visual evaluation of the QQ plot easier, it is recommended to follow the previous
command with

qqline(x)

that adds a straight line passing through the first and third quartiles to the QQ plot. If the
sample data is more or less normally distributed, the points on the QQ plot are on or close
to this reference line, while substantial departures from it are indicative of a lack of normality.

To create the normal QQ plot for WH, execute the following commands:

qqnorm(WH, main = "Normal Q-Q Plot",


xlab = "Theoretical Quantiles", ylab = "Sample Quantiles",
col = “forestgreen”)
qqline(WH, col = "steelblue")

On your Plots tab you should have the following plot:

5
ECON20003 - Tutorial 3
Since there are only 14 different WH values in this sample of size 200, the QQ plot looks
like the graph of a step function. More importantly, the points tend to be below the
reference line for negative theoretical quantiles and above it for positive theoretical
quantiles. Hence, it seems unreasonable to assume that the population of WH is
normally distributed.

The evaluations of these graphs are somewhat subjective and hence it is useful to
supplement these visual checks with numerical checks.

(ii) Checking normality numerically with descriptive measures

There are three simple numerical tools for normality. First, recall that the normal
distribution is symmetrical, thus its mean and median are equal, and that these and
other descriptive statistics for WH can be obtained by executing the following
commands:

library(pastecs)
round(stat.desc(WH),3)
They return the following printout:

The sample mean of WH is 5.125 and its sample median is 5.000. They are not equal
to each other, but very similar. Their ratio is 5.125 / 5 = 1.025, so the sample mean is
only 2.5% larger than the sample median. Hence, the distribution of WH might be
symmetrical.

Second, we can make use skewness (SK), which is concerned with the asymmetry of a
distribution around its mean. It is measured by the third standardized moment,

 X   3 
SK  E  
   

For symmetric distributions SK = 0, for distributions that are skewed to the right SK > 0
and for distributions that are skewed to the left SK < 0. SK can be estimated with

 x  x
n 3

SK i 1 i

ns 3

where x-bar and s are the sample mean and sample standard deviation, respectively.

6
ECON20003 - Tutorial 3
The estimated approximate standard error of this statistic is

6
 
sSK
n

and the data are likely asymmetric, thus non-normal (at  = 0.05), when


SK

SK  2 s SK which implies 1

2sSK

Third, we can make use of kurtosis (K), which is related to the thickness of the tails of
the distribution. It is measured by the fourth standardized moment,

 X   4 
K  E  
   

For normal distributions K = 3, so K – 3, the so-called excess kurtosis, is zero. For


leptokurtic distributions K > 3 and K – 3 > 0, and for platykurtic distributions K < 3 and K
– 3 < 0.

K can be estimated with3

 x  x
n 4

K i 1 i

ns 4

The estimated approximate standard error of this statistic is4

24
sK 
n

and the data are likely non-normal (at  = 0.05), when

Kˆ  3
Kˆ  3  2 s K which implies 1
2sK

Skewness and kurtosis cannot be estimated with the base R program, but they can be
obtained by the stat.desc command. As we saw, by default, this command returns some
basic statistics (basic = TRUE provides nbr.val, nbr.null, nbr.na, min, max, range, sum)
and descriptive statistics (desc = TRUE provides median, mean, SE.mean,
CI.mean.0.95, var, std.dev, coef.var), but it can also report additional normal distribution
statistics.

3
There are alternative estimators for SK and K, but for the sake of simplicity we use the estimators provided
in the pastecs package.
4
The comparison of the standard error formulas shows that sK  2sSK

7
ECON20003 - Tutorial 3
Namely,

stat.desc(x, basic = TRUE , desc = TRUE, norm = TRUE, p = 0.95)

where x is the variable of interest, basic, desc and norm are three logical variables (with
default values true, true and false) for basic statistics, descriptive statistics and normal
distribution statistics, respectively, and p is the probability level for a confidence interval on
the mean (the default value is 0.95). In particular, basic = TRUE provides nbr.val, nbr.null,
nbr.na, min, max, range, sum, desc = TRUE provides median, mean, SE.mean,
CI.mean.0.95, var, std.dev, coef.var), and norm = TRUE returns skewness, excess kurtosis
and the ratios of their point estimates to twice of the corresponding estimated standard
errors.

To illustrate this, execute the following commands:

library(pastecs)
round(stat.desc(WH, basic = TRUE, desc = TRUE, norm = TRUE), 3)

They return:

As you can see, skewness is 0.778. It is positive suggesting that this sample of WH is
skewed to the right, a conclusion we have already reached based on the histogram.
Excess kurtosis is 2.278. It is positive, and hence kurtosis = 2.278 + 3 = 5.278 is larger
than 3, suggesting that this sample of WH is leptokurtic, i.e., has fatter tails than the
normal distribution.

The point estimates of SK and K imply that this sample of WH is certainly not normally
distributed. Note, however, that this does not allow us to claim that the population of WH
is not normal either. To do so, we need to consider their standard errors as well.

Estimates of the approximate standard errors are

6 6 24 24
 
sSK   0.173 and sK    0.346
n 200 n 200

implying that

8
ECON20003 - Tutorial 3
SK 0.778 Kˆ  3 2.278
  2.249 and   3.292
2 s SK
 2  0.173 2 s K 2  0.346

respectively. On the previous R printout these ratios are reported as skew.2SE = 2.262
and kurt.2SE = 3.329. Notice that they are a bit larger than the ones we calculated
because we used approximate standard errors while R used the exact ones.5 Still, they
are all above one, indicating at the 5% significance level that S is different from zero and
K is different from 3, and hence the population of WH is unlikely normal.

(iii) Checking normality with hypothesis tests

There are several hypothesis tests for the null hypothesis of normality. In this course,
we rely on the Shapiro-Wilk (SW) test, which has good statistical properties compared
to other normality tests and is easy to implement with R.

We do not discuss the details of this test and we shall always perform it with R. It is
enough to remember that R reports the test statistic and the p-value, and the null
hypothesis is rejected if the p-value is smaller than the selected significance level.

The SW test can be performed with the

shapiro.test(WH)

command, which returns

Shapiro-Wilk normality test

data: WH
W = 0.92484, p-value = 1.338e-08

The p-value6 of this test is 1.338e-08 = 0.00000001338. It is practically zero, so we can


reject the null hypothesis of normality at any reasonable significance level.

This time it was unnecessary to execute the shapiro.test command because we already
have the rounded values of the W test statistic and p-value. If you go back to the
previous page, you can see that on the stat.desc printout normtest.W = 0.925 is the test
statistic and normtest.p = 0.000 is the p-value of the SW test.

6n(n  1) n2  1
5
The exact standard errors are s   and s   2 s  . Although at
SK
(n  2)( n  1)( n  3) K SK
( n  3)( n  5)
small sample sizes the approximate standard errors can be inaccurate, we keep using them in manual
calculations for the sake of simplicity.
6
Recall that the p-value of a test is the probability of observing a value of the test statistic as extreme as, or
more extreme (in the appropriate direction) than the one calculated from the sample, given that H0 is correct.
In other words, the p-value is the smallest significance level at which H0 can be rejected. If it happens to be
smaller than the previously selected  value, then we reject H0, otherwise we maintain it. In this case, the p-
value is 1.338e-08 = 0.00000001338.
9
ECON20003 - Tutorial 3
All things considered, there is no ground to assume that the population of WH is normally
distributed. Note, however, that the histogram does not exhibit extreme non-normality.
Hence, with some caveat, at the given large sample size (n = 200) we are going to use the

x  t /2,n1sx

formula to develop the required 95% confidence interval for the mean amount of time all 15-
year-old high school students devote per week to part-time jobs.

The confidence interval estimate is based on the sample mean and its standard error. We
already know the sample mean of WH (5.125) and from the sample standard deviation
(3.310) and the sample size (200) we can estimate the standard error of the sample mean:

s 3.310
sx    0.234
n 200

We already have this on the stat.desc printout (see it on the previous page): S.E. mean =
0.234.

The third term in the formula of the confidence interval is the (1-/2)  100% percentile of
the t-distribution with n -1 degrees of freedom. It is provided by the R quantile function of the
Student t distribution,

qt(“1-alpha/2”,”df”)

In this case, /2 = 0.025, 1-/2 = 0.975, df = n – 1 = 199, and the

qt(0.975,199)

command returns 1.971957.

Therefore,

x  t1 /2,n1sx  5.125  (1.972)(0.234)  5.125  0.461  (4.664;5.586)

Return to the stat.desc printout on page 14. As you can see, CI.mean.0.95 = 0.461 is the t
percentile times the estimate of the standard error, that is half of the width of the confidence
interval. Hence, to develop the 95% confidence interval, you just need the stat.desc printout
and to subtract/add CI.mean.0.95 from/to mean.7

All in all, we conclude with 95% confidence that the mean number of hours all 15-year-old
high school students devote per week to part-time jobs is within the (4.664; 5.586) interval.
Quit RStudio and save your RData and R files.

7
In fact, there is an even simpler way to obtain a confidence interval for a population mean with R. We shall
consider it in the next exercise.
10
ECON20003 - Tutorial 3
Exercise 2

In quality control applications of hypothesis testing, the null and alternative hypotheses are
frequently specified as

H0: The production process is performing satisfactorily.


HA: The process is performing in an unsatisfactory manner.

Accordingly, α, the probability of Type I error, is sometimes referred to as the producer’s


risk, while β, the probability of Type II error, is called the consumer’s risk.

An injection moulder produces plastic golf tees. The process is designed to produce tees
with a mean weight of 0.250 ounce. To investigate whether the injection moulder is operating
satisfactorily, 40 tees were randomly sampled from the last hour’s production. Their weights
(in ounces)8 are saved in the t3e2.xlsx file.

Do the data provide sufficient evidence at the 1% significance level to conclude that the
process is not operating satisfactorily?

Launch RStudio, create a new project and script, and name both t3e2. Import the data saved
in the t3e2 Excel data file to RStudio.

The variable of interest is the Weight of a tee, and the null and alternative hypotheses are

H0 :   0.25 , HA :   0.25

Both the shape and the standard deviation of the population of tee weights are unknown.
However, the sample size is reasonably large (n = 40), so the sample mean is approximately
normally distributed. Consequently, granted that the population is not extremely non-normal,
the test statistic is

X   x ,0
t  tn 1
sx

Let’s start again with normality. We can check for normality like in Exercise 1.

Execute the following commands to develop a relative frequency histogram with a normal
curve superimposed on it:

attach(t3e2)
hist(Weight, freq = FALSE,
col = "lightslateblue")
lines(seq(0.245, 0.257, by = 0.0001),
dnorm(seq(0.245, 0.257, by = 0.0001), mean(Weight), sd(Weight)),
col="red")

8
1 ounce (oz) is equal to 28.34952 grams (g).
11
ECON20003 - Tutorial 3
Histogram of Weight

250
200
Density

150
100
50
0

0.248 0.250 0.252 0.254 0.256

Weight

Next, run the following commands to develop a normal QQ plot:

qqnorm(Weight, main = "Normal Q-Q Plot",


xlab = "Theoretical Quantiles", ylab = "Sample Quantiles",
pch = 19, col = "salmon")
qqline(Weight, col = "royalblue4")

Normal Q-Q Plot


0.256
0.250 0.252 0.254
Sample Quantiles

0.248

-2 -1 0 1 2

Theoretical Quantiles

12
ECON20003 - Tutorial 3
Finally, obtain descriptive statistics and perform the SW test by executing the following
commands:

library(pastecs)
round(stat.desc(Weight, basic = FALSE, desc = TRUE, norm = TRUE), 4)

The relevant printout is:

The histogram is slightly skewed to the left and the QQ plot resembles to a step function
due to the relatively small number of different Weight values in the sample. However, the
histogram is not very skewed, the sample mean (0.252) and the sample median (0.253) are
almost equal, the skew.2SE and kurt.2SE values (-0.590 and -0.170) are smaller than one
in absolute value, and the p-value of the Shapiro-Wilk test (0.076) is larger than 0.05.
Consequently, there is no reason to believe that the population of Weight is not normally
distributed.

The critical values from the t table are  t/2,n-1 = t0.005,39  t0.005,40 = 2.704, or using the

qt(0.995,39)

command, a bit more accurate values are 2.707913. Hence, we can reject H0 at the 1%
significance level if the absolute value of the observed test statistic is larger than 2.708.

The observed value of the test statistic can be calculated from the sample mean (0.2525),
the hypothesized mean value (0.2500) and the estimated standard error of the sample mean
(0.0004). Note, however, that this time the reported SE.mean is rounded to the 4th decimal
and has only one non-zero digit. For this reason, it is better not to rely on it but use the
sample standard deviation (0.0022) and the sample size (40) to calculate the observed test
statistic:

x  0 x  0 0.2525  0.2500
tobs     7.187
sx s/ n 0.0022 / 40

Since it is above the upper critical value, we reject H0 and conclude at the 1% significance
level that the process is not operating satisfactorily.

13
ECON20003 - Tutorial 3
We can conduct a one-sample t-test on the population mean of variable x in R with the

t.test(x, mu = mu0, alternative = " ")

command, where mu0 is the hypothesized population mean value under the null hypothesis
and alternative is one of "two.sided" (default), "greater" or "less".9 It not only conducts a t-
test but provides the corresponding confidence interval estimate as well. By default, the test
is performed at the 5% significance level, which implies a 95% confidence level. If the
significance level is different from 5%, the corresponding confidence level needs to be
specified as a decimal by an additional argument of the t.test command, conf.level.

This time, 0 = 0.25, the test is two sided and the significance level is 0.01, so the confidence
level is 0.99. Hence, we need to execute

t.test(Weight, mu = 0.25, conf.level = 0.99)

It generates the printout displayed on the top of the next page.

It shows the variable of interest (Weight), the alternative hypothesis (true mean is not equal
to 0.25), the test statistic value (t = 7.0188), which is slightly different from the one we
obtained manually due to rounding errors, the degrees of freedom (df = 39), and the p-value
(2.019e-08). Since the p-value is practically zero, we can reject H0 at any reasonable
significance level and conclude that the process is not operating satisfactorily.

One Sample t-test

data: Weight
t = 7.0188, df = 39, p-value = 2.019e-08
alternative hypothesis: true mean is not equal to 0.25
99 percent confidence interval:
0.2515201 0.2534299
sample estimates:
mean of x
0.252475

Notice, that the 99% confidence interval estimate is also provided on the printout. It shows
that with 99% confidence the mean weight of tees is between 0.2515201 and 0.2534299
ounces.

Quit RStudio and save your RData and R files.

Let’s return to Exercise 1 for a minute.

9
As you will see next week, this command can be used to perform two sample t-tests as well.
14
ECON20003 - Tutorial 3
Exercise 1 (cont.)

Earlier you developed a 95% confidence interval for the mean number of hours all 15-year-
old high school students devote per week to part-time jobs both manually and with the
stat.desc command. Use this time the t.test command.

Launch RStudio and open the t3e1.R file.10 On the Files tab click t3e1.RData to reload the
data in your project. Run the first command, attach(t3e1), and then

t.test(WH)

You should get the following printout:

The top part of this printout shows the result of a two-tail t-test with zero hypothesized
population mean (this is the default). Do not worry about it as it was not required in this
exercise. The relevant part of the printout is the 95% confidence interval, (4.6635; 5.5865).

Nonparametric Tests for a Population Central Location

Hypotheses about the central location of a quantitative population are usually best tested
with the Z / t tests for the population mean. These tests are based on four assumptions:

i. The data is a random sample of independent observations.


ii. The variable of interest is quantitative and continuous …
iii. … and is measured on an interval or ratio scale.
iv. Either (Z test) the population standard deviation, , is known and the sample mean
is at least approximately normally distributed (because the sampled population itself
is normally distributed or the sample size is large and thus CLT holds), or (t-test) 
is unknown but the sampled population is normally distributed (approximately at
least).

These assumptions are not always satisfied. For example, when the observations are
measured on an ordinal scale, the mean does not exist, and the central location of the data
can be captured only by the median. Or, even if the sample mean exists, the sample median
is a more appropriate measure of central location when the sample at hand likely contains
one or more observations that are very small or large relative to the rest of the population.

10
If you failed to save this script earlier, then you need to import again the data saved in the t3e1 Excel file to
RStudio.
15
ECON20003 - Tutorial 3
Moreover, the Z / t tests can be misleading when the population standard deviation is
unknown, the population is strongly non-normal, and the sample size is relatively small.

In these cases, we should use some alternative procedures, some nonparametric tests,
which focus on the median instead of the mean and are based on weaker distributional
assumptions than the Z / t tests. We consider two possible alternatives, the one sample sign
test for the median and the one sample Wilcoxon signed ranks test for the median.

The one-sample sign test assumes that

i. The data is a random sample of independent observations.


ii. The variable of interest is qualitative or quantitative.
iii. The measurement scale is at least ordinal.

The test is based on the signs of the observed deviations from the hypothesized population
median. Let S- and S+ denote the numbers of negative and positive deviations and n the
number of non-zero deviations (i.e. S- + S+). If H0 is true and the selection of the sample
items is random, S- and S+ follow a binomial distribution (B) with n and p = 0.5 parameters.11

Since the sum of S- + S+ is fixed (n), one could use either of them as the test statistic, S.
However, to avoid ambiguity, we use S = S+. Then, the p-value is

(i) right-tail test: pR = P(S  S+),


(ii) left-tail test: pL = P(S  S+),
(iii) two-tail test: 2  min(pR ; pL).

In each case, reject H0 if the p-value is smaller than the selected significance level.

When the sample size is small, the p-value can be obtained from the binomial table (Table
1 in Appendix B of the Selvanathan book), while for n  10 this binomial distribution can be
approximated with a normal distribution (N),12


B  n , 0.5   N 0.5 n , 0.5 n 

The one-sample Wilcoxon signed ranks test serves the same purpose as the sign test, but
some of the assumptions required by this test are stronger. Namely, the one-sample
Wilcoxon signed ranks test assumes that

i. The data is a random sample of independent observations.


ii. The variable of interest is quantitative and continuous.
iii. The measurement scale is interval or ratio.
iv. The distribution of the sampled population is symmetric.

11
The sign test is actually a binomial test with p = 0.5.
12
Most likely you learnt about the normal approximation to the binomial distribution in QM1. If not, or if you do
not remember it, you can read about this procedure in the Review 3 file and in Appendix 8.A of Selvanathan
(pp. 335-338).
16
ECON20003 - Tutorial 3
This test takes into consideration not only the signs of the deviations from the median but
their magnitudes as well. For this reason, it is more powerful than the sign test, granted that
the required conditions are met.

The Wilcoxon signed ranks test is based on the ranks of the absolute values of the non-zero
deviations from the hypothesized population median. Let n again denote the number of non-
zero deviations and T_ and T+ the sums of the ranks that belong to negative deviations and
to positive deviations, respectively. Their sum is
n (1  n )
T  T 
2
and when the null hypothesis is correct,
n (1  n )
T  T 
4
The test statistic is denoted as T and we use a similar convention as in the sign test, namely,
T = T+. It has a non-standard sampling distribution, but for 6  n  30 the critical values (TL
and TU) are tabulated in Table 9, Appendix B of the Selvanathan book. Using these critical
values, reject H0 if

(i) right-tail test: T ≥ TU,,


(ii) left-tail test: T ≤ TL,,
(iii) two-tail test: T ≥ TU,/2 or T ≤ TL,/2.

When the number of non-zero deviations is large, n > 30, and thus the critical values are not
available, the sampling distribution of T can be approximated with a normal distribution,

n ( n  1) n ( n  1)(2 n  1)
T  N  T ,  T  , T  ,  T2 
4 24

Exercise 3 (Selvanathan, p. 509, ex. 12.49)

A courier service in Brisbane advertises that its average delivery time is less than six hours
for local deliveries. A random sample of the amount of time this courier takes to deliver
packages to an address across town produced the delivery times (DT, rounded to the
nearest hour) saved in the t3e3 Excel file.

Does this sample provide sufficient evidence to support the courier’s advertisement, at the
5% level of significance?

If we denote the population mean of the delivery times of this courier service as , then the
task is to perform a test with

H0 :   6 vs. HA :   6

Since the variable of interest is quantitative but the population standard deviation is
unknown, in principle we can perform a t test, granted that the sampled population is at least
approximately normally distributed.

17
ECON20003 - Tutorial 3
Launch RStudio, create a new project and script, and name both t3e3. Import the data saved
in the t3e3 Excel data file to RStudio. Save the data as t3e3.RData.

As you can check on the Environment tab, the sample size is only 10, far too small to check
normality with reasonable certainty. For this reason, it is better to rely on a nonparametric
procedure, the sign test or/and the Wilcoxon signed ranks test.13

Accordingly, we rewrite the hypotheses as

H 0 :  6 vs. H A :  6

For the sake of illustration, let’s perform both tests, first manually and then with R.

a) One-sample sign test

This test is based on the signs of the observed non-zero deviations from the
hypothesized population median, i.e. 6. Since this is a left-tail test, we reject the null
hypothesis if the number of negative deviations is sufficiently large and hence the
number of positive deviations is sufficiently small.

The calculations are shown below. The sample observations are in the first column, the
hypothesized median in the second column and the deviations in the third column.

DT Median under H0 Deviation


7 6 1
3 6 ‐3
4 6 ‐2
6 6 0
10 6 4
5 6 ‐1
6 6 0
4 6 ‐2
3 6 ‐3
8 6 2

There are five negative deviations and three positive deviations, so S- = 5, S+ = 3, and
the number of nonzero deviations is n* = 8. S- is larger than S+, in accordance with HA,
but it might not be large enough to reject H0 at the 5% significance level.

The test statistic is S = S+ = 3, and since this is a left-tail test, the p-value from the
binomial table of Selvanathan (Table 1, Appendix B, pp. 1089-1092, n = 8, p = 0.5) is

pL  (S  3)  0.3633

13
Having said that, it is important to recall that the Wilcoxon signed ranks test assumes that the sampled
population is symmetric, an assumption that we cannot verify either this time.
18
ECON20003 - Tutorial 3
This p-value is far above the selected significance level ( = 0.05), so we maintain the
null hypothesis at the 5% significance level and conclude that the average delivery time
for local deliveries might be six hours or more, contradicting the courier’s advertisement.

As mentioned earlier, the sign test is a special case of the binomial test with p = 0.5.
Hence, we can perform the sign test with the

binom.test(x, n, p = p0, alternative = " ")

R command, where x is the number of successes (i.e. S), n is the number of trials (i.e. the
number of non-zero deviations, n*), p0 is the probability of success under H0 (the default
value is 0.5) and alternative is one of "two.sided" (default), "greater" or "less". By default,
the test is performed at the 5% significance level, which is equivalent with a 95% confidence
level. If the significance level is different from 5%, the corresponding confidence level needs
to be specified as a decimal by the conf.level additional argument.

In this case x = 3, n* = 8, the significance level is 5% and the test is a left-tail test. Thus,
you need to execute

binom.test(3, 8, alternative = “less”)

It generates the following printout:

This printout shows that (i) this is a left-tail test, (ii) in the sample the proportion or
probability of success is 3/8 = 0.375, (iii) the 95% confidence interval14 for the true
proportion of success is about (0.000; 0.711), and (iv) the p-value is 0.3633, the same
that we obtained from the binomial table.15

The binom.test command offers a convenient way to perform the sign test with R when the
number of successes is known, but it cannot handle the raw data. In this latter case, we can
rely on the

SignTest(x, mu = mu0, alternative = " ")

14
Since the test is left-sided, this confidence interval for the population proportion is actually a one-sided
confidence interval and its lower limit is zero.
15
Depending on the alternative argument, binom.test always reports the proper p-value for right-tail, left-tail
and two-tail tests alike.
19
ECON20003 - Tutorial 3
command, where x is the variable of interest, mu0 is the hypothesized median value (0 by
default) and alternative is one of "two.sided" (default), "greater" or "less", just like in the t.test
and binom.test commands.

This command is part of the DescTools package, so install this package if you do not
have it yet, load it and then execute

SignTest(DT, mu = 6, alternative = "less")

You should get the following printout:

It shows that (i) this is a left-tail test, (ii) the hypothesized median is 6, (iii) the sample
median16 of DT is 5.5, (iv) the test statistic is S = 3, (v) the number of nonzero differences
is 8, (vi) the one-sided 94.5% confidence interval for the number of successes out of 8
trials is from negative infinity to 6, and (vii) the p-value is 0.3633, the same as before.

b) One-sample Wilcoxon signed ranks test

First, we rank the nonzero absolute deviations. The details are shown below:

DT Median under H0 Deviation Abs. deviation Rank


7 6 1 1 1.5
3 6 ‐3 3 6.5
4 6 ‐2 2 4.0
6 6 0 0
10 6 4 4 8.0
5 6 ‐1 1 1.5
6 6 0 0
4 6 ‐2 2 4.0
3 6 ‐3 3 6.5
8 6 2 2 4.0

16
The label on the printout, median of the differences, is somewhat misleading because 5.5 is the median of
DT. The median of DT – 6 is -0.5.
20
ECON20003 - Tutorial 3
Note that the zero deviations are disregarded, and the tied absolute deviations get the
same rank. For example, the two smallest absolute deviations are 1. Since they are
equal, we assign the same rank to them, namely the average of 1 and 2, i.e. 1.5.

Next, we calculate the sum of the ranks assigned to negative deviations, T- = 22.5, and
the sum of the ranks assigned to positive deviations, T+ = 13.5. T- is larger than T+, in
accordance with HA, but it might not be large enough to reject H0 at the 5% significance
level.

The test statistic is T = T+ = 13.5. Since this is a left-tail test, H0 is rejected if the test
statistic is equal to or smaller than the lower critical value, TL. From Table 9, Appendix
B, p. 1089 of Selvanathan et al., the 5% one-tail critical values are TL, = 6 and TU, =
30. T > TL,, so we maintain the null hypothesis at the 5% significance level and conclude
that the average delivery time for local deliveries might be six hours or more,
contradicting the courier’s advertisement.

This test can be performed with the

wilcox.exact(x, mu = mu0, alternative = " ")

command, where the arguments are the same as before. By default, an exact p-value is
computed if the samples contain less than 50 finite values and there are no ties. Otherwise,
a normal approximation is used.

This command is part of the exactRankTests package. Install and load this package.

You get the following warning:

Package ‘exactRankTests’ is no longer under development. Please consider using


package ‘coin’ instead.

Do not worry, this package still works fine.

Execute

wilcox.exact(DT, mu = 6, alternative = "less")

You should get the following output:

It shows that (i) this is a left-tail test, (ii) the hypothesized median is 6, (iii) the test statistic
is V = 13.5, like T+ above, (v) the p-value is 0.3008, larger than 0.05, confirming that at
the 5% significance level there is not enough evidence against the null hypothesis.

21
ECON20003 - Tutorial 3
Quit RStudio and save your RData and R files.

Exercise 4

In Exercise 2 we tried to find out whether the injection moulder of plastic golf tees was
operating satisfactorily in the sense that the mean weight of tees was not significantly
different from the specified 0.250 ounce. We performed a two-tail t-test for the population
mean on a random sample of 40 tees from the last hour’s production and concluded at the
1% significance level that the moulding process was not operating satisfactorily. Since prior
to the t-test we managed to verify with reasonable certainty that the weights of the tees from
the last hour’s production might be normally distributed, we have no reason to doubt this
conclusion. Still, for the sake of illustration,

a) Use the one sample sign test to determine whether the median weight of tees differs
from 0.250 ounce.

The question implies

H0 :  0.25 , HA :  0.25

Try to perform the sign test manually first. The deviations are not shown here, but you
should be able to verify that S- = 4 of them are negative and S+ = 34 of them are positive,
so the number of non-zero deviations, i.e. the effective sample size, is n* = 38.

The test statistic is S = S+ = 34. This is a two-tail test, and its p-value is 2min(pR ; pL),
where pR = P(S  S+) and pL = P(S  S+). We cannot find these probabilities ‘manually’
because the largest number of trials in the binomial probability table (Selvanathan,
Appendix B, Table 1) is 25. We can, however, rely on the normal approximation to the
binomial distribution:

 
S  N 0.5n* ,0.5 n*  N (19.00,3.08)

Since the expected value of this normal distribution is 19, pR = P(S  34) < P(S  34) =
pL, and hence the p-value is 2 pR. Also notice, that the largest possible value of S is 38,
so pR = P(S  34) = P(34  S  38).

From the standard normal probability table (Selvanathan, Appendix B, Table 3)

 (34  0.5)  19 (38  0.5)  19 


P ( S  34)  P  34  S  38   P  Z 
 3.08 3.08 
 P (4.71  Z  6.33)  P ( Z  6.33)  P ( Z  4.71)  0.0000

Hence, the p-value, twice of this probability, is practically zero, so we can safely reject
the null hypothesis and conclude that the process is not operating satisfactorily.

22
ECON20003 - Tutorial 3
Let’s now perform this test with R using the binom.test and SignTest commands, just
like in part (a) of Exercise 3.

Launch RStudio, create a new project and script, and name both t3e4. Import the data
saved in the t3e2 Excel data file to RStudio. Attach the data to your project and execute

binom.test(34, 38, conf.level = 0.99)

to obtain the following printout:

The p-value is practically zero (6.039e-07), so we can reject the null hypothesis at the
1% significance level.

Next, load the DescTools package and execute

SignTest(Weight, mu = 0.25, conf.level = 0.99)

You should get the following output:

Of course, the p-value and the decision are the same as before.17

b) Use the one sample Wilcoxon signed ranks test to determine whether the median
Weight of tees differ from 0.250 ounce.

Although it is a bit time consuming, do the calculations manually first. You should be
able to verify that the sum of the ranks assigned to the absolute values of the negative
deviations is T- = 48 and the sum of the ranks assigned to the absolute values of the
positive deviations is T+ = 693. The details are shown on the next page.

17
Notice that R reported the 99.4% confidence interval, although we specified a 99% confidence level in the
SignTest command. This discrepancy is due to the discrete nature of the binomial distribution.
23
ECON20003 - Tutorial 3
Weight Median under H0 Deviation Abs. deviation Rank
0.247 0.25 -0.003 0.003 20.50
0.251 0.25 0.001 0.001 4.50
0.254 0.25 0.004 0.004 29.00
0.253 0.25 0.003 0.003 20.50
0.253 0.25 0.003 0.003 20.50
0.248 0.25 -0.002 0.002 11.50
0.253 0.25 0.003 0.003 20.50
0.255 0.25 0.005 0.005 33.00
0.256 0.25 0.006 0.006 36.50
0.252 0.25 0.002 0.002 11.50
0.253 0.25 0.003 0.003 20.50
0.252 0.25 0.002 0.002 11.50
0.253 0.25 0.003 0.003 20.50
0.256 0.25 0.006 0.006 36.50
0.254 0.25 0.004 0.004 29.00
0.256 0.25 0.006 0.006 36.50
0.252 0.25 0.002 0.002 11.50
0.251 0.25 0.001 0.001 4.50
0.253 0.25 0.003 0.003 20.50
0.251 0.25 0.001 0.001 4.50
0.253 0.25 0.003 0.003 20.50
0.253 0.25 0.003 0.003 20.50
0.248 0.25 -0.002 0.002 11.50
0.251 0.25 0.001 0.001 4.50
0.253 0.25 0.003 0.003 20.50
0.256 0.25 0.006 0.006 36.50
0.254 0.25 0.004 0.004 29.00
0.250 0.25 0.000
0.254 0.25 0.004 0.004 29.00
0.255 0.25 0.005 0.005 33.00
0.249 0.25 -0.001 0.001 4.50
0.250 0.25 0.000
0.254 0.25 0.004 0.004 29.00
0.251 0.25 0.001 0.001 4.50
0.251 0.25 0.001 0.001 4.50
0.255 0.25 0.005 0.005 33.00
0.251 0.25 0.001 0.001 4.50
0.253 0.25 0.003 0.003 20.50
0.252 0.25 0.002 0.002 11.50
0.253 0.25 0.003 0.003 20.50
741.00
T- 48.00
T+ 693.00

T- and T+ add up to 741, which is correct since

n (1  n ) 38  39
  741
2 2

The test statistic is T = T+ = 693.

24
ECON20003 - Tutorial 3
Since n > 30, the sampling distribution of T can be approximated with a normal
distribution,

n ( n  1) n ( n  1)(2 n  1)
T  N  T ,  T  , T  ,  T2 
4 24

In this case,

n ( n  1) 38  39
T    370.5
4 4

n(n  1)(2n  1) 38  39  77
T    68.955
24 24

Hence, the standardized test statistic is

T  T T  370.5
Z 
T 68.955

The 1% two-tail standard normal critical values are 2.576, so H0 is rejected if zobs < -
2.576 or zobs > 2.576, i.e. if |zobs| > 2.576.

The observed test statistic is

T  370.5 693  370.5


z obs    4.677
68.955 68.955

It is well above 2.576, so we reject H0 and conclude that the population median of the
weight of tees is different from 0.25 ounce, and hence the moulding process is not
operating satisfactorily.

To verify this with R, load the exactRankTests package and execute

wilcox.exact(Weight, mu = 0.25)

You should get the following output:

As you can see, the p-value is practically zero, so H0 can be rejected at any reasonable
significance level.

Quit RStudio and save your RData and R files.

25
ECON20003 - Tutorial 3
Exercises for Assessment

Exercise 5 (Selvanathan, p. 401, ex. 10.51)

A parking officer is conducting an analysis of the amount of time left on parking meters. A
quick survey of 15 cars that have just left their metered parking spaces produced the
times (Time, in minutes) saved in the t3e5 Excel file. Assume that the population of
Time is normally distributed. Estimate with 95% confidence the mean amount of time
left for all the vacant meters. Do the calculations first manually and then with R.

Exercise 6 (Selvanathan, p. 508, ex. 12.41)

In this exercise do all calculations manually.

a) A random sample of eight observations was taken from a normal population. The sample
mean and standard deviation are 75 and 50, respectively. Can we infer at the 10%
significance level that the population mean is less than 100?

b) Repeat part (a) assuming that you know that the population standard deviation is 50.

c) Review parts (a) and (b). Explain why the test statistics differ.

Exercise 7

Environmental engineers have found that the percentages of active bacteria in sewage
specimens collected at a sewage treatment plant have a non-normal distribution with a
median of 40% when the plant is running properly. If the median is larger than 40%, some
adjustments must be made. The percentages of active bacteria (PAB) in a random sample
of 10 specimens are saved in the t3e7 Excel file. Do the data provide enough evidence (at
 = 0.05) to indicate that adjustments are needed?

a) What are the null and alternative hypotheses?

b) Which test(s) can be used to answer this question? What are the required conditions?
Do you think that these conditions are likely satisfied this time? Explain your answer.

c) Perform the test(s) first manually and then with R. Explain your decision and conclusion.

26
ECON20003 - Tutorial 3

You might also like