Exercise 2.3 The data set UCBAdmissions is a 3-way table of frequencies classified by Admit, Gender, and Dept.

UCB = UCBAdmissions

(a) Find the total number of cases contained in this table.

summary(UCB)
## Number of cases in table: 4526 
## Number of factors: 3 
## Test for independence of all factors:
##  Chisq = 2000.3, df = 16, p-value = 0

The total number of cases contained in this table is 4526

(b) For each department, find the total number of applicants.

margin.table(UCB, 3)
## Dept
##   A   B   C   D   E   F 
## 933 585 918 792 584 714

Above is the total number of applicants in each department.

(c) For each department, find the overall proportion of applicants who were admitted.

UCBSum = margin.table(UCB,c(1,3))
UCBProp = prop.table(UCBSum, 2)
UCBProp
##           Dept
## Admit               A          B          C          D          E
##   Admitted 0.64415863 0.63247863 0.35076253 0.33964646 0.25171233
##   Rejected 0.35584137 0.36752137 0.64923747 0.66035354 0.74828767
##           Dept
## Admit               F
##   Admitted 0.06442577
##   Rejected 0.93557423

Above is the overall proportion of applicants who were admitted by department.

(d) Construct a tabular display of department (rows) and gender (columns), showing the proportion of applicants in each cell who were admitted relative to the total applicants in that cell.

UCBAdmTot = margin.table(UCB,c(3,2))
UCBAdmTot
##     Gender
## Dept Male Female
##    A  825    108
##    B  560     25
##    C  325    593
##    D  417    375
##    E  191    393
##    F  373    341
UCBAdm = margin.table(UCB[1,,],c(2,1))
UCBAdm
##     Gender
## Dept Male Female
##    A  512     89
##    B  353     17
##    C  120    202
##    D  138    131
##    E   53     94
##    F   22     24
UCBProp2 = UCBAdm/UCBAdmTot
UCBProp2
##     Gender
## Dept       Male     Female
##    A 0.62060606 0.82407407
##    B 0.63035714 0.68000000
##    C 0.36923077 0.34064081
##    D 0.33093525 0.34933333
##    E 0.27748691 0.23918575
##    F 0.05898123 0.07038123

Exercise 2.5 The data set UKSoccer in vcd gives the distributions of number of goals scored by the 20 teams in the 1995/96 season of the Premier League of the UK Football Association. #This two-way table classifies all 20 × 19 = 380 games by the joint outcome (Home, Away), the number of goals scored by the Home and Away teams. The value 4 in this table actually represents 4 or more goals.

(a) Verify that the total number of games represented in this table is 380.

library(vcd)
## Loading required package: grid
summary(UKSoccer)
## Number of cases in table: 380 
## Number of factors: 2 
## Test for independence of all factors:
##  Chisq = 18.699, df = 16, p-value = 0.2846
##  Chi-squared approximation may be incorrect

The total number of games in this table is 380

(b) Find the marginal total of the number of goals scored by each of the home and away teams.

UKSocTot = addmargins(UKSoccer)
UKSocTot
##      Away
## Home    0   1   2   3   4 Sum
##   0    27  29  10   8   2  76
##   1    59  53  14  12   4 142
##   2    28  32  14  12   4  90
##   3    19  14   7   4   1  45
##   4     7   8  10   2   0  27
##   Sum 140 136  55  38  11 380

(c) Express each of the marginal totals as proportions.

prop.table(UKSocTot)
##      Away
## Home             0            1            2            3            4
##   0   0.0177631579 0.0190789474 0.0065789474 0.0052631579 0.0013157895
##   1   0.0388157895 0.0348684211 0.0092105263 0.0078947368 0.0026315789
##   2   0.0184210526 0.0210526316 0.0092105263 0.0078947368 0.0026315789
##   3   0.0125000000 0.0092105263 0.0046052632 0.0026315789 0.0006578947
##   4   0.0046052632 0.0052631579 0.0065789474 0.0013157895 0.0000000000
##   Sum 0.0921052632 0.0894736842 0.0361842105 0.0250000000 0.0072368421
##      Away
## Home           Sum
##   0   0.0500000000
##   1   0.0934210526
##   2   0.0592105263
##   3   0.0296052632
##   4   0.0177631579
##   Sum 0.2500000000