Statistical Hypothesis Testing with SAS and R - Dirk Taeger - E-Book

Statistical Hypothesis Testing with SAS and R E-Book

Dirk Taeger

0,0
73,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

A comprehensive guide to statistical hypothesis testing with examples in SAS and R

When analyzing datasets the following questions often arise:

Is there a short hand procedure for a statistical test available in SAS or R?

If so, how do I use it?
If not, how do I program the test myself?

This book answers these questions and provides an overview of the most common
statistical test problems in a comprehensive way, making it easy to find and perform
an appropriate statistical test.

A general summary of statistical test theory is presented, along with a basic
description for each test, including the necessary prerequisites, assumptions, the
formal test problem and the test statistic. Examples in both SAS and R are provided,
along with program code to perform the test, resulting output and remarks
explaining the necessary program parameters.

Key features:
• Provides examples in both SAS and R for each test presented.
• Looks at the most common statistical tests, displayed in a clear and easy to follow way.
• Supported by a supplementary website http://www.d-taeger.de featuring example
program code.

Academics, practitioners and SAS and R programmers will find this book a valuable
resource. Students using SAS and R will also find it an excellent choice for reference
and data analysis.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 280

Veröffentlichungsjahr: 2014

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Title Page

Copyright

Dedication

Preface

Part I: Introduction

Chapter 1: Statistical hypothesis testing

1.1 Theory of statistical hypothesis testing

1.2 Testing statistical hypothesis with SAS and R

1.3 Presentation of the statistical tests

References

Part II: Normal Distribution

Chapter 2: Tests on the Mean

2.1 One-sample tests

2.2 Two-sample tests

References

Chapter 3: Tests on the variance

3.1 One-sample tests

3.2 Two-sample tests

References

Part III: Binomial Distribution

Chapter 4: Tests on proportions

4.1 One-sample tests

4.3 -sample tests

References

Part IV: Other Distributions

Chapter 5: Poisson distribution

5.1 Tests on the Poisson parameter

References

Chapter 6: Exponential Distribution

6.1 Test on the parameter of an exponential distribution

Reference

Part V: Correlation

Chapter 7: Tests on association

7.1 One-sample tests

7.2 Two-sample tests

References

Part VI: Nonparametric Tests

Chapter 8: Tests on location

8.1 One-sample tests

8.2 Two-sample tests

8.3 -sample tests

References

Chapter 9: Tests on scale difference

9.1 Two-sample tests

References

Chapter 10: Other Tests

10.1 Two-sample tests

References

Part VII: Goodness-of-Fit Tests

Chapter 11: Tests on normality

11.1 Tests based on the EDF

11.2 Tests not based on the EDF

References

Chapter 12: Tests on other Distributions

12.1 Tests based on the EDF

12.2 Tests not based on the EDF

References

Part VIII: Tests on Randomness

Chapter 13: Tests on randomness

13.1 Run tests

13.2 Successive difference tests

References

Part IX: Tests on Contingency Tables

Chapter 14: Tests on contingency tables

14.1 Tests on independence and homogeneity

14.2 Tests on agreement and symmetry

14.3 Test on risk measures

References

Part X: Tests on Outliers

Chapter 15: Tests on outliers

15.1 Outliers tests for Gaussian null distribution

15.2 Outlier tests for other null distributions

References

Part XI: Tests in Regression Analysis

Chapter 16: Tests in Regression Analysis

16.1 Simple linear regression

16.2 Multiple linear regression

References

Chapter 17: Tests in variance analysis

17.1 Analysis of variance

17.2 Tests for homogeneity of variances

References

Appendix A: Datasets

Appendix B: Tables

Glossary

Index

This edition first published 2014

© 2014 John Wiley & Sons, Ltd

Registered office

John Wiley & Sons Ltd, The Atrium, Southern Gate, Chichester, West Sussex, PO19 8SQ, United Kingdom

For details of our global editorial offices, for customer services and for information about how to apply for permission to reuse the copyright material in this book please see our website at www.wiley.com.

The right of the author to be identified as the author of this work has been asserted in accordance with the Copyright, Designs and Patents Act 1988.

All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, except as permitted by the UK Copyright, Designs and Patents Act 1988, without the prior permission of the publisher.

Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books.

Designations used by companies to distinguish their products are often claimed as trademarks. All brand names and product names used in this book are trade names, service marks, trademarks or registered trademarks of their respective owners. The publisher is not associated with any product or vendor mentioned in this book.

Limit of Liability/Disclaimer of Warranty: While the publisher and author have used their best efforts in preparing this book, they make no representations or warranties with respect to the accuracy or completeness of the contents of this book and specifically disclaim any implied warranties of merchantability or fitness for a particular purpose. It is sold on the understanding that the publisher is not engaged in rendering professional services and neither the publisher nor the author shall be liable for damages arising herefrom. If professional advice or other expert assistance is required, the services of a competent professional should be sought.

Library of Congress Cataloging-in-Publication Data

Taeger, Dirk, author.

Statistical hypothesis testing with SAS and R / Dirk Taeger, Sonja Kuhnt.

pages cm

Includes bibliographical references and index.

ISBN 978-1-119-95021-9 (hardback)

1. Statistical hypothesis testing. 2. SAS (Computer program language) 3. R (Computer program language) I. Kuhnt, Sonja, author. II. Title.

QA277.T34 2014

519.50285′5133— dc23

2013041089

A catalogue record for this book is available from the British Library.

ISBN: 978-1-119-95021-9

Preface

Statistical hypothesis testing has been introduced almost one hundred years ago and has become a key tool in statistical inferences. The number of available tests has grown rapidly over the decades. With this book we present an overview of common statistical tests and how to apply them in SAS and R. For each test a general description is provided as well as necessary prerequisites, assumptions and the formal test problem. The test statistic is stated together with annotations on its distribution. Additionally two examples, one in SAS and one in R, are given. Each example contains the code to perform the test using a tiny dataset, along with output and remarks that explain necessary program parameters.

This book is addressed to you, whether you are an undergraduate student who must do course work, a postgraduate student who works on a thesis, an academic or simply a practitioner. We hope that the clear structure of our presentation of tests will enable you to perform statistical tests much faster and more directly, instead of searching through documentation or looking on the World Wide Web. Hence, the book may serve as a reference work for the beginner as well as someone with more advanced knowledge or even a specialist.

The book is organized as follows. In the first part we give a short introduction to the theory of statistical hypothesis testing and describe the programming philosophy of SAS and R. This part also contains an example of how to perform statistical tests in both programming languages and of the way tests are presented throughout the book. The second part deals with tests on normally distributed data and includes well-known tests on the mean and the variance for one and two sample problems. Part three explains tests on proportions as parameters of binomial distributions while the fourth part deals with tests on parameters of Poisson and exponential distributions. The fifth part shows how to conduct tests related to the Pearson's, Spearman's and partial correlation coefficients. With Part six we change to nonparametric tests, which include tests on location and scale differences. Goodness-of-fit tests are handled in Part seven and include tests on normality and tests on other distributions. Part eight deals with tests to assess randomness. Fisher's exact test and further tests on contingency tables are covered in Part nine, followed by tests on outliers in Part ten. The book finished with tests in regression analysis. We provide the used datasets in the appendices together with some tables on critical values of the most common test distributions and a glossary.

Due to the numerous statistical tests available we naturally can only present a selection of them. We hope that our choice meets your needs. However, if you miss some particular tests please send us an e-mail at: [email protected]. We will try to publish these missing tests on our book homepage. No book is free of errors and typos. We hope that the errors follow a Poisson distribution, that is, the error rate is low. In the event that you find an error please send us an e-mail. We will publish corrections on the accompanying website (http:\\www.d-taeger.de).

Last but not least we would like to thank Wiley for publishing our book and especially Richard Davies from Wiley for his support and patience. We hope you will not reject the null hypothesis that this book is useful for you.

Dirk TaegerSonja KuhntDortmund

Part I

Introduction

The theory of statistical hypothesis testing was basically founded one hundred years ago by the Britons Ronald Aylmer Fisher, Egon Sharpe Pearson, and the Pole Jerzy Neyman. Nowadays it seems that we have a unique test theory for testing statistical hypothesis, but the opposite is true. On one hand Fisher developed the theory of significance testing and on the other hand Neyman and Pearson the theory of hypothesis testing.

Whereas with the Fisher theory the formulation of a null hypothesis is enough, Neyman's and Pearson's theory demands alternative hypotheses as well. They open the door to calculating error probabilities of two kinds, namely of a false rejection (type I error) and of a false acceptance (type II error) of the null hypothesis. This leads to the well known Neyman–Pearson lemma which helps us to find the best critical region for a hypothesis test with a simple alternative. The largest difference of both schools, however, are the Fisherian measure of evidence (p-value) and the Neyman–Pearson error rate ().

With the Neyman–Pearson theory the error rate is fixed and must be defined before performing the test. Within the Fisherian context the p-value is calculated from the value of the test statistic as a quantile of the test statistic distribution and serves as a measure of disproving the null hypothesis. Over the decades both theories have merged together. Today it is common practice – and described by most textbooks – to perform a Neyman–Pearson test and, instead of comparing the value of the test statistic with the critical region, to decide from the p-value. As this book is on testing statistical hypothesis with SAS and R we follow the common approach of mixing both theories. In SAS and R the critical regions are not reported, only p-values are given. We want to make the reader aware of this situation. In the next two chapters we shortly summarize the concept of statistical hypothesis testing and introduce the performance of statistical tests with SAS and R.

Chapter 1

Statistical hypothesis testing

1.1 Theory of statistical hypothesis testing

Hypothesis testing is a key tool in statistical inference next to point estimation and confidence sets. All three concepts make an inference about a population based on a sample taken from it. Hypothesis testing aims at a decision on whether or not a hypothesis on the nature of the population is supported by the sample.

In the following we shortly run through the steps of a statistical test procedure and introduce the notation used throughout this book. For a detailed mathematical explanation please refer to the book by Lehmann (1997).

We denote a sample of size by , where the are observations of identically independently distributed random variables , . Usually some further assumptions are needed concerning the nature of the mechanism generating the sample. These can be rather general assumptions like a symmetric continuous distribution. Often a parametric distribution is assumed with only parameter values unknown, for example, the Gaussian distribution with both or either unknown mean and variance. In this case hypothesis tests deal with statements on the unknown population parameters. We exemplify our general discussion by this situation.

Each of the statistical tests presented in the following chapters is introduced by a verbal description of the type of conjecture to be decided upon together with the made assumptions. Next the test problem is formalized by the null hypothesis and the alternative hypothesis . If a statement on population parameters is of interest, often the parameter space , is partitioned into disjunct sets and with , corresponding to and , respectively.

As the next building stone of a statistical test the test statistic, which is a function of the random sample, is stated. This function fulfills two criteria. First of all its value must provide insight on whether or not the null hypothesis might be true. Next the distribution of the test statistic must be known, given that the null hypothesis is true. Table 1.1 shows the four possible outcomes of a statistical test. In two of the cases the result of the test is a correct decision. Namely, a true null hypothesis is not rejected and a false null hypothesis is rejected. If the null hypothesis is true but is rejected as a result of the test, a type I error occurs. In the opposite situation that is true in nature but the test does not reject the null hypothesis, a type II error occurs.

Table 1.1 Possible results in statistical testing.

Generally, unless sample size or hypothesis are changed, a decrease in the probability of a type I error causes an increase in the probability for a type II error and vice versa. With the significance level the maximal probability of the appearance of a type I error is fixed and the critical region of the test is chosen according to this condition. If the observed value of the test statistic lies in the critical region, the null hypothesis is rejected. Hence, the error probability is under control when a decision is made against but not when the decision is for , which needs to be kept in mind while drawing conclusions from test results. If possible, the researcher's conjecture corresponds to the alternative hypothesis due to primarily controlling the type I error. However, in goodness-of-fit tests one is forced to formulate the researcher's hypothesis, that is, the specific distribution of interest, as null hypothesis as it is otherwise usually unfeasible to derive the distribution of the test statistic.

The power function measures the quality of a test. It yields the probability of rejecting the hypothesis for a given true parameter value . The test with the greatest power among all tests with a given significance level is called the most powerful test.

Traditionally a pre-specified significance level of or is selected. However, there is no reason why a different value should not be chosen.

Up to here we are in the context of the Neyman–Pearson test theory. Most statistical computer programs are not returning whether the calculated test statistic lies within the critical region or not. Instead the p-value (probability-value) is given. This is the probability to obtain the observed value of the test statistic or a value that is more extreme in the direction of the alternative hypothesis calculated when is true. If the p-value is smaller than it follows that is rejected, otherwise is not rejected.

As already mentioned in the introduction this is the common approach. For further reading on the differences please refer to Goodman (1994), Hubbard and Bayarri (2003), Johnstone (1987), and Lehmann (1993).

1.2 Testing statistical hypothesis with SAS and R

Testing statistical hypotheses with SAS and R is very convenient. A lot of tests are already integrated in these software packages. In SAS tests are invoked via procedures while R uses functions. Although many test problems are handled in this way situations may occur where a SAS procedure or a R function is not available. Reasons are manifold. The SAS Institute decides which statistical test to include in SAS. Even if a newly developed test is accepted for inclusion in SAS it takes some time to develop a new procedure or to incorporate it in an existing SASprocedure. If a test is not implemented in a SAS procedure or in the R standard packages the likelihood is high to find the test as a SAS macro or in R user packages which are available through the World Wide Web. However, in this book we have refrained from presenting tests from SAS macros or R user packages for several reasons. We do not know how long macros, program code, or user packages are supported by the programmer and are therefore available for newer versions of SAS or R. In addition it is not possible to trace if the code is correct. If a statistical test is not implemented in the SAS software as procedure or in the R standard packages we will provide an algorithm with small SAS and R code to circumvent these problems. All presented statistical tests are accompanied by an example of their use in a given dataset. So it is easy to retrace the example and to translate the code to your own datasets. Sometimes more than one SAS procedure or R function is available to perform a statistical test. We only present one way to do so.

1.2.1 Programming philosophy of SAS and R

Testing statistical hypothesis in SAS or R is not the same, while R is a matrix language orientated software, SAS follows a different philosophy (except for SAS/IML). With a matrix orientated language some calculations are easier. For instance the average of a few observations, for example, the age and of four children in a family, can be calculated with one line of code in R by applying the function mean() to the vector containing the values, c(1,4,2,5).

mean(c(1,4,2,5))

Here the numeric vector of data values to be analyzed is inserted directly in the R function. However, it is also possible to call data from a previously defined object, for example, a dataframe

children<-data.frame(age=c(1,4,2,5)) mean(children$age)

In SAS a little more effort is necessary due to the required division into data and proc steps.

data children; input age; datalines; 1 4 2 5 ; run; proc means; var age; run;

The dataset children holds the variable age with observed values and . The SAS procedure proc means calculates the mean value. This type of programming philosophy must not be a disadvantage. It can save a lot of time, because the SAS procedures are very powerful and incorporate many statistical calculations in one go.

We assume that the reader is familiar with the basic programming features of SAS or R, such as data input and output, and only remark on some important points related to conducting statistical tests. Concerning data format usually one entry per observation and a column for each variable are suitable. However, in some cases it may be required to reorganize the dataset for test procedures. We accompany our examples with small datasets (see Appendix A), such that it is easy to see how data need to be arranged for the specific test.

In SAS most statistical tests are performed with procedures, which usually follow the schema:

proc

proc-name

data=

dataset-name options

;

var

variable-names options

;

options

;

run;

The data= statement identifies the dataset to be analyzed. If missing, the most recent dataset is taken. In some procedures it is necessary to fix some options to set up the statistical test, for example, to define the value to test against, or if the test is one or two sided. The var statement is followed by the variables on which the test shall be performed. Sometimes further options can be stated in separate command lines, for instance requesting an exact test. Note, some procedures differ from this general set-up. The procedure proc freq as an example has no var but a table statement. Occasionally the statement classclass-variable is needed indicating a grouping variable which assigns each observation to a specific group. As options of procedures can be numerous and not all of them may be needed for the treated test, we restrict our exposure to the indispensable options. The same applies to the output we present for the examples.

Conducting a statistical test in the program R usually only requires one line of code. The common layout of R functions is:

function-name

(

x, options

)

The function-name identifies the function to be applied to the data x. In two-sample tests data on a second variable are needed, such that the general layout is extended to:

function-name

(

x,y, options

)

Options differ for each test, but the option alternative=alternative-hypothesis occurs often. As alternative-hypothesis of “two.sided”, “less”, or “greater” is chosen, depending on how the alternative hypothesis is to be specified. It suffices to state only the first letter, that is, “t”, “l”, or “g”. As in SAS we only present the options that are necessary to perform the test and restrict the presented output to the relevant parts.

1.2.2 Testing in SAS and R—An example

To demonstrate the testing of hypothesis in SAS and R let us look at the ordinary t-test which tests if a population mean differs from a given values . We employ the dataset in Table A.1 from Appendix A containing observations on three variables for people: subject number (no), status of the subject (status), and systolic blood pressure in millimeters of mercury (mmhg). Now, we want to test if the mean systolic blood pressure of the population differs statistically significantly from mmHg at the 5% level . The null hypothesis is given by and the alternative hypothesis is . We assume that the systolic blood pressure is normally distributed.1

SAS provides the procedure ttest to handle this test problem. The SAS code is:

proc ttest data=blood_pressure ho=140; var mmhg; run;

The dataset option data= specifies the dataset and the option ho= the null value to test. With var mmhg you tell SAS that the variable mmhg is the variable which contains the observations to be used. In the output containing, for example, the mean, standard error and 95% confidence interval, the following refers to the statistical test:

DF t Value Pr> |t| 54 -3.87 0.0003

DF characterizes the degrees of freedom of the t-distribution, as the test statistic is t-distributed. The value of the test statistic (t Value) is and the corresponding p-value (Pr > |t|) is 0.0003. So we can conclude that the mean value differs statistically significantly from 140 mmHg at a significance level of 5%.

As in SAS it is also simple in R to conduct a t-test:

t.test(blood_pressure$mmhg,mu=140)

The first argument calls the data on the variable mmhg from the dataset blood_pressure. The second argument mu= specifies the value of the null hypothesis. The most relevant part of the output for the testing problem is:

t = -3.8693, df = 54, p-value = 0.0002961

The values are the same as for the SAS procedure of course, except for the fact that they are rounded to more digits. A nice feature of R is that it returns the alternative hypothesis with the output:

alternative hypothesis: true mean is not equal to 140

If a ready to use SAS procedure or R function is not available, we have to calculate the test statistic and compare it to the corresponding test statistic distribution by hand. The formula for the test statistic of the t-test is given by:

and follows a t-distribution with degrees of freedom, where is the sample size. The null hypothesis is rejected if for the observed value of either or holds. These quantiles are describing the critical regions of this test (see Figure 1.1 for ). The p-value is calculated as , where denotes the probability function of the t-distribution with degrees of freedom.

Figure 1.1 Critical regions (shaded areas) of a t-distribution with 54 degrees of freedom with significance level =0.05.

Let us start with SAS to program this test by hand.

* Calculate sample mean and standard deviation; proc means data=blood_pressure mean std; var mmhg; output out=ttest01 mean=meanvalue std=sigma; run; * Calculate test statistic; data ttest02; set ttest01; mu0=140; * Set mean value under the null hypothesis; t=sqrt(55)*(meanvalue-mu0)/sigma; run; * Output results; proc print; var t; run;

The output gives a t-value of . The critical values and can be calculated with the SAS function TINV, which returns the quantiles of a t-distribution.

data temp; x=tinv(0.025,54); run;

Here tinv(0.025,54) gives and tinv(0.975,54) returns . Because the t-value calculated for the dataset is less than we reject the null hypothesis at the 5% level.

The p-value is also not complicated to calculate. The probability distribution function of the t-distribution in SAS is PROBT and 2*probt(-3.86927,54) gives a p-value of 0.0002961135.

To write a code for the same t-test in R is quite easy as well.

# Calculate sample mean and standard deviation xbar<-mean(blood_pressure$mmhg) sigma<-sd(blood_pressure$mmhg) # Set mean value under the null hypothesis mu0<-140 # Calculate test statistic t<-sqrt(55)*(xbar-mu0)/sigma # Output results t

This R code returns the test statistic value of . To calculate the boundaries of the critical regions the R function qt can be used, where qt(0.025,54) returns and qt(0.975,54) returns 2.004879. The p-value is calculated as 2*pt(-3.869272,54) with the function pt of the probability function of the t-distribution and has a value of 0.0002961135.

The three typical hypotheses for a t-test are:

A. vs
B. vs
C. vs

with the sample mean and mmHg in our example. So far case (A) has been treated. Let us now look at the t-tests for hypotheses (B) and (C) at the 5% significance level.2 The significance level is no longer split between the lower and upper critical regions. For hypothesis (B) the decision rule is: reject if for the observed value of it holds that and for hypothesis (C) reject if for the observed value of it holds that . In our example with significance level 0.05 the boundaries for the critical regions are for hypothesis (B) and for hypothesis (C). See Figure 1.2 and Figure 1.3 for a graphical representation.

Figure 1.2 Lower critical region (shaded area) and critical value of a one-sided test with significance level of 5% (t-distribution with 54 degrees of freedom).

Figure 1.3 Upper critical region (shaded area) and critical value of a one-sided test with significance level of 5% (t-distribution with 54 degrees of freedom).

In SAS these values are computed as tinv(0.95,54) for (B) and tinv(0.05,54) for (C). In R these values are computed as qt(0.95,54) for (B) and qt(0.05,54) for (C). Please note, both boundaries of the critical regions are the same except for the algebraic sign as the t-distribution is a symmetric distribution. SAS and R do not report the critical values, only p-values–as any statistical software we know. Some tables of critical values for several distributions can be found in Appendix B.

The option sides=U of the procedure proc ttest forces SAS to test the one-sided hypothesis were the alternative hypothesis is that the true mean is greater than . The output is:

DF t Value Pr> t 54 -3.87 0.9999

The R code for this hypothesis is:

t.test(blood_pressure$mmhg,mu=140,alternative="greater")

and gives a similar output:

t = -3.8693, df = 54, p-value = 0.9999 alternative hypothesis: true mean is greater than 140

To test the hypothesis (C) vs is not complicated at all. In SAS the following code

proc ttest data=blood_pressure ho=140 sides=L; var mmhg; run;

yields the output.

DF t Value Pr < t 54 -3.87 0.0001

Here the option sides=L forces SAS to test the one-sided hypothesis where the alternative hypothesis is that the true mean is lower than . In R the tests is done with

t.test(blood_pressure$mmhg,mu=140,alternative="less")

and returns

t = -3.8693, df = 54, p-value = 0.0001481 alternative hypothesis: true mean is less than 140

1.2.3 Calculating p-values

Generally the calculation of a p-value is straightforward. In the case of the t-test the p-value is either the area under the probability curve lower or greater than the calculated t-value, that is, the p-value is for hypothesis (C) and for hypothesis (B), where . The SAS function probt and the R function pt calculate the probability that for the -distribution.

The p-value of hypothesis (A) is twice the minimum of the lowest p-value of the one-sided tests, which is equal to . However, keep in mind that this is only valid for symmetric distributions like the t- or Gaussian distribution. If the test statistic is a non-symmetric distribution like the F-distribution it is necessary to work out if the observed value is at the lower or upper tail of the distribution. For a two-sided test use . This ensures the lowest two-sided p-value is obtained and not, on occasion, a p-value above 1 [see Gallagher (2006) for further discussion].

It is usual to format p-values such that values between 0 and 0.0001 are printed as <0.0001 and values above are restricted to four digits. In SAS procedures this is the case. If calculating a p-value yourself you can us the SAS format pvalue.

data ttest; format p_value pvalue.; p_value=2*probt(-3.86927,54); run;

This will result in a p-value of 0.0003.

Usually the p-value in R is not formatted in this way. The function format.pval can be used to format it. But first with the R option scipen=3 the scientific notation should be suppressed.

options(scipen=3) format.pval(2*pt(-3.86927,54),1,eps=0.0001)

This R code will also give a p-value of 0.0003.

If necessary it is possible to derive quantiles, and thereby p-values and critical values, by simulation. Let us stick to the assumed symmetric t-distribution. In SAS the code could look as follows:

data random; do i=1 to 100000; r=rand(‘T’,54); output; end; run; proc univariate data=random noprint; var r; output out=critical pctlpts=2.5 97.5 pctlpre=crit_; run; proc print data=critical; run;

The rand(‘T’,54) function calculates a random number from a t-distribution with 54 degrees of freedom. This is repeated 100 000 times and the random numbers are stored. The proc univariate procedure calculates the desired quantiles using the option pctlpts=. The additional option pctlpre= is used to give a prefix to the variable names of the calculated quantiles. The output is then for the 0.025-quantile and 0.975-quantile calculated here

crit_2_5 crit_97_5 -2.01062 2.01350

In R we need only one line of code

quantile(rt(100000,54),c(0.025,0.975))

The quantile(.) function calculates quantiles and the rt(.) function calculates 100 000 random numbers of the desired t-distribution. The option c(0.025,0.975) then calculates the 0.025-quantile and 0.975-quantile of these random numbers. We get the output

2.5% 97.5% -2.020038 2.014382

These numbers are different to those SAS returned and they will always vary if you try it by yourself, because random numbers should differ from run to run. However, we see that these values are very close to the critical values that are given by the quantiles of the t-distribution.

For some tests, for example, the Jarque–Bera test (see Test 11.2.2), these kinds of Monte Carlo simulation are needed to get the critical values. For some tests implemented in SAS and R this Monte Carlo approach can be additionally requested.

Remember that in most cases the p-value is stated in the output of the applied procedure or function. If the statistical test you would like to apply is not implemented in SAS or R you have to write it yourself (or use the code we provide in such situations). Table 1.2 and Table 1.3 list some common distribution functions in SAS and R, respectively, which are of interest in calculating p-values from parametric distributions. For nonparametric tests or tests with distributions other than stated above or implemented in SAS or R, p-value calculation is sometimes cumbersome. If necessary we provide code for such problems.

Table 1.2 Some distribution functions in SAS.

Table 1.3 Some distribution functions in R.

1.3 Presentation of the statistical tests

In this section we use the single sample t-test again to describe the presentation of statistical tests in this book. The layout follows a structured table.

First the name of the test is given2.1.2t-Test
A brief description of the test followsDescription:Tests if a population mean differs from a specific value .
Assumptions of the test are listedAssumptions:• Data are randomly sampled from a Gaussian distribution.
Different hypotheses are listed. In this case the non-directional hypothesis and the two directional hypotheses can be tested with this testHypotheses:(A) vs (B) vs (C) vs
The test statistic is displayedTest statistic:
Decision rules for each hypothesis are listedTest decision: Reject if for the observed value of (A) or (B) (C)
Formulae of p-values for each hypothesis are givenp-value:(A) (B) (C)
Annotations of the test, such as the distribution of the test statistic, are pointed outAnnotations:Test statistic is t-distributed with degrees of freedom.
An explaining example on a dataset is introducedExample: To test the hypothesis that the mean systolic blood pressure of a certain population equals 140 mmHg. The dataset at hand has measurements on 55 patients (dataset in Table A.1).
The SAS code of the example is givenSAS codeproc ttest data=blood_pressure ho=140 sides=2; var mmhg; run;
The SAS output of the example code is givenSAS output DF t Value Pr < t 54 -3.87 0.0003
Remarks concerning the SAS code and/or output are givenRemarks:ho=value is optional and defines the value to test against. Default is 0.
The R code of the example is givenR codet.test(blood_pressure$mmhg,mu=140,alternative="two.sided")
The R output of the example code is givenR outputt = -3.8693, df = 54, p-value = 0.0002961
Remarks concerning the R code and/or output are givenRemarks:mu=value is optional and defines the value to test against. Default is 0.

References

Gallagher J. 2006 The F test for comparing two normal variances: correct and incorrect calculation of the two-sided p-value? Teaching Statistics28, 58–60.

Goodman S.N. 1994 P values, hypothesis tests, and likelihood: implications for epidemiology of a neglected historical debate. American Journal of Epidemiology139, 116–118.

Hubbard R.H. and Bayarri M.J. 2003 Confusion over measures of evidence (p's) versus errors ('s) in classical statistical testing (with discussions). The American Statistician57, 171–182.

Johnstone D.J. 1987 Tests on significance following R. A. Fisher. The British Society for the Philosophy of Science38, 481–499.

Lehmann E.L. 1993 The Fisher, Neyman–Pearson theory of testing hypothesis: one theory or two? Journal of the American Statistical Association88, 1242–1249.

Lehmann E.L. 1997 Testing Statistical Hypotheses, 2nd edn. Springer.

1 As the systolic blood pressure only takes positive values, the assumption of a normal distribution is strictly speaking not appropriate. However, blood pressure measurements usually lie in a region far away from zero, so that in this case the t-test can be expected to be reasonably robust against this violation.

2 For scientific correctness the significance level always needs to be decided upon before conducting the test.