Worksheet 2
MATH7016 The Nature of Data
1. The following data is from lectures.
Table 1: Numbers of refugees in each distress category
low moderate high very high
123 70 93 157
The data can be entered directly into R by typing,
iraqi = c(123, 70, 93, 157)
names(iraqi) = c("low", "moderate", "high", "very high")
Use the barplot command to draw a barplot of this data.
barplot(iraqi)
Horizontal barplots can be drawn by including the horiz option.
barplot(iraqi, horiz=TRUE)
2. The following data are the AIHW percentages of each distress category.
Table 2: Australians in each distress category (%)
low moderate high very high
70.65 18.5 7.41 3.43
Enter these into R, and draw a barplot.
3. There are 443 total Iraqi refugees. If they follow the AIHW percentages, the expected counts are
(assuming you entered AIHW percentages into a variable named aihw)
e = 443*aihw/100
4. Compute the chi-squared distance of the Iraqi counts from the expected values
1
sum((iraqi-e)ˆ2/e)
5. Simulate counts from the AIHW percentages. There are two ways to do this. Firstly, we can simulate
443 observations from four categories and make a table.
table(sample(1:4, replace=TRUE, prob=aihw/100, size=443))
(I have used 1 to 4 as the category names here. I could use the original names low, moderate, high, very
high but R would then reorder them into alphabetical order. This could be fixed but this way is simpler.)
A second way is to simulate the counts directly. This is an example of a multinomial distribution, so;
rmultinom(1, 443, aihw/100)
6. Putting it all together:
e = 443*aihw/100
[Link] = sum((iraqi-e)ˆ2/e)
x = replicate(1000, {
counts = rmultinom(1, 443, aihw/100)
chisq = sum((counts-e)ˆ2/e)
})
sum(x>[Link])
Chi-square approximation.
7. R has the chi-square distribution built in. We can get the proportion less than a certain value with
pchisq([Link], df=3)
(There are 4 cells in the table so we use df=3)
In fact, R will do the whole job for us,
[Link](iraqi, p=aihw, rescale.p=TRUE)
I’ve been mean because R will even do the simulation version.
[Link](iraqi, p=aihw, rescale.p=TRUE, [Link] = TRUE, B=2000)
Eels data
1. Entering the Eel data is a bit more complex.
Border Grass Sand
[Link] 264 127 99
2
Border Grass Sand
[Link] 161 116 67
We can use the matrix function. (Notice the column-wise data order)
eels = matrix(c(264, 161, 127, 116, 99, 67), ncol=3)
Or a function called rbind that binds rows together.
eels = rbind(c(264, 127, 99), c(161, 116, 67))
To add names to both dimensions we use
dimnames(eels) = list(species = c("[Link]", "[Link]"),
location = c("Border", "Grass", "Sand"))
2. Expected values. To calculate the expected values we need to compute the species totals multiplied
into the columns proportions . We can use colSums to get the column sums and rowSums for row
sums. . .
[Link] = rowSums(eels)
[Link] = colSums(eels)
We can do the multiplication of each species count by each location counts (and get a table as a result) using
something called an outer [Link] we divide this by total number of eels, we will get expected counts.
outer([Link], [Link]) / sum(eels)
## Border Grass Sand
## [Link] 249.7002 142.7698 97.52998
## [Link] 175.2998 100.2302 68.47002
3. Simulation. We need allocate 834 eels to species and location, using first the species proportions and
then the overall location proportions. I leave this a Challenge question!
4. Again we can use R to do the simulation.
[Link](eels, [Link] = TRUE, B=2000)
Or use the chi-square approximation.
[Link](eels)
3
Other examples
1. ABC bank has determined the following counts of use of its ATM.
Mon Tues Wed Thurs Fri
253 197 204 279 267
Enter the data, and use [Link] to obtain a p-value, for the hypothesis that usage is equally spread.
(Use simulation and the approximation)
2. Clasp you hands together. Which thumb is on top? Everyone has two copies of a gene which determines
which thumb is most comfortable on top, the variants can be labelled L and R, an individual is either
LL, LR, or RR. Counts of 65 children found,
LL LR RR
14 31 20
According to Mendelian genetics 25% should be LL, 50% LR and 25% RR. Use [Link] to see if the
data is consistent with this hypothesis (simulation and approximation)
3. 300 adults were asked whether school teachers should be given more freedom to punish unruly students.
The following results were obtained?
In favour Against No opinion
Men 93 70 12
Women 87 32 6
Do men and women have the same distribution of opinions?
4. Two drugs are administered to patients to treat the same disease.
Cured Not Cured
Drug A 44 16
Drug B 18 22
Are the drugs equally effective?