Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

MATH253 Week 6 Tutorial

R tutorial

This tutorial sheet is related to material covered in chapters 7, 8 and 9 (how to do tests/calculations/plots from these chapters in R).

Solutions will be available on Canvas on Friday 5pm.

Two-Sample Tests

Part A: Paired t-test

In an investigation into the effects of a chemical on the leaves of a plant, eight leaves were chosen and each was divided into two parts.  One part of each leaf was treated with the chemical and the other was not. Microscopic examination of the leaves for structural damage gave the following comparative responses (a higher number corresponds to more damage).   The purpose of the investigation is to determine whether damage to the leaf is increased when treated with the chemical.  The data can be found on Canvas file Tutorial6 leaf damage .xlsx.

❼ Download the le Tutorial6 leaf damage .xlsx to your computer into a folder dedicated to R.

Make sure that this folder is set up as your working directory in RStudio.

❼ In RStudio open a new R script.

❼ Load the le Tutorial6 leaf damage .xlsx using readxl package, creating the variable called

leaf.  (See Tutorial 2 for details how to load data using readxl package.)

❼ Make sure you save your R script in the folder dedicated to R and it is a good idea to keep saving it

after each task you complete.

Note that these data are paired: we have two observations on each individual leaf.

1.  Test the hypothesis H0  : u1  = u2  versus the alternative H1  : u1  > u2  where u1  is the mean damage of treated leaves and u2  is the mean damage of untreated leaves.

❼ Perform a paired t-test in R, using the command t .test:

t .test(leaf$Treated,  y  =  leaf$Untreated,  alternative  =  c("g"),  paired  =  TRUE, var .equal  =  FALSE,  conf .level  =  0 . 95)

❼ Since we perform a paired t-test, we put paired=TRUE and var .equal  =  FALSE.

❼ Note that R takes the differences in the form y - .’, so take care how you define alternative. Write up your conclusions. Explain why the alternative H1  : u1  > u2  is appropriate here.

2.  Recall the underlying assumption for the paired two-sample t-test is that the differences are normally distributed. We now check this assumption.

❼ First compute the sample differences in R, creating the new variable called difference.

❼ Plot a histogram of the sample differences, name the histogram and horizontal axis appropriately.

Comment on your plot.

❼ Test for normality more formally as follows. We use Anderson-Darling test for normality.

 You will have to install the package nortest rst.  (See Tutorial 2 for details how to install a package.)

  Call the package and now you can use the command ad .test for the differences: ad .test(difference)

  Construct the normal probability plot for the differences by running commands:

qqnorm(difference,  col="blue")

qqline(difference,  col="red")

Recall that if the data are  (approximately) normally distributed, the blue points on the plot will lie (approximately) on a straight line. A small p-value for the Anderson-Darling test for normality is evidence against normality.

Comment on your result.  Does the assumption underlying the paired t-test appear to be justified for these data?

Part B: Independent two-sample t-test

An investigation was conducted into the dust content in the exhaust gases of two types of solid fuel boilers. Thirteen boilers of Type A and nine boilers of Type B were used under identical conditions for a specified period, and the quantities (in grams) of dust were deposited.

The data can be found on Canvas – file Tutorial6 boilers .xlsx. Download the le to your computer and load into R.

Notice that we have two independent samples.  The aim is to test whether there is any difference between the two types of boilers, in terms of the quantities of dust deposited.

1.  Test the hypothesis H0  : uA  = uB  versus the alternative:  H1  : uA    uB  where uA  is the population mean of the deposited dust in Type A boiler and uB  is the population mean of the deposited dust in Type B boiler.

❼ Perform an independent two–sample t-test in R, using the command t .test.

❼ Since we perform an independent test, we have to set up paired=FALSE for the parameters of

the command.

❼ For this test we assume equal variances, so make sure you have var .equal  =  TRUE for the

parameters of the command.

❼ Construct boxplots for this data (for informal assessment of data), including sample means. (Hint:

use the command boxplot and to display the sample means in boxplots use points.)

Write up your conclusions, including commenting on the boxplots.

Recall the underlying assumptions for the independent two-sample t-test are that the two samples are from independent, normally distributed populations with equal variances.

2.  Check the assumptions of normality by carrying out a normality test and constructing a normal prob- ability plot for each of the two groups separately. Write up your conclusions.

3.  Check the assumption of equal variances.

❼ Perform a F-test to compare two variances in R, using the command var .test:

var .test(x,  y,  ratio  =  1,  alternative  =   . . . ,  conf .level  =  0 . 95)

❼ Similarly as for other tests in R, put in the required parameters for  x,  y,  alternative,

conf .level.

❼ Note that R takes the ratio in the form y/.’, so take care how you define alternative (when

you perform a one-sided test).

Remember that we are assuming the data are normally distributed, so that the relevant test of equality of variances is the F-test.

Write up your conclusions.  Does the assumption of equal variances appear to be justified for these data? Comment on the boxplots of part 1 in the light of this.

4.  Find the 90% confidence interval for the population variance 72  for the dust content of Type A boiler.

❼ A test and confidence interval for the variance of a single population is not a part of basic R,

however it is a part of TeachingDemos package. Install TeachingDemos package. (See Tutorial 2 for details how to install a package.)

❼ Call the package and now you can use the command sigma .test (using parameters in a similar

way as for other tests in R):

sigma .test(x,  sigma=1,  alternative  =  c("t"),  conf .level  =   . . . )

❼ Similarly as for other tests in R, put in the required parameters for x,  alternative,  conf .level.

❼ There is parameter  sigma if we want to perform a hypothesis test to determine whether the

population variance is equal to some hypothesised value.  In our case we are asked to nd only a confidence interval so it doesn’t matter what sigma is.  You don’t have to include this at all when we want to nd only a confidence interval.  Note that if  sigma is not included, R takes sigma=1 as default.

Finally, save your work.

Tip:  To practice using R more, perform the relevant tests in R for examples in notes, quizzes on M¨obius, tutorial sheets in odd weeks (where the sets of data are given).