<<

3. Hypothesis Testing and Analysis of with R Parametric and non parametric tests PROBABILITY and WITH R Copyright c 2016

Tom´as Goicoa Department of Statistics and Operations Research Public University of Navarre [email protected] Ana F. Militino Department of Statistics and Operations Research Public University of Navarre [email protected] − Outline Introduction Hypothesis Tests for µ Hypothesis Tests for µX µY Hypothesis Tests for π

Hypothesis Testing

Introduction Hypothesis Test for µ and µ µ (Parametric and non-parametric) 1 − 2 Hypothesis Test for π Analysis of variance (Parametric and non-parametric)

M´aster Universitario en Salud P´ublica 2/66 UPNA − Outline Introduction Hypothesis Tests for µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Introduction

A hypothesis test is a decision criterion to select between two complementary hypotheses.

The null hypothesis, H0, which is assumed to be true prior to conducting the hypothesis test. It is compared to another hypothesis called the and denoted H1. The alternative hypothesis is often called the research hypothesis since the theory or what is believed to be true about the parameter is specified in the alternative hypothesis.

M´aster Universitario en Salud P´ublica 3/66 UPNA − Outline Introduction Hypothesis Tests for µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Introduction

Table 1 : Form of hypothesis test Null Hypothesis Alternative Hypothesis Type of Alternative

(A) H1 : θ<θ0 lower one-sided

H0 : θ = θ0 (B) H1 : θ>θ0 upper one-sided (C) H : θ = θ two-sided 1 6 0

M´aster Universitario en Salud P´ublica 4/66 UPNA − Outline Introduction Hypothesis Tests for µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Introduction-Example

Doctors wants to know the average height of women who are given epidural anesthesia in traditional sitting position. They suspect that the average height is greater than 163 cm.

H0 : µ = 163

H1 : µ> 163

M´aster Universitario en Salud P´ublica 5/66 UPNA − Outline Introduction Hypothesis Tests for µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Introduction

To help decide between the two hypotheses, calculate a test based on the information from the . Split the sample space into the rejection region R, and the acceptance region Rc . If the value of the falls in the rejection region, reject the null hypothesis and accept the alternative hypothesis.

M´aster Universitario en Salud P´ublica 6/66 UPNA − Outline Introduction Hypothesis Tests for µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Type I and Type II errors

Table 2 : Possible outcomes and the consequences for a trial by jury Jury’s Decision (Reality) Accept H0 Reject H0 (not guilty) (guilty) True State of the Defendant H0 True (innocent) Correct Type I error H0 False (guilty) Type II error Correct

M´aster Universitario en Salud P´ublica 7/66 UPNA − Outline Introduction Hypothesis Tests for µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Type I and Type II errors

The probability of committing a type I error (rejecting H0 when it is true), is called the level of significance for a hypothesis test. The level of significance is denoted by α where

α = P(type I error) = P(reject H H is true) 0| 0 = P( accept H H is true). 1| 0 The probability of committing a type II error is β where

β = P(type II error) = P(fail to reject H H is false) 0| 0 = P(accept H H is true). 0| 1 1 β is known as power of the test −

M´aster Universitario en Salud P´ublica 8/66 UPNA − Outline Introduction Hypothesis Tests for µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Type I and Type II errors

A type I error is frequently considered to be more serious than a type II error and the probability of a type I error is easier to control than the probability of a type II error It is common practice for researchers to specify a priori the largest probability of a type I error Researchers typically fix the probability of committing a type I error at the 0.01, 0.05, or 0.1 significance level

M´aster Universitario en Salud P´ublica 9/66 UPNA − Outline Introduction Hypothesis Tests for µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

P-value

The ℘-value is defined as the probability of observing a difference as extreme or more extreme than the difference observed under the assumption that the null hypothesis is true It is important to note that the ℘-value is not fixed a priori but rather is determined after the sample is taken. A small ℘-value indicates that observing differences as large or larger than the one found in the sample is rare, and thus do not occur by chance alone.

A small ℘-value lends support to H1; so given a fixed significance level α, reject H0 whenever the ℘-value < α.

M´aster Universitario en Salud P´ublica 10/66 UPNA − Outline Introduction Hypothesis Tests for µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Test of significance

Step 1: Hypotheses — State the null and alternative hypotheses.

Step 2: Test Statistic — Select an appropriate test statistic and its distribution under the null hypothesis.

Step 3: Calculate ℘-value—

Step 4: Statistical Conclusion —

Step 5: Explain Conclusion —

M´aster Universitario en Salud P´ublica 11/66 UPNA − Outline Introduction Hypothesis Tests for µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

R Commands

Hypothesis Test for µ and µ µ 1 − 2 t.test — Unknown Population Variance

M´aster Universitario en Salud P´ublica 12/66 UPNA − Outline Introduction Hypothesis Tests for µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Hypothesis Test for µ — Unknown Population Variance

Table 3 : Summary for testing the when sampling from a with unknown variance (one-sample t-test)

Null Standardized Test x¯ µ — H : µ = µ — t = − 0 Hypothesis 0 0 Statistic’s Value obs s/√n

Alternative H : µ<µ H : µ>µ H : µ = µ Hypothesis 1 0 1 0 1 6 0 Rejection Region tobs < tα;n 1 tobs > t1 α;n 1 tobs > t1 α/2;n 1 − − − | | − −

M´aster Universitario en Salud P´ublica 13/66 UPNA − Outline Introduction Hypothesis Tests for µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example EPIDURAL. (PASWR2)

Doctors wants to know the average height of women who are given epidural anesthesia in traditional sitting position. They suspect that the average height is greater than 163 cm.

M´aster Universitario en Salud P´ublica 14/66 UPNA − Outline Introduction Hypothesis Tests for µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Solution Using R: Checking normality

Check for normality: eda()

> library(PASWR2) > attach(EPIDURAL) > cm.sit<-cm[treatmen=="Traditional Sitting"] > eda(cm.sit) [1] "cm.sit" Size (n).... SW p-val 50.000.... 0.230

M´aster Universitario en Salud P´ublica 15/66 UPNA − Outline Introduction Hypothesis Tests for µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Solution Using R: Checking normality

EXPLORATORY DATA ANALYSIS

Histogram of cm.sit Density of cm.sit

Boxplot of cm.sit Q−Q Plot of cm.sit

M´aster Universitario en Salud P´ublica 16/66 UPNA − Outline Introduction Hypothesis Tests for µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.1: Hypothesis Test for µ — Solution

Step1: Hypotheses — H0 : µ = 163 versus H1 : µ> 163.

Step 2: Test Statistic — The test statistic chosen is X . n Pi=1 xi 8265 The value of this test statistic isx ¯ = n = 50 = 172.5. The standardized test statistic and its distribution under the assumption H0 is true are

X µ0 − t50 1. S/√n ∼ −

M´aster Universitario en Salud P´ublica 17/66 UPNA − Outline Introduction Hypothesis Tests for µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.1: Hypothesis Test for µ — Solution

Step 3: Rejection Region Calculations —

The rejection region is tobs > t1 0.05;49 = t0.95;49 =1.68 The value of the standardized test− statistic is x¯ µ0 172.5 163 t = − = − =2.36. obs s/√n 6.91/√50

1−pt(2.36,49)=0.011 dt(x, 49)

t0.95:49 = 1.68

−4 −2 0 2 2.36 4

M´aster Universitario en Salud P´ublica x 18/66 UPNA − Outline Introduction Hypothesis Tests for µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.1: Hypothesis Test for µ — Solution

Step 4: Statistical Conclusion — The ℘-value is P(t 2.36) = 0.01. 49 ≥ I. From the rejection region, reject H0 because tobs =2.36 is greater than 1.68.

II. From the ℘-value, reject H0 because the ℘-value = 0.01 is less than 0.05.

Reject H0.

Step 5: Explain Conclusion — There is evidence to suggest that the mean height of women in sitting position is greater than 163 cm.

M´aster Universitario en Salud P´ublica 19/66 UPNA − Outline Introduction Hypothesis Tests for µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Solution Using R: t.test()

> t.test(cm.sit,mu=163,alternative="g")

One Sample t-test

data: cm.sit t = 2.3552, df = 49, p-value = 0.01128 alternative hypothesis: true mean is greater than 163 95 percent : 163.6627 Inf sample estimates: mean of x 165.3

M´aster Universitario en Salud P´ublica 20/66 UPNA − Outline Introduction Hypothesis Tests for µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Non-parametric alternative: Wilcoxon Signed-Rank Test wilcox.test()

If normality does not hold, then use wilcox.test()

> wilcox.test(cm.sit,mu=163,alternative="g") Wilcoxon signed rank test with continuity correction

data: cm.sit V = 650.5, p-value = 0.01614 alternative hypothesis: true location is greater than 163

Warning messages: 1: In wilcox.test.default(cm.sit, mu = 163, alternative = "g") : cannot compute exact p-value with ties 2: In wilcox.test.default(cm.sit, mu = 163, alternative = "g") : cannot compute exact p-value with zeroes

M´aster Universitario en Salud P´ublica 21/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Test for a Difference in . Welch-Satterthwaite. Independent Populations

Table 4 : Summary for test for differences in mean when taking independent samples from normal distributions with unknown and unequal (Welch test)

Null Hypothesis — H : µX µY =0 0 − Standardized Test x¯ y¯ 0 — tobs = − − Statistic’s Value s2 s2 X + Y snX nY

Alternative Hypothesis Rejection Region H : µX µY < 0 t < t 1 − obs α;ν H1 : µX µY > 0 tobs > t1 α;ν − − H1 : µX µY =0 tobs > t1 α/2;ν − 6 | | − M´aster Universitario en Salud P´ublica 22/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.2: EPIDURAL

With the data set EPIDURAL, conduct a test of significance to see if the mean height of women attended by Dr A is equal to the mean height of women attended by Dr. B at significance level α =0.05, where now the variances are unknown but unequal.

M´aster Universitario en Salud P´ublica 23/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.2: EPIDURAL — Solution

To solve this problem, start by verifying if the normality assumption is reasonable. Now, proceed with the five-step procedure.

M´aster Universitario en Salud P´ublica 24/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.2: EPIDURAL

> cm.A<-cm[doctor=="Dr. A"] > cm.C<-cm[doctor=="Dr. C"] > eda(cm.A) .....SW p-val 0.225

> eda(cm.C) .....SW p-val 0.817

M´aster Universitario en Salud P´ublica 25/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.2: EPIDURAL. Dr. A and Dr. C — Solution with t.test()

> t.test(cm.A, cm.C, alternative="two.sided")

Welch Two Sample t-test

data: cm.A and cm.C t = -1.2259, df = 25.598, p-value = 0.2314 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -6.758720 1.711101 sample estimates: mean of x mean of y 164.0000 166.5238

M´aster Universitario en Salud P´ublica 26/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Non-parametric alternative: Mann-Whitney U-Test wilcox.test()

If normality does not hold, then use wilcox.test()

> wilcox.test(cm.A, cm.C, alternative="two.sided")

Wilcoxon rank sum test with continuity correction

data: cm.A and cm.C W = 194, p-value = 0.266 alternative hypothesis: true location shift is not equal to 0

Warning message: In wilcox.test.default(cm.A, cm.C, alternative = "two.sided") : cannot compute exact p-value with ties

M´aster Universitario en Salud P´ublica 27/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Test for comparing means. Two dependent samples

Table 5 : Summary for test for the mean of the differences between two dependent samples when the differences follow a normal distribution with unknown variance (paired t-test)

Null Hypothesis — H : µD = µX µY =0 0 − Standardized d¯ 0 Test Statistic’s — tobs = − sD /√nD Value

Alternative Hypothesis Rejection Region H1 : µD < 0 tobs < tα;n 1 − H1 : µD > 0 tobs > t1 α;n 1 − − H1 : µD =0 tobs > t1 α/2;n 1 6 | | − −

M´aster Universitario en Salud P´ublica 28/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.3: PHENYL (PASWR2)

Phenylketonuria (PKU) is a genetic disorder that is characterized by an inability of the body to utilize the essential amino acid, phenylalanine. Research suggests patients with phenylketonuria have deficiencies in coenzyme Q10. The data frame PHENYL records the level of Q10 at four different times for 46 patients diagnosed with phenylketonuria. The variable Q10.1 contains the level of Q10 measured in micromoles for the 46 patients. Q10.2, Q10.3, and Q10.4 are the values recorded at later times respectively for the 46 patients (the study lasted three years). Consider the second and fourth measurements of Q10. Is there evidence to suggest the level of Q10 decreases with the age?

M´aster Universitario en Salud P´ublica 29/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.3: PHENYL — Solution with t.test()

> library(PASWR2) > attach(PHENYL) > head(PHENYL) Q10.1 Q10.2 Q10.3 Q10.4 1 0.39 0.56 0.65 0.45 2 0.33 0.32 0.44 0.37 3 0.59 0.58 0.38 0.55 4 0.86 0.51 0.53 0.47 5 0.33 0.29 0.29 0.28 6 0.49 0.37 0.33 0.39 ......

M´aster Universitario en Salud P´ublica 30/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.3: PHENYL — Solution with t.test()

> d <- Q10.4 - Q10.2 > eda(d) [1] "d" Size (n) ...... SW p-val 46.000 ...... 0.272

M´aster Universitario en Salud P´ublica 31/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.3: PHENYL — Solution with t.test()

EXPLORATORY DATA ANALYSIS

Histogram of d Density of d

Boxplot of d Q−Q Plot of d

M´aster Universitario en Salud P´ublica 32/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.3: PHENYL — Solution with t.test()

> t.test(d, alternative = "less") One Sample t-test

data: d t = -2.6036, df = 45, p-value = 0.006226 alternative hypothesis: true mean is less than 0 95 percent confidence interval: -Inf -0.02423 sample estimates: mean of x -0.06826087

M´aster Universitario en Salud P´ublica 33/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.3: PHENYL — Solution with t.test()

Alternatively

> t.test(Q10.4, Q10.2, paired=TRUE, alternative="less")

Paired t-test

data: Q10.4 and Q10.2 t = -2.6036, df = 45, p-value = 0.006226 alternative hypothesis: true difference in means is less than 0 95 percent confidence interval: -Inf -0.02423 sample estimates: mean of the differences -0.06826087

M´aster Universitario en Salud P´ublica 34/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Non-parametric alternative: Wilcoxon Signed-Rank Test for paired data wilcox.test()

If normality does not hold, then use wilcox.test()

> wilcox.test(Q10.4, Q10.2, paired=TRUE,alternative="less")

Wilcoxon signed rank test with continuity correction

data: Q10.4 and Q10.2 V = 313, p-value = 0.01064 alternative hypothesis: true location shift is less than 0

Warning messages: 1: In wilcox.test.default(Q10.4, Q10.2, paired = TRUE, alternative = "less") cannot compute exact p-value with ties 2: In wilcox.test.default(Q10.4, Q10.2, paired = TRUE, alternative = "less") cannot compute exact p-value with zeroes

M´aster Universitario en Salud P´ublica 35/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Hypothesis Test for π

There are two possibilities 1 : binom.test() 2 Normal approximation (nπ 10 and n(1 π) 10 ): prop.test() ≥ − ≥

M´aster Universitario en Salud P´ublica 36/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.4: Exact Binomial Test. (EPIDURAL)

Consider the data set EPIDURAL from the library PASWR2. Use an exact test to check if the true proportion of women that have no complications with anesthesia is greater than 0.80.

> n.sample<-dim(EPIDURAL)[1] > n.sample [1] 85 > n.none<-sum(Complications=="None") > n.none [1] 76 ## Or > table(Complications) Complications Person got dizzy Too many OCs None Paresthesia Wet Tap 1 2 765 1

M´aster Universitario en Salud P´ublica 37/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.4: Exact Binomial Test.

> binom.test(n.none,n.sample,p=0.80,alternative="g") Exact binomial test

data: n.none and n.sample number of successes = 76, number of trials = 85, p-value = 0.01595 alternative hypothesis: true probability of success is greater than 0.8 95 percent confidence interval: 0.8225071 1.0000000 sample estimates: probability of success 0.8941176

M´aster Universitario en Salud P´ublica 38/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.5: Asymptotic Test for the Proportion. (EPIDURAL)

Consider the data set EPIDURAL from the library PASWR2. Use the normal approximation to test if the true proportion of women that have no complications with anesthesia is greater than 0.80.

M´aster Universitario en Salud P´ublica 39/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.5: Asymptotic Test for the proportion. (EPIDURAL)

> prop.test(n.none,n.sample,p=0.8,alternative="g", correct=TRUE)

1-sample proportions test with continuity correction

data: n.none out of n.sample, null probability 0.8 X-squared = 4.136, df = 1, p-value = 0.02099 alternative hypothesis: true p is greater than 0.8 95 percent confidence interval: 0.819639 1.000000 sample estimates: p 0.8941176

M´aster Universitario en Salud P´ublica 40/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.6. Hypothesis Tests for π π . Normal 1 − 2 approximation

Consider the data set EPIDURAL from the library PASWR2. Use the normal approximation to test if the true proportion of women having anesthesia in Hamstring Stretch position is smaller than the true proportion of women having anesthesia in Traditional Sitting position.

M´aster Universitario en Salud P´ublica 41/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.6: Hypothesis Tests for π π . Normal 1 − 2 approximation

> n.harm<-sum(treatmen=="Hamstring Stretch") > n.harm [1] 35 > n.sit<-sum(treatmen=="Traditional Sitting") > n.sit [1] 50

M´aster Universitario en Salud P´ublica 42/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.6: Hypothesis Tests for π π . Normal 1 − 2 approximation

> prop.test(c(n.harm,n.sit),c(n.sample,n.sample),alternative="l", correct=TRUE)

2-sample test for equality of proportions with continuity correction

data: c(n.harm, n.sit) out of c(n.sample, n.sample) X-squared = 4.6118, df = 1, p-value = 0.01588 alternative hypothesis: less 95 percent confidence interval: -1.00000000 -0.04053125 sample estimates: prop 1 prop 2 0.4117647 0.5882353

M´aster Universitario en Salud P´ublica 43/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Summary

Hypothesis Test for µ and µ µ 1 − 2 t.test — One sample: Unknown Population Variance t.test — Two sample 1 Independent populations 2 Paired data: argument paired=TRUE Hypothesis Test for π binom.test — Exact test prop.test — Normal approximation (for one and two samples)

M´aster Universitario en Salud P´ublica 44/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Analysis of variance

Suppose we have a populations with means µ1, µ2,...,µa The goal is to test

H0 : µ1 = µ2 = ... = µa

H : µi = µj , for some i, j 0 6 The test statistics is based on sample variances instead of sample means

M´aster Universitario en Salud P´ublica 45/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.7. Systolic pressure

A group of hypertensive patients are treated with four different drugs, A, B, C, and D to see if one of the drugs is better at reducing the blood pressure. The resulting blood pressure after having the drugs are given in the following table

Blood pressure A 119 118 121 122 120 122 B 126 124 124 126 127 127 C 117 118 116 117 115 116 D 123 125 122 124 122 122

M´aster Universitario en Salud P´ublica 46/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.7. Systolic pressure

If all subjects are identical If there are not differences due to the drugs

No differences A 121.375 121.375 121.375 121.375 121.375 121.375 B 121.375 121.375 121.375 121.375 121.375 121.375 C 121.375 121.375 121.375 121.375 121.375 121.375 D 121.375 121.375 121.375 121.375 121.375 121.375

M´aster Universitario en Salud P´ublica 47/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.7. Systolic pressure

If all subjects are identical There are differences due to the drugs

No random errors Differences are due to effect of drugs A 120.33 120.33 120.33 120.33 120.33 120.33 B 125.67 125.67 125.67 125.67 125.67 125.67 C 116.50 116.50 116.50 116.50 116.50 116.50 D 123.00 123.00 123.00 123.00 123.00 123.00

M´aster Universitario en Salud P´ublica 48/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.7. Systolic pressure

Subjects are usually different There are differences due to the drugs

Differences are due to the effect of drugs and subjects A 120.33-1.33=119 120.33-2.33=118 120.33+0.67=121 120.33+1.67=122 120.33-0.33=120 120.33+1.67=122 B 125.67+0.33=126 125.67-1.67=124 125.67-1.67=124 125.67+0.33=126 125.67+1.33=127 125.67+1.33=127 C 116.50+0.50=117 116.50+1.50=118 116.50-0.50=116 116.50+0.50=117 116.50-1.50=115 116.50-0.50=116 D 123.00+0.00=123 123.00+2.00=125 123.00-1.00=122 123.00+1.00=124 123.00-1.00=122 123.00-1.00=122

M´aster Universitario en Salud P´ublica 49/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Sum of squares

The variability is decompose into two sources Variability due to the treatment Uncontrolled variability due to random error

SSTotal = SS Treatment + SSError (1)

Compare SS Error and SS Treatment If SS >> SS = There exist significant differences Treatment Error ⇒ The test statistics follows an F distribution

M´aster Universitario en Salud P´ublica 50/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.7: Analysis of variance Blood pressure

> pressure<-c(119, 118, 121, 122, 120, 122, 126, 124, 124, 126, 127, 127, 117, 118, 116, 117, 115, 116, 123, 125, 122, 124, 122, 122) > treatment<-factor(rep(c("A","B","C","D"),each=6))

> blood.aov<-aov(pressure~treatment) > summary(blood.aov) Df Sum Sq Mean Sq F value Pr(>F) treatment 3 275.46 91.82 50.78 1.55e-09 *** Residuals 20 36.17 1.81 --- Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1

M´aster Universitario en Salud P´ublica 51/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Analysis of variance. Assumptions

The analysis of variance relies on three assumptions Normality of errors Homogeneity of variance Independence

M´aster Universitario en Salud P´ublica 52/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.7: Analysis of variance. Checking assumptions

> library(PASWR2) ## Graphical tools to check assumptions > checking.plots(blood.aov) ## standardized residuals > r<-rstandard(blood.aov) ## test de Shapiro-Wilk for normality > shapiro.test(r) Shapiro-Wilk data: r W = 0.9561, p-value = 0.3647 ##Test de levene for homogeneity of variance > library(car) > leveneTest(pressure~treatment) Levene’s Test for Homogeneity of Variance (center = ) Df F value Pr(>F) group 3 0.4524 0.7185 20

M´aster Universitario en Salud P´ublica 53/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.7: Analysis of variance. Checking assumptions

Residuals vs. ordered observations (Independence?) qq-norm of standardized residuals (Normality) Standardized residuals vs. fitted values (Homogeneity of variance)

Standardized residuals versus Normal Q−Q plot of standardized Standardized residuals versus ordered values for blood.aov residuals from blood.aov fitted values for blood.aov

20 20 20

standardized residuals standardized 9 residuals standardized 9 residuals standardized 9

−2 −12 0 1 2 −2 −12 0 1 2 −2 −1 0 1 2

5 10 15 20 −2 −1 0 1 2 118 120 122 124 126

ordered values Theoretical Quantiles fitted values

M´aster Universitario en Salud P´ublica 54/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Analysis of variance. Multiple comparisons

The analysis of variance does not indicate which means are different or how they differ We consider Tukeys Honestly Significant Difference to compare pair of treatments > blood.mca<-TukeyHSD(blood.aov) > blood.mca Tukey multiple comparisons of means 95% family-wise confidence level Fit: aov(formula = pressure ~ treatment) $treatment diff lwr upr p adj B-A 5.333333 3.1602739 7.5063928 0.0000063 C-A -3.833333 -6.0063928 -1.6602739 0.0004271 D-A 2.666667 0.4936072 4.8397261 0.0128635 C-B -9.166667 -11.3397261 -6.9936072 0.0000000 D-B -2.666667 -4.8397261 -0.4936072 0.0128635 D-C 6.500000 4.3269406 8.6730594 0.0000003

M´aster Universitario en Salud P´ublica 55/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Analysis of variance. Multiple comparisons

> plot(blood.mca)

95% family−wise confidence level D−C D−B C−B D−A C−A B−A

−10 −5 0 5

Differences in mean levels of treatment

M´aster Universitario en Salud P´ublica 56/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.8. Analysis of variance with two factors. EPIDURAL

With the data set EPIDURAL, conduct an analysis of variance to test if the mean height of women is different according to doctor and treatmen (Hamstring Stretch and Traditional Sitting). Use a significance level α =0.05.

M´aster Universitario en Salud P´ublica 57/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.8. Analysis of variance with two factors. EPIDURAL

> library(PASWR2) > attach(EPIDURAL) > twoway.plots(cm,doctor,treatmen)

Doctor Treatment cm cm 155 165 175 185 155 165 175 185

Dr. A Dr. B Dr. C Dr. D Hamstring Stretch Traditional Sitting

Interaction

cm cm 2 Dr.HamstringTraditional DCB StretchSitting 2 1 1 Dr. A 21 1 2 155 165 175 185 155 165 175 185 fac1 fac2 Dr. A Dr. B Dr. C Dr. D M´aster Universitario en Salud P´ublica Factors 58/66 Doctor UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.8. Analysis of variance with two factors. EPIDURAL

> epi.aov<-aov(cm~doctor+treatmen+doctor*treatmen) > summary(epi.aov) Df Sum Sq Mean Sq F value Pr(>F) doctor 3 75 25.16 0.564 0.640 treatmen 1 0 0.00 0.000 0.997 doctor:treatmen 3 17 5.81 0.130 0.942 Residuals 77 3433 44.58

The are not differences among doctors There are not differences among the levels of treatmen The is not statistically significant

M´aster Universitario en Salud P´ublica 59/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.8. Analysis of variance with two factors. Checking assumptions

> checking.plots(epi.aov) > r<-rstandard(epi.aov) > shapiro.test(r)

Shapiro-Wilk normality test

data: r W = 0.9912, p-value = 0.8435

> leveneTest(cm~doctor*treatmen) Levene’s Test for Homogeneity of Variance (center = median) Df F value Pr(>F) group 7 2.3898 0.02886 * 77

M´aster Universitario en Salud P´ublica 60/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.8. Analysis of variance with two factors. Checking assumptions

Standardized residuals versus Normal Q−Q plot of standardized Standardized residuals versus ordered values for epi.aov residuals from epi.aov fitted values for epi.aov 51 51 51 standardized residuals standardized residuals standardized residuals standardized 14 23 2314 14 23 −3 −2 −1 0 1 2 3 −3 −2 −1 0 1 2 3 −3 −2 −1 0 1 2 3

0 20 40 60 80 −3 −2 −1 0 1 2 3 164.0 165.0 166.0 167.0

ordered values Theoretical Quantiles fitted values

M´aster Universitario en Salud P´ublica 61/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Summary. Analysis of variance

Hypothesis Test for µ1 = µ2 = ... = µa aov() — One or more factor variables Checking assumptions checking.plots() (PASWR2) shapiro.test() leveneTest() (car) Multiple comparisons. Test de Tukey TukeyHSD()

M´aster Universitario en Salud P´ublica 62/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Non-parametric alternative: Kruskal-Wallis test kruskal.test(). Example 3.9: diabetes

If normality does not hold, then use kruskal.test()

Use the the Kruskal-Wallis test to see if there exist differences among the cholesterol levels according to the body frame.

M´aster Universitario en Salud P´ublica 63/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.9: Solution with R

> diabetes<-read.table("diabetes.txt",header=TRUE) > attach(diabetes)

> kruskal.test(chol~frame)

Kruskal-Wallis rank sum test

data: chol by frame Kruskal-Wallis chi-squared = 7.6388, df = 2, p-value = 0.02194

There exist differences in cholesterol among the levels of frame.

M´aster Universitario en Salud P´ublica 64/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.9: Multiple comparisons with R. library(agricolae)

> library(agricolae) > kruskal(chol,frame, alpha = 0.05, group=FALSE, main = NULL,console=TRUE)

Study: chol ~ frame Kruskal-Wallis test’s Ties or no Ties

Value: 7.638769 degrees of freedom: 2 Pvalue chisq : 0.0219413 . . . .

M´aster Universitario en Salud P´ublica 65/66 UPNA − Outline Introduction HypothesisTestsfor µ Hypothesis Tests for µX µY Hypothesis Tests for π Analysis of variance

Example 3.9: Multiple comparisons with R. library(agricolae)

......

Comparison between treatments mean of the ranks

Difference pvalue sig. LCL UCL large - medium -6.941698 0.622136 -34.61104064 20.72764 large - small 31.516897 0.050642 . -0.08839348 63.12219 medium - small 38.458595 0.006398 ** 10.87659101 66.04060

There are statistically significant differences in cholesterol between body frame medium and small.

M´aster Universitario en Salud P´ublica 66/66 UPNA