Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

ECON20003 – QUANTITATIVE METHODS 2

TUTORIAL 3

Download the t3e2, t3e3, t3e4, t3e6 and t3e8 Excel data files from the subject website and
save them to your computer or USB flash drive. Read this handout and try to complete the
tutorial exercises before your tutorial class, so that you can ask help from your tutor during
the Zoom session if necessary.

After you have completed the tutorial exercises attempt the “Exercises for assessment”. You
must submit your answers to these exercises in the Tutorial 3 Homework Canvas
Assignment Quiz by the next tutorial in order to get the tutorial mark. For each assessment
exercise type your answer in the relevant box available in the Quiz or type your answer
separately in Microsoft Word and upload it in PDF format as an attachment. In either case,
if the exercise requires you to use R, save the relevant R/RStudio script and printout in a
Word document and upload it together with your written answer in PDF format.

Numerical Descriptive Measures

R has a list of functions for computing descriptive statistics for a single variable, such as

mean arithmetic mean


sd standard deviation
var variance1
min minimum (i.e. smallest value)
max maximum (i.e. largest value)
range range of values (i.e. minimum and maximum)
median median
mad median absolute deviation, i.e. the median of the absolute values of the
deviations from the median;
quantile sample q-quantiles;2,3
IQR interquartile range, i.e. Q3 – Q1.

Each command must be followed by the name of the variable to be analysed in brackets.

We are going to illustrate the application of these R commands with one of the datasets
used last week in Tutorial 2. Namely, in Exercise 3 you imported the Height (cm) and Weight
(kg) of 20 randomly selected people from an Excel spreadsheet to RStudio and illustrated

1
By default, sd() and var() assume that the dataset is a sample, so the sum of squared deviations from the
mean is divided by n-1 (number of observations minus one) rather than by n. If the dataset is a population,
then you can obtain the population variance by calculating (n-1)/n * var() and the population standard deviation
as the square root of population variance.
2
q-quantiles, in general, are q - 1 cut points that partition a set of observation ordered in ascending order into
q number of subsets of (nearly) equal sizes.
3
By default, q = 4 and quantile returns the minimum, the three quartiles and the maximum (i.e. 0%, 25%, 50%,
75%, 100%), but in general q can be any positive integer larger than one.
1
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
this dataset by various graphs.4

Exercise 1

a) Launch RStudio, create a new RStudio project and script, and name both t3e1. Retrieve
the t2e3 data set and save it as t3e1.RData.

Follow similar steps than in Exercise 2 of Tutorial 2. Note that although you save the
retrieved dataset in the t3e1.RData file as part of the new t3e1 project, the name of the
data frame is still t2e3.

b) Calculate the descriptive statistics listed on the previous page on Height and Weight.

In order to be able to access the Height and Weight variables by their names, first
execute the

attach(t2e3)

command.

Then, to obtain the sample means, execute

mean(Height)
mean(Weight)

You should see the following in the Console:

We could obtain all other descriptive statistics for Height and Weight in a similar way,
i.e. by executing each command for each variable one by one. However, this would be
very time consuming and thus impractical, especially when there are several variables
in the data frame. It is easier and quicker to compute an overall summary of each
variable or that of the entire data frame using the sapply and summary functions.

4
If you have not completed Exercise 3 of Tutorial 2, then do it before attempting Exercise 1 of Tutorial 3.
2
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
sapply: provides the value of a particular statistic for every variable in the data frame.

summary: returns the values of min, Q1, median, mean, Q3 and max for numeric-type
variables and the frequency of each category for character-type variables.

For example,

sapply(t2e3, quantile)

displays the smallest and the largest values and the 1st, 2nd and 3rd quartiles in the
Console

while

summary(Height)

returns the smallest and the largest values, the 1st, 2nd and 3rd quartiles, and the mean

and

summary(t2e3)

returns

The sapply and summary functions are in the core set of packages that come with the
installation of R. In addition to them, the basic descriptive statistics and some others are
also provided by the stat.dec function which is part of the pastecs package.

stat.dec: provides the basic descriptive statistics (min, max, range, sum, median, mean, var,
sd), the number of values (nbr.val), the number of zero values (nbr.null), the number of
missing values (nbr.na), the estimated standard error on the sample mean (SE.mean), half
3
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
of the width of the 95% confidence interval of the mean (CI.mean), and the coefficient of
variation (coef.var).

The pastecs package does not come automatically with the installation of R, so it might not
be available on your computer. The currently installed packages are shown in the User
Library on the Packages tab of RStudio.

If you cannot find it on the Packages tab of RStudio, follow the Tools / Install Packages…
menu steps, enter the name of the package in the opening Install Packages dialogue
window and click Install. Once pastecs turns up on your Packages tab, check the box
next to it to load it in your current RStudio project.5

Then, execute

stat.desc(t2e3)

It displays the descriptive statistics for Height and Weight but in scientific notation.6 To
have the results in standard form, we need to use the round function (see page 2 of
Tutorial 2) as well. If, for example, we want the results with a precision of two decimals,

5
Alternatively, enter and run the library("pastecs") command in the Source panel.
6
Scientific notation is a way of writing numbers in the form a × 10b, where -10 < a < 10 and b is an integer.
4
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
the appropriate command is

round(stat.desc(t2e3),2)

It returns the following table:

c) Although it is very convenient to let R do the hard work, it is useful to reproduce the
same results ‘manually’ to make sure that you understand the underlying formulas and
the numbers reported by R. To save time, here in the tutorial we focus on Weight. What
do the descriptive measures in the previous table tell you about this sample of Weight?

nbr.val Number of observations, i.e. the sample size, n = 20.

nbr.null Number of zero observations. Browse the data in the RStudio data
viewer7 to verify that none of the Weight values is zero.

nbr.na Number of missing (i.e. not available, abbreviated as NA) Weight values.
There are n = 20 observations for Weight, none is missing.

min Smallest observation. Rank the data in the ascending order8 of Weight to
verify that the smallest observation is 64. Hence, the lightest person in
the sample weighs 64kg.9

max Largest observation. The same ordered array shows that the largest
observation is 93, so the heaviest person in the sample weighs 93kg.

range It is the difference between the largest and the smallest observations, i.e.
93 – 64 = 29 and it shows that people’s weight in this sample varies within
an interval of 29kg wide.10

7
Use the View(t2e3) command.
8
If you do not remember how to do this, see page 12 of Tutorial 2.
9
When you interpret a descriptive measure, do not forget to mention the name or meaning of the variable and
its unit of measurement.
10
Notice that the range() function of R provides the smallest and the largest values, not their difference (see
this function on page 1). So, if you run range(Weight), the result is 64 93, not 29.
5
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
sum Sum of all observations. In this sample the total weight of all 20 people is
1537kg.

median Middle value, or the average of the two middle values, of an ordered
array. Since there are even number of observations, the ordered array
has two middle values, the 10th and the 11th values. Go back to the
RStudio data viewer to verify that both are 75, and hence their average
is also 75. It shows that in this sample, half of the people weigh at most
(i.e. less than or equal to) 75kg and half of the people weigh at least (i.e.
more or equal to) 75kg.

mean Arithmetic mean, i.e. the sample mean

1 n 1537
x 
n i 1
xi 
20
 76.85

where the numerator is sum and the denominator is nbr.val. It shows that
the average weight of people in this sample is 76.85kg.

Skip the next two statistics (SE.mean and CI.mean.0.95) for the time being, we shall
consider them in the next exercise.

var Sample variance. It is equal to 55.40kg2. It is an important statistical term,


but since its unit of measurement is ‘squared kg’, it is not possible to
assign a reasonable interpretation to it.

std.dev Sample standard deviation. It is the square root of the sample variance,

s  55.40 kg 2  7.44 kg

so the ‘average’ deviation of weight from the sample mean is about


7.44kg.11

coef.var Sample coefficient of variation. It is the sample standard deviation divided


by the sample mean,

s 7.44 kg
cv    0.0968  0.10
x 76.85 kg

and it shows that in this sample the standard deviation of people’s weight
is about 10% of the average weight.

11
Although to save time we do not calculate var and std.dev manually in this tutorial, try to obtain the sample
standard deviation with the STAT mode of your Casio FX82 calculator.
6
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
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.12

Exercise 2 (Selvanathan, p. 398, ex. 10.54)

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 t3e2 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 t3e2. Import the data saved
in the t3e2 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

To make sure that we do not use this formula erroneously, we need to focus on normality
first.13 In the lectures on week 2 we discussed that normality can be checked (i) visually by
graphs, like 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
than 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).

12
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.
13
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.
7
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
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.

Histogram of WH
0.15
0.10
Density

0.05
0.00

0 5 10 15 20

WH

8
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
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:

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 on the next page, which 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.

9
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
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.

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.

10
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
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:

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. To obtain descriptive
statistics for WH, load the pastecs package in your current RStudio project to be able to
use the following command:

11
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
stat.desc(x, basic = TRUE , desc = TRUE, norm = FALSE, p = 0.95)

where 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).

Execute

round(stat.desc(WH),3)

to obtain the following printout14:

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.
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

14
To keep the printout neat, it is recommended to combine stat.desc() with the round() command.
12
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3

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 with15

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

ns 4

The estimated approximate standard error of this statistic is16

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 (norm = TRUE: 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 = FALSE, desc = FALSE, norm = TRUE), 3)

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

13
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
They return:

  skewness skew.2SE kurtosis kurt.2SE normtest.W normtest.p


0.778 2.262 2.278 3.329 0.925 0.000

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 thinner 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

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.17 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

6n( n  1) n2  1
17
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.
14
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
shapiro.test(WH)

command, which returns

Shapiro-Wilk normality test

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

The p-value18 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.

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, so at
the given large sample size (n = 200), we can still 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 first stat.desc printout on page 12: 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”)

18
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.
15
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
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 first stat.desc printout on page 12. 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.19

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.

Exercise 3

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)20 are saved in the t3e3 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 t3e3. Import the data saved
in the t3e3 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

19
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.
20
1 ounce (oz) is equal to 28.34952 grams (g).
16
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
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 2.

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

attach(t3e3)
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")

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")

17
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
Normal Q-Q Plot

0.256
0.250 0.252 0.254
Sample Quantiles

0.248

-2 -1 0 1 2

Theoretical Quantiles

Finally, obtain descriptive statistics and perform the SW test by executing the following
commands:

library(pastecs)
round(stat.desc(Weight, basic = FALSE, 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)

18
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
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.

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".21 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 following printout:

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

21
As you will see next week, this command can be used to perform two sample t-tests as well.
19
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
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.

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 2 for a minute.

Exercise 2 (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 t3e2.R file.22 On the Files tab click t3e2.RData to reload the
data in your project. Run the first command, attach(t3e2), 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).

22
If you failed to save this script earlier, then you need to import again the data saved in the t3e2 Excel file to
RStudio.
20
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
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. Even if the sample mean exists, the sample median is
a more appropriate measure of central location when the sample at hand contains one or
more observations that are very small or large relative to the rest of the population.
Moreover, the Z / t tests can be very 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, that
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.23

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.

23
The sign test is actually a binomial test with p = 0.5.
21
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
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),24


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

The one-sample Wilcoxon signed ranks test serves the same purpose than 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.

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

24
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 Appendix 8.A of Selvanathan (pp. 335-338).
22
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
Exercise 4 (Selvanathan, p. 500, 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 t3e4 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.

Launch RStudio, create a new project and script, and name both t3e4. Import the data saved
in the t3e4 Excel data file to RStudio. Save the data as t3e4.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.25

Accordingly, we rewrite the hypotheses as

H0 :  6 vs. HA :  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.

25
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.
23
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
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. 1068-1071, n = 8, p = 0.5) is

pL  (S  3)  0.3633

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:

24
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
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 interval26 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.27

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 = " ")

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
median28 of DT is 5.5, (iv) the test statistic is S = 3, (v) the number of nonzero differences
26
Since the test is left-sided, this confidence interval for the population proportion is also one-sided.
27
Depending on the alternative argument, binom.test always reports the proper p-value for right-tail, left-tail
and two-tail tests alike.
28
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.
25
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
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

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.

26
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
This command is part of the exactRankTests package.29 Install this package, load it and
then 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.

Quit RStudio and save your RData and R files.

Exercise 5

In Exercise 3 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 is n* = 38.

The test statistic is S = S+ = 34. This is a two-tail test and its p-value is 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,

29
You will get a warning: Package ‘exactRankTests’ is no longer under development. Please consider using
package ‘coin’ instead. Yet, we use ‘exactRankTests’ this semester because this package was used in the R
video recorded in the last semester for Tutorial 3.

27
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
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.

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

Launch RStudio, create a new project and script, and name both t3e5. Import the data
saved in the t3e3 Excel data file to RStudio. 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:

28
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
Of course, the p-value and the decision are the same as before.30

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 there are four negative deviations and 34 positive deviations, so the
effective sample size is n = 38. 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.

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. 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

30
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.
29
L. Kónya, 2020, Semester 2 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

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

30
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
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.

Exercises for Assessment

Exercise 6 (Selvanathan, p. 397, ex. 10.43)

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
(T, in minutes) saved in the t3e6 Excel file. Assume that the population of T 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 7 (Selvanathan, p. 499, 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.

31
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3
Exercise 8

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 t3e8 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.

32
L. Kónya, 2020, Semester 2 ECON20003 - Tutorial 3

You might also like