기본세팅

# 필요한 패키지 로드
library(readr)

# CSV 파일의 URL 지정
url <- "https://raw.githubusercontent.com/jeongmoonwon/jm/main/%5BWeek7%5D%20univ.csv"

# CSV 파일 읽기
dat <- read.csv(url)

# 읽어온 데이터 확인
dat
##     level pass
## 1       1    2
## 2       2    2
## 3       2    1
## 4       1    1
## 5       1    2
## 6       1    2
## 7       2    1
## 8       1    2
## 9       1    1
## 10      3    1
## 11      1    2
## 12      2    2
## 13      1    2
## 14      2    2
## 15      1    2
## 16      1    1
## 17      1    1
## 18      2    1
## 19      3    1
## 20      1    1
## 21      2    1
## 22      1    2
## 23      2    2
## 24      1    2
## 25      1    2
## 26      3    2
## 27      1    2
## 28      2    1
## 29      1    1
## 30      1    1
## 31      1    1
## 32      2    1
## 33      2    1
## 34      1    1
## 35      3    1
## 36      2    1
## 37      2    2
## 38      2    1
## 39      1    2
## 40      1    1
## 41      2    1
## 42      2    2
## 43      1    2
## 44      3    1
## 45      2    2
## 46      2    2
## 47      1    2
## 48      3    1
## 49      2    1
## 50      2    1
## 51      2    1
## 52      1    1
## 53      1    1
## 54      2    1
## 55      3    2
## 56      2    1
## 57      1    1
## 58      2    1
## 59      1    2
## 60      2    1
## 61      1    1
## 62      2    2
## 63      3    1
## 64      2    1
## 65      2    1
## 66      2    1
## 67      1    1
## 68      1    1
## 69      2    1
## 70      2    1
## 71      1    1
## 72      1    2
## 73      2    2
## 74      1    2
## 75      2    1
## 76      2    2
## 77      1    2
## 78      1    1
## 79      1    1
## 80      2    2
## 81      1    2
## 82      3    1
## 83      1    2
## 84      2    1
## 85      3    2
## 86      3    1
## 87      1    1
## 88      2    1
## 89      1    1
## 90      3    2
## 91      1    1
## 92      3    2
## 93      2    2
## 94      3    1
## 95      1    1
## 96      3    1
## 97      1    1
## 98      2    2
## 99      3    2
## 100     2    1
## 101     3    1
## 102     1    1
## 103     2    1
## 104     3    1
## 105     1    1
## 106     2    1
## 107     3    2
## 108     1    2
## 109     2    1
## 110     1    1
## 111     1    1
## 112     2    1
## 113     1    1
## 114     1    2
## 115     3    1
## 116     1    2
## 117     2    2
## 118     1    1
## 119     3    1
## 120     1    1
## 121     2    1
## 122     2    2
## 123     3    1
## 124     3    1
## 125     2    1
## 126     2    1
## 127     2    1
## 128     1    2
## 129     3    2
## 130     2    2
## 131     2    2
## 132     1    1
## 133     3    1
## 134     2    1
## 135     2    2
## 136     1    1
## 137     2    1
## 138     2    1
## 139     1    1
## 140     3    1
## 141     2    1
## 142     3    1
## 143     2    1
## 144     1    1
## 145     2    1
## 146     3    1
## 147     2    1
## 148     1    1
## 149     2    1
## 150     3    1
## 151     2    1
## 152     2    2
## 153     2    1
## 154     1    2
## 155     3    1
## 156     2    1
## 157     2    1
## 158     3    2
## 159     1    1
## 160     1    2
## 161     2    1
## 162     3    1
## 163     2    1
## 164     3    1
## 165     2    2
## 166     1    2
## 167     3    2
## 168     1    1
## 169     1    1
## 170     2    2
## 171     3    2
## 172     1    1
## 173     3    2
## 174     2    2
## 175     1    2
## 176     3    2
## 177     1    2
## 178     2    1
## 179     3    2
## 180     1    1
## 181     1    1
## 182     3    1
## 183     2    1
## 184     3    2
## 185     1    2
## 186     1    2
## 187     3    1
## 188     2    1
## 189     1    2
## 190     3    1
## 191     1    2
## 192     3    1
## 193     3    1
## 194     1    2
## 195     2    2
## 196     1    1
## 197     1    1
## 198     1    2
## 199     3    2
## 200     1    1
## 201     3    2
## 202     2    1
## 203     3    1
## 204     1    1
## 205     2    1
## 206     3    2
## 207     1    2
## 208     1    1
## 209     2    1
## 210     3    2
## 211     3    1
## 212     3    2
## 213     1    2
## 214     2    2
## 215     1    1
## 216     1    1
## 217     2    2
## 218     1    2
## 219     2    2
## 220     1    2
## 221     3    2
## 222     2    1
## 223     1    2
## 224     3    2
## 225     3    2

##귀무가설/대립가설 세우기 H0: 학력수준에 따라 자녀의 대학진학에 영향이 있다 H0: 학력수준에 따라 자녀의 대학진학에 영향이 없다

##box plot으로 시각화

# 비율 계산
library(dplyr) 
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
summary_data <- dat %>%
  group_by(level, pass) %>% 
  summarise(count = n()) %>% 
  mutate(percentage = count / sum(count) * 100)
## `summarise()` has grouped output by 'level'. You can override using the
## `.groups` argument.
# 막대 그래프 그리기
library(ggplot2)
ggplot(summary_data, aes(x = factor(level), y = percentage, fill = factor(pass))) +
  geom_bar(stat = "identity", position = "dodge") + # position = "dodge": 막대를 서로 겹치지 않도록 표시
  labs(x = "level", y = "%", fill = "pass") + # 축 이름 
  scale_fill_discrete(labels = c("Yes", "No")) # 범례

##Chi-squared test 수행

library('gmodels')
CrossTable(dat$level, dat$pass, chisq = T, # chisq=T 를 반드시 지정
           expected = T, dnn=c("부모 학력 수준","자녀의 대학진학"), # expected=T를 지정하면 cell당 기대빈도 표시 
           prop.r=F, prop.c=F, prop.t=F) 
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## |              Expected N |
## | Chi-square contribution |
## |-------------------------|
## 
##  
## Total Observations in Table:  225 
## 
##  
##              | 자녀의 대학진학 
## 부모 학력 수준 |         1 |         2 | Row Total | 
## -------------|-----------|-----------|-----------|
##            1 |        49 |        40 |        89 | 
##              |    53.400 |    35.600 |           | 
##              |     0.363 |     0.544 |           | 
## -------------|-----------|-----------|-----------|
##            2 |        55 |        27 |        82 | 
##              |    49.200 |    32.800 |           | 
##              |     0.684 |     1.026 |           | 
## -------------|-----------|-----------|-----------|
##            3 |        31 |        23 |        54 | 
##              |    32.400 |    21.600 |           | 
##              |     0.060 |     0.091 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |       135 |        90 |       225 | 
## -------------|-----------|-----------|-----------|
## 
##  
## Statistics for All Table Factors
## 
## 
## Pearson's Chi-squared test 
## ------------------------------------------------------------
## Chi^2 =  2.766951     d.f. =  2     p =  0.2507057 
## 
## 
## 

##유의수준 5%에서 귀무가설이 기각 가능한지 판단하기 p값이 유의수준 0.05 보다 작은 0.25이기 때문에 귀무가설을 기각한다. <기억!>범위에 포함되면 기각!