Sei sulla pagina 1di 3

ST1051 Practical 8

Hypothesis Testing and Confidence Intervals

One sample test for a proportion

Suppose we have a binomial random variable with an unknown probability of success p, and we
have a value for the number of successes that have occurred (i.e. we have a value for the random
variable). We may want to test whether p = some specific value, for example 0.5. We can use the
command binom.test() as follows:

We have to tell R

(i) How many successes were observed


(ii) How many trials were conducted and
(iii) What value of p do we want to use in the test.
If out of n trials we observe k successes and we want to test whether p=0.4, we would type:

binom.test(k,n,p=0.4)

For example, suppose we toss a coin 1000 times and we observe 550 heads. We want to test
whether this is a fair coin or not. We would type:

binom.test(550,1000,p=0.5)

With this form of the command, the alternative hypothesis is two-sided (i.e. “not equal to 0.5”).
Examine help(binom.test) – in particular the way to specify a one-sided test.

One sample t-test

Suppose we have a random sample of people, and we want to test whether the mean age of the
population (from which our sample is drawn) is equal to 30. We will use the t.test() command in R to
perform a hypothesis test as well as to construct a confidence interval. We must tell R

(i) The name of the variable containing the data


(ii) The value we want to compare the mean of our variable to
(iii) The size of the confidence interval we want
Part (iii) is optional and is used only if we want a confidence interval for the mean. Try the following
example:

age=c(52,22,34,56,15,20,17,44,19,35,41,36)

t.test(age,mu=30,conf.level=0.95)

this procedure tests the null hypothesis that the mean age (of the population) is equal to 30 years.
The resulting output provides the test statistic, degrees of freedom, and p-value for the test, along
with a point estimate of the mean age, and a 95% confidence interval for the mean age. In this case
the estimated mean age is 32.58 which is not significantly different from 30 (p-value = 0.54). We may
want to just construct a confidence interval for the mean of age without the t-test.

Confidence interval for a mean from a small sample (n<30):


𝜎
𝑋̅ ± 𝑡𝑛−1
√𝑛

If the population standard deviation σ is unknown it can be estimated with the sample standard
deviation s. Mean and standard deviation are easily found in R and we can use the qt function to find
the critical value in the t-distribution.

xbar=mean(age)

t=qt(0.975,11)

sigma=sd(age)

standerror=sigma/sqrt(12)

xbar+t*(standerror)

xbar-t*(standerror)

As usual the arguments can be changed to whatever we require. For example, to test the null
hypothesis that the mean age is 20 and to obtain a 90% confidence interval we type:

t.test(age,mu=20,conf.level=0.9)

Find the confidence interval manually.

Comparing two proportions

Suppose we want to compare the effect of Timolol with that of a placebo for patients who have
angina pectoris.

Timolol Placebo
Angina-free 44 19
Not Angina-free 116 128

The standard hypothesis test is 𝐻0 : 𝑝1 = 𝑝2 against the alternative (two-sided) 𝐻1 : 𝑝1 ≠ 𝑝2 . The


function prop.test may be used here.

prop.test(c(44,19),c(44+116,19+128))

The on-screen output:


2-sample test for equality of proportions with continuity
correction
data: c(44, 19) out of c(44 + 116, 19 + 128)
X-squared = 9.1046, df = 1, p-value = 0.002550
alternative hypothesis: two.sided
95 percent confidence interval:
0.05131344 0.24018316
sample estimates:
prop 1 prop 2
0.2750000 0.1292517

Two Sample t-test

Suppose we had a sample of males and a sample of females. Are the (population) mean ages the
same for males and females?

We must tell the R the two variables we wish to compare.

age=c(52,22,34,56,15,20,17,44,19,35,41,36)

gender=c(“M”,”F”,”M”,”M”,”M”,”F”,”F”,”M”,”F”,”M”,”F”,”F”)

male.age=age[gender==”M”]

female.age=age[gender==”F”]

t.test(male.age,female.age)

Potrebbero piacerti anche