Assignment 1

library(vcd)
## Loading required package: grid
data(UCBAdmissions)
summary(UCBAdmissions) #No. of cases in table 
## Number of cases in table: 4526 
## Number of factors: 3 
## Test for independence of all factors:
##  Chisq = 2000.3, df = 16, p-value = 0
ftable(UCBAdmissions) # Total number of applicants by Dept
##                 Dept   A   B   C   D   E   F
## Admit    Gender                             
## Admitted Male        512 353 120 138  53  22
##          Female       89  17 202 131  94  24
## Rejected Male        313 207 205 279 138 351
##          Female       19   8 391 244 299 317
prop.table(UCBAdmissions) # Overall proportion of applicants admitted in each Dept
## , , Dept = A
## 
##           Gender
## Admit             Male      Female
##   Admitted 0.113124171 0.019664163
##   Rejected 0.069155988 0.004197967
## 
## , , Dept = B
## 
##           Gender
## Admit             Male      Female
##   Admitted 0.077993814 0.003756076
##   Rejected 0.045735749 0.001767565
## 
## , , Dept = C
## 
##           Gender
## Admit             Male      Female
##   Admitted 0.026513478 0.044631021
##   Rejected 0.045293858 0.086389748
## 
## , , Dept = D
## 
##           Gender
## Admit             Male      Female
##   Admitted 0.030490499 0.028943880
##   Rejected 0.061643836 0.053910738
## 
## , , Dept = E
## 
##           Gender
## Admit             Male      Female
##   Admitted 0.011710119 0.020768891
##   Rejected 0.030490499 0.066062749
## 
## , , Dept = F
## 
##           Gender
## Admit             Male      Female
##   Admitted 0.004860804 0.005302696
##   Rejected 0.077551922 0.070039770
aperm(UCBAdmissions,c(3,2,1)) 
## , , Admit = Admitted
## 
##     Gender
## Dept Male Female
##    A  512     89
##    B  353     17
##    C  120    202
##    D  138    131
##    E   53     94
##    F   22     24
## 
## , , Admit = Rejected
## 
##     Gender
## Dept Male Female
##    A  313     19
##    B  207      8
##    C  205    391
##    D  279    244
##    E  138    299
##    F  351    317
#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.
data("UKSoccer", package = "vcd")
ftable(UKSoccer)
##      Away  0  1  2  3  4
## Home                    
## 0         27 29 10  8  2
## 1         59 53 14 12  4
## 2         28 32 14 12  4
## 3         19 14  7  4  1
## 4          7  8 10  2  0
summary(UKSoccer) #Verify that the total number of games represented in this table is 380.
## 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
addmargins(UKSoccer) #Find the marginal total of the number of goals scored by each of the home and away teams.
##      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
prop.table(UKSoccer) #Express each of the marginal totals as proportions.
##     Away
## Home           0           1           2           3           4
##    0 0.071052632 0.076315789 0.026315789 0.021052632 0.005263158
##    1 0.155263158 0.139473684 0.036842105 0.031578947 0.010526316
##    2 0.073684211 0.084210526 0.036842105 0.031578947 0.010526316
##    3 0.050000000 0.036842105 0.018421053 0.010526316 0.002631579
##    4 0.018421053 0.021052632 0.026315789 0.005263158 0.000000000