Data input
load("./sejong_poll_data.RData")
Data
kable(sejong.poll[4:1])
Seoul |
high |
yes |
21 |
Seoul |
high |
no |
194 |
Seoul |
third.current |
yes |
259 |
Seoul |
third.current |
no |
393 |
Seoul |
third.ex |
yes |
443 |
Seoul |
third.ex |
no |
117 |
yuhu |
ordinary |
yes |
1123 |
yuhu |
ordinary |
no |
71 |
gyunggi |
chief |
yes |
29 |
gyunggi |
chief |
no |
5 |
gyunggi |
ordinary |
yes |
17076 |
gyunggi |
ordinary |
no |
236 |
pyungan |
high |
no |
1 |
pyungan |
chief |
yes |
6 |
pyungan |
chief |
no |
35 |
pyungan |
ordinary |
yes |
1326 |
pyungan |
ordinary |
no |
28474 |
hwanghae |
chief |
yes |
17 |
hwanghae |
chief |
no |
17 |
hwanghae |
ordinary |
yes |
4454 |
hwanghae |
ordinary |
no |
15601 |
chungcheong |
high |
no |
2 |
chungcheong |
chief |
yes |
35 |
chungcheong |
chief |
no |
26 |
chungcheong |
ordinary |
yes |
6982 |
chungcheong |
ordinary |
no |
14013 |
kangwon |
chief |
yes |
5 |
kangwon |
chief |
no |
10 |
kangwon |
ordinary |
yes |
939 |
kangwon |
ordinary |
no |
6888 |
hamgil |
high |
no |
1 |
hamgil |
chief |
yes |
3 |
hamgil |
chief |
no |
14 |
hamgil |
ordinary |
yes |
75 |
hamgil |
ordinary |
no |
7387 |
gyungsang |
chief |
yes |
55 |
gyungsang |
chief |
no |
16 |
gyungsang |
ordinary |
yes |
36262 |
gyungsang |
ordinary |
no |
377 |
jeolla |
high |
no |
2 |
jeolla |
chief |
yes |
42 |
jeolla |
chief |
no |
12 |
jeolla |
ordinary |
yes |
29505 |
jeolla |
ordinary |
no |
257 |
Korean Factor Levels
sejong.poll.kr <- sejong.poll
sejong.poll.kr$vote <- factor(sejong.poll.kr$vote, levels = c("yes", "no"), labels = c("찬성", "반대"))
class.labels.kr <- c("대신 등", "3품이하현직", "3품이하전직", "수령", "품관촌민")
sejong.poll.kr$class <- factor(sejong.poll.kr$class, levels = class.levels, labels = class.labels.kr)
region.labels.kr <- c("서울", "유후사", "경기", "평안", "황해", "충청", "강원", "함길", "경상", "전라")
sejong.poll.kr$region <- factor(sejong.poll.kr$region, levels = region.levels, labels = region.labels.kr)
vnames.kr <- c("소계", "찬반", "계급", "지역")
# names(sejong.poll.kr) <- vnames.kr
kable(sejong.poll.kr[4:1])
서울 |
대신 등 |
찬성 |
21 |
서울 |
대신 등 |
반대 |
194 |
서울 |
3품이하현직 |
찬성 |
259 |
서울 |
3품이하현직 |
반대 |
393 |
서울 |
3품이하전직 |
찬성 |
443 |
서울 |
3품이하전직 |
반대 |
117 |
유후사 |
품관촌민 |
찬성 |
1123 |
유후사 |
품관촌민 |
반대 |
71 |
경기 |
수령 |
찬성 |
29 |
경기 |
수령 |
반대 |
5 |
경기 |
품관촌민 |
찬성 |
17076 |
경기 |
품관촌민 |
반대 |
236 |
평안 |
대신 등 |
반대 |
1 |
평안 |
수령 |
찬성 |
6 |
평안 |
수령 |
반대 |
35 |
평안 |
품관촌민 |
찬성 |
1326 |
평안 |
품관촌민 |
반대 |
28474 |
황해 |
수령 |
찬성 |
17 |
황해 |
수령 |
반대 |
17 |
황해 |
품관촌민 |
찬성 |
4454 |
황해 |
품관촌민 |
반대 |
15601 |
충청 |
대신 등 |
반대 |
2 |
충청 |
수령 |
찬성 |
35 |
충청 |
수령 |
반대 |
26 |
충청 |
품관촌민 |
찬성 |
6982 |
충청 |
품관촌민 |
반대 |
14013 |
강원 |
수령 |
찬성 |
5 |
강원 |
수령 |
반대 |
10 |
강원 |
품관촌민 |
찬성 |
939 |
강원 |
품관촌민 |
반대 |
6888 |
함길 |
대신 등 |
반대 |
1 |
함길 |
수령 |
찬성 |
3 |
함길 |
수령 |
반대 |
14 |
함길 |
품관촌민 |
찬성 |
75 |
함길 |
품관촌민 |
반대 |
7387 |
경상 |
수령 |
찬성 |
55 |
경상 |
수령 |
반대 |
16 |
경상 |
품관촌민 |
찬성 |
36262 |
경상 |
품관촌민 |
반대 |
377 |
전라 |
대신 등 |
반대 |
2 |
전라 |
수령 |
찬성 |
42 |
전라 |
수령 |
반대 |
12 |
전라 |
품관촌민 |
찬성 |
29505 |
전라 |
품관촌민 |
반대 |
257 |
# kable(sejong.poll.kr[4:1], align = NULL, col.names = vnames.kr[4:1])
Cross Tables
tbl.vote <- xtabs(counts ~ vote, data = sejong.poll.kr)
tbl.class <- xtabs(counts ~ class, data = sejong.poll.kr)
tbl.region <- xtabs(counts ~ region, data = sejong.poll.kr)
tbl.vote.class <- xtabs(counts ~ vote + class, data = sejong.poll.kr)
tbl.vote.region <- xtabs(counts ~ vote + region, data = sejong.poll.kr)
tbl.vote.class.region <- xtabs(counts ~ vote + class + region, data = sejong.poll.kr)
ftbl.vote.class.region <- ftable(xtabs(counts ~ vote + class + region, data = sejong.poll.kr))
Array
We can set up the data as an array
sejong.poll.array <- xtabs(counts ~ vote + class + region, data = sejong.poll.kr)
str(sejong.poll.array)
## int [1:2, 1:5, 1:10] 21 194 259 393 443 117 0 0 0 0 ...
## - attr(*, "dimnames")=List of 3
## ..$ vote : chr [1:2] "찬성" "반대"
## ..$ class : chr [1:5] "대신 등" "3품이하현직" "3품이하전직" "수령" ...
## ..$ region: chr [1:10] "서울" "유후사" "경기" "평안" ...
## - attr(*, "class")= chr [1:2] "xtabs" "table"
## - attr(*, "call")= language xtabs(formula = counts ~ vote + class + region, data = sejong.poll.kr)
sejong.poll.array
## , , region = 서울
##
## class
## vote 대신 등 3품이하현직 3품이하전직 수령 품관촌민
## 찬성 21 259 443 0 0
## 반대 194 393 117 0 0
##
## , , region = 유후사
##
## class
## vote 대신 등 3품이하현직 3품이하전직 수령 품관촌민
## 찬성 0 0 0 0 1123
## 반대 0 0 0 0 71
##
## , , region = 경기
##
## class
## vote 대신 등 3품이하현직 3품이하전직 수령 품관촌민
## 찬성 0 0 0 29 17076
## 반대 0 0 0 5 236
##
## , , region = 평안
##
## class
## vote 대신 등 3품이하현직 3품이하전직 수령 품관촌민
## 찬성 0 0 0 6 1326
## 반대 1 0 0 35 28474
##
## , , region = 황해
##
## class
## vote 대신 등 3품이하현직 3품이하전직 수령 품관촌민
## 찬성 0 0 0 17 4454
## 반대 0 0 0 17 15601
##
## , , region = 충청
##
## class
## vote 대신 등 3품이하현직 3품이하전직 수령 품관촌민
## 찬성 0 0 0 35 6982
## 반대 2 0 0 26 14013
##
## , , region = 강원
##
## class
## vote 대신 등 3품이하현직 3품이하전직 수령 품관촌민
## 찬성 0 0 0 5 939
## 반대 0 0 0 10 6888
##
## , , region = 함길
##
## class
## vote 대신 등 3품이하현직 3품이하전직 수령 품관촌민
## 찬성 0 0 0 3 75
## 반대 1 0 0 14 7387
##
## , , region = 경상
##
## class
## vote 대신 등 3품이하현직 3품이하전직 수령 품관촌민
## 찬성 0 0 0 55 36262
## 반대 0 0 0 16 377
##
## , , region = 전라
##
## class
## vote 대신 등 3품이하현직 3품이하전직 수령 품관촌민
## 찬성 0 0 0 42 29505
## 반대 2 0 0 12 257
Votes
Total
Check the total vote with xtabs()
vote.total <- xtabs(counts ~ vote, data = sejong.poll.kr)
kable(t(as.matrix(vote.total)), caption = "총계")
# format(prop.table(vote.total)*100, digits = 3, nsmall = 1)
kable(t(as.matrix(format(prop.table(vote.total)*100, digits = 3, nsmall = 1))), caption = "백분률", align = rep("r", 2))
vote.total.2 <- apply(sejong.poll.array, 1, sum)
# kable(t(as.matrix(vote.total.2)))
kable(t(as.matrix(vote.total.2)), caption = "총계")
Vote by class
vote.class <- xtabs(counts ~ vote + class, data = sejong.poll.kr)
kable(vote.class, caption = "계급별")
계급별
찬성 |
21 |
259 |
443 |
192 |
97742 |
반대 |
200 |
393 |
117 |
135 |
73304 |
vote.class.a <- apply(sejong.poll.array, 1:2, sum)
kable(vote.class.a, caption = "계급별")
계급별
찬성 |
21 |
259 |
443 |
192 |
97742 |
반대 |
200 |
393 |
117 |
135 |
73304 |
Commons vs Bureaucrats
We need to analyse Commons separately.
sejong.poll.kr$class.2 <- factor(ifelse(sejong.poll.kr$class == "품관촌민", "품관촌민", "관료"), levels = c("관료", "품관촌민"))
kable(sejong.poll.kr[c(4, 3, 5, 2, 1)])
서울 |
대신 등 |
관료 |
찬성 |
21 |
서울 |
대신 등 |
관료 |
반대 |
194 |
서울 |
3품이하현직 |
관료 |
찬성 |
259 |
서울 |
3품이하현직 |
관료 |
반대 |
393 |
서울 |
3품이하전직 |
관료 |
찬성 |
443 |
서울 |
3품이하전직 |
관료 |
반대 |
117 |
유후사 |
품관촌민 |
품관촌민 |
찬성 |
1123 |
유후사 |
품관촌민 |
품관촌민 |
반대 |
71 |
경기 |
수령 |
관료 |
찬성 |
29 |
경기 |
수령 |
관료 |
반대 |
5 |
경기 |
품관촌민 |
품관촌민 |
찬성 |
17076 |
경기 |
품관촌민 |
품관촌민 |
반대 |
236 |
평안 |
대신 등 |
관료 |
반대 |
1 |
평안 |
수령 |
관료 |
찬성 |
6 |
평안 |
수령 |
관료 |
반대 |
35 |
평안 |
품관촌민 |
품관촌민 |
찬성 |
1326 |
평안 |
품관촌민 |
품관촌민 |
반대 |
28474 |
황해 |
수령 |
관료 |
찬성 |
17 |
황해 |
수령 |
관료 |
반대 |
17 |
황해 |
품관촌민 |
품관촌민 |
찬성 |
4454 |
황해 |
품관촌민 |
품관촌민 |
반대 |
15601 |
충청 |
대신 등 |
관료 |
반대 |
2 |
충청 |
수령 |
관료 |
찬성 |
35 |
충청 |
수령 |
관료 |
반대 |
26 |
충청 |
품관촌민 |
품관촌민 |
찬성 |
6982 |
충청 |
품관촌민 |
품관촌민 |
반대 |
14013 |
강원 |
수령 |
관료 |
찬성 |
5 |
강원 |
수령 |
관료 |
반대 |
10 |
강원 |
품관촌민 |
품관촌민 |
찬성 |
939 |
강원 |
품관촌민 |
품관촌민 |
반대 |
6888 |
함길 |
대신 등 |
관료 |
반대 |
1 |
함길 |
수령 |
관료 |
찬성 |
3 |
함길 |
수령 |
관료 |
반대 |
14 |
함길 |
품관촌민 |
품관촌민 |
찬성 |
75 |
함길 |
품관촌민 |
품관촌민 |
반대 |
7387 |
경상 |
수령 |
관료 |
찬성 |
55 |
경상 |
수령 |
관료 |
반대 |
16 |
경상 |
품관촌민 |
품관촌민 |
찬성 |
36262 |
경상 |
품관촌민 |
품관촌민 |
반대 |
377 |
전라 |
대신 등 |
관료 |
반대 |
2 |
전라 |
수령 |
관료 |
찬성 |
42 |
전라 |
수령 |
관료 |
반대 |
12 |
전라 |
품관촌민 |
품관촌민 |
찬성 |
29505 |
전라 |
품관촌민 |
품관촌민 |
반대 |
257 |
str(sejong.poll.kr)
## 'data.frame': 44 obs. of 5 variables:
## $ counts : int 21 194 259 393 443 117 1123 71 29 5 ...
## $ vote : Factor w/ 2 levels "찬성","반대": 1 2 1 2 1 2 1 2 1 2 ...
## $ class : Factor w/ 5 levels "대신 등","3품이하현직",..: 1 1 2 2 3 3 5 5 4 4 ...
## $ region : Factor w/ 10 levels "서울","유후사",..: 1 1 1 1 1 1 2 2 3 3 ...
## $ class.2: Factor w/ 2 levels "관료","품관촌민": 1 1 1 1 1 1 2 2 1 1 ...
Compare the votes by class.2
, (Bureaucrats vs Commons)
vote.class.2 <- xtabs(counts ~ vote + class.2, data = sejong.poll.kr)
kable(vote.class.2, caption = "관료와 품관촌민")
관료와 품관촌민
찬성 |
915 |
97742 |
반대 |
845 |
73304 |
vote.class.2.a <- cbind("관료" = rowSums(vote.class.a[, -5]), "품관촌민" = vote.class.a[, 5])
kable(vote.class.2.a, caption = "관료와 품관촌민")
관료와 품관촌민
찬성 |
915 |
97742 |
반대 |
845 |
73304 |
Add subtotals to the margins,
vote.class.2.am <- addmargins(vote.class.2)
colnames(vote.class.2.am)[3] <- "계"
rownames(vote.class.2.am)[3] <- "계"
kable(vote.class.2.am, align = rep("r", 3))
찬성 |
915 |
97742 |
98657 |
반대 |
845 |
73304 |
74149 |
계 |
1760 |
171046 |
172806 |
Compute the marginal proportions. Note the use of digits = 3
and nsmall = 1
.
kable(format(prop.table(vote.class.2, margin = 2)*100, digits = 3, nsmall = 1), caption = "관료와 품관촌민", align = rep("r", 2))
관료와 품관촌민
찬성 |
52.0 |
57.1 |
반대 |
48.0 |
42.9 |
Votes by region with respect to class.2
Count the vote by region class.2 wise.
class.2 <- sejong.poll.kr$class.2
vote.region.bureaus <- xtabs(counts ~ vote + region, data = sejong.poll.kr, class.2 == "관료", drop = TRUE)
kable(vote.region.bureaus, caption = "집계(관료)")
집계(관료)
찬성 |
723 |
29 |
6 |
17 |
35 |
5 |
3 |
55 |
42 |
반대 |
704 |
5 |
36 |
17 |
28 |
10 |
15 |
16 |
14 |
# xtabs(counts ~ vote + region, data = sejong.poll.kr[class.2 == "Bureaus", ], drop = TRUE)
vote.region.commons <- xtabs(counts ~ vote + region, data = sejong.poll.kr, class.2 == "품관촌민", drop = TRUE)
kable(vote.region.commons, caption = "집계(품관촌민)", align = rep("r", 9))
집계(품관촌민)
찬성 |
1123 |
17076 |
1326 |
4454 |
6982 |
939 |
75 |
36262 |
29505 |
반대 |
71 |
236 |
28474 |
15601 |
14013 |
6888 |
7387 |
377 |
257 |
Seoul has three times more Bureaucrats than other regions, so analyse further.
region <- sejong.poll.kr$region
vote.seoul.class <- xtabs(counts ~ vote + class, data = sejong.poll.kr, region == "서울", drop = TRUE)
kable(vote.seoul.class, caption = "서울")
서울
찬성 |
21 |
259 |
443 |
반대 |
194 |
393 |
117 |
kable(format(prop.table(vote.seoul.class, margin = 2)*100, digits = 3, nsmall = 1), caption = "서울", align = rep("r", 3))
서울
찬성 |
9.77 |
39.72 |
79.11 |
반대 |
90.23 |
60.28 |
20.89 |
Chungcheong’s case.
vote.chung.class <- xtabs(counts ~ vote + class, data = sejong.poll.kr, region == "충청", drop = TRUE)
kable(format(prop.table(vote.chung.class, margin = 2)*100, digits = 3, nsmall = 1), caption = "충청", align = rep("r", 3))
충청
찬성 |
0.0 |
57.4 |
33.3 |
반대 |
100.0 |
42.6 |
66.7 |
- Save the working directory image.
save.image(file = "sejong_poll_data_kr.RData")