Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
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.
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
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.
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)
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
prop.test(c(44,19),c(44+116,19+128))
Suppose we had a sample of males and a sample of females. Are the (population) mean ages the
same for males and females?
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)