The data set UCBAdmissions is a 3-way table of frequencies classified by Admit, Gender, and Dept.
data("UCBAdmissions")
View(UCBAdmissions)
summary(UCBAdmissions)
## 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 number of cases in this table is 4526.
table1<-UCBAdmissions%>%
as.data.frame()%>%
group_by(Dept)%>%
summarize(sum1=sum(Freq))
table1
## # A tibble: 6 x 2
## Dept sum1
## <fct> <dbl>
## 1 A 933.
## 2 B 585.
## 3 C 918.
## 4 D 792.
## 5 E 584.
## 6 F 714.
table2<-UCBAdmissions%>%
as.data.frame()%>%
group_by(Dept,Admit)%>%
summarize(sum2=sum(Freq)) %>%
filter(Admit=="Admitted")
table2$admission_rate<-100*table2$sum2/(table1$sum1)
table2
## # A tibble: 6 x 4
## # Groups: Dept [6]
## Dept Admit sum2 admission_rate
## <fct> <fct> <dbl> <dbl>
## 1 A Admitted 601. 64.4
## 2 B Admitted 370. 63.2
## 3 C Admitted 322. 35.1
## 4 D Admitted 269. 34.0
## 5 E Admitted 147. 25.2
## 6 F Admitted 46. 6.44
table3<-t(percentages(UCBAdmissions,which="Gender",by="Dept"))
table3
## Gender
## Dept Male Female
## A 88.424437 11.575563
## B 95.726496 4.273504
## C 35.403050 64.596950
## D 52.651515 47.348485
## E 32.705479 67.294521
## F 52.240896 47.759104
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.
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)
## 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
table4<-addmargins(UKSoccer)
table4
## 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
vec1<-t(prop.table(table4[1:5,6]))
vec2<-prop.table(table4[6,1:5])
table5<-table4%>%
as.data.frame()%>%
filter(Home=="Sum") %>%
mutate(prec_away=Freq/380)
table5
## Home Away Freq prec_away
## 1 Sum 0 140 0.36842105
## 2 Sum 1 136 0.35789474
## 3 Sum 2 55 0.14473684
## 4 Sum 3 38 0.10000000
## 5 Sum 4 11 0.02894737
## 6 Sum Sum 380 1.00000000
table6<-table4%>%
as.data.frame()%>%
filter(Away=="Sum") %>%
mutate(prec_Home=Freq/380)
table6
## Home Away Freq prec_Home
## 1 0 Sum 76 0.20000000
## 2 1 Sum 142 0.37368421
## 3 2 Sum 90 0.23684211
## 4 3 Sum 45 0.11842105
## 5 4 Sum 27 0.07105263
## 6 Sum Sum 380 1.00000000
The perc_home calculates the percentage of the frequency of the away team’s scores. For example, away team score 1 in 136 games, which is 35.79% percentage of the total 380 games. The perc_away calculates the percentage of the frequency of the home team’s score. For example, home team score 0 in 76 games, which is 20% of the total 380 games.
homesum<-sum(margin.table(UKSoccer,1)*c(0,1,2,3,4))
homesum
## [1] 565
homemean<-homesum/380
homemean
## [1] 1.486842
awaysum<-sum(margin.table(UKSoccer,2)*c(0,1,2,3,4))
awaysum
## [1] 404
awaymean<-awaysum/380
awaymean
## [1] 1.063158
The results shows that the total score that home team got is 565 and average score per team is 1.49. Away team score in total 404 goals, on leverage score 1.06 each game, less than home team