01. ๊ต์ก์์ค(education)๊ณผ ํก์ฐ์จ(smoking) ๊ฐ์ ๊ด๋ จ์ฑ์ ๋ถ์ํ๊ธฐ ์ํ ๊ฐ์ค์ ์๋ฆฝํ๊ณ , ์ด๋ฅผ ํ ๋๋ก ๊ฐ์ค์ ๊ฒ์ ํ์์ค.[๋ ๋ฆฝ์ฑ ๊ฒ์ ]
๊ท๋ฌด๊ฐ์ค : ๊ต์ก์์ค๊ณผ ํก์ฐ์จ์ ๊ด๋ จ์ฑ์ด ์๋ค(๋ ๋ฆฝ์ ์ด๋ค).
์ฐ๊ตฌ๊ฐ์ค : ๊ต์ก์์ค๊ณผ ํก์ฐ์จ์ ๊ด๋ จ์ฑ์ด ์๋ค(๋ ๋ฆฝ์ ์ด์ง ์๋ค).
<๋จ๊ณ 1> ํ์ผ ๊ฐ์ ธ์ค๊ธฐ
setwd("c:/ITWILL/2_Rwork/data")
smoke = read.csv("smoke.csv", header=TRUE)
๋ณ์ ๋ณด๊ธฐ
head(smoke) # education, smoking ๋ณ์
<๋จ๊ณ 2> ์ฝ๋ฉ ๋ณ๊ฒฝ - ๋ณ์ ๋ฆฌ์ฝ๋ฉ (๊ฐ๋
์ฑ ์ ๊ณต )
education(๋
๋ฆฝ๋ณ์) : 1:๋์กธ, 2:๊ณ ์กธ, 3:์ค์กธ
smoking(์ข
์๋ณ์): 1:๊ณผ๋คํก์ฐ, 2:๋ณดํตํก์ฐ, 3:๋นํก์ฐ
education ๋ณ์ ๋ฆฌ์ฝ๋ฉ : education2
smoke$education2[smoke$education == 1] = '1:๋์กธ'
smoke$education2[smoke$education == 2] = '2:๊ณ ์กธ'
smoke$education2[smoke$education == 3] = '3:์ค์กธ'
table(smoke$education2)
smoking ๋ณ์ ๋ฆฌ์ฝ๋ฉ : smoking2
smoke$smoking2[smoke$smoking == 1] = "1:๊ณผ๋คํก์ฐ"
smoke$smoking2[smoke$smoking == 2] = "2:๋ณดํตํก์ฐ"
smoke$smoking2[smoke$smoking == 3] = "3:๋นํก์ฐ"
table(smoke$smoking2)
<๋จ๊ณ 3> ๊ต์ฐจ๋ถํ ํ ์์ฑ(table ํจ์ ์ด์ฉ)
table(smoke$education2, smoke$smoking2)
<๋จ๊ณ 4> ๋
๋ฆฝ์ฑ ๊ฒ์ (CrossTable ํจ์ ์ด์ฉ)
CrossTable(x=smoke$education2, y=smoke$smoking2, chisq = T)
Chi^2 = 18.91092 d.f. = 4 p = 0.0008182573
<๋จ๊ณ 5> ๊ฒ์ ๊ฒฐ๊ณผ ํด์
* P-value < ์ํ : ์ ์๋ฏธํ ์์ค์์ ๋ ๋ณ์ธ์ ๊ด๋ จ์ฑ์ด ์๋ค๊ณ ๋ณผ ์ ์๋ค.
02. ๋์ด(age3)์ ์ง์(position) ๊ฐ์ ๊ด๋ จ์ฑ์ ๋จ๊ณ๋ณ๋ก ๋ถ์ํ์์ค. [๋ ๋ฆฝ์ฑ ๊ฒ์ ]
๊ท๋ฌด๊ฐ์ค : ๋์ด์ ์ง์์ ๊ด๋ จ์ฑ์ด ์๋ค.
๋๋ฆฝ๊ฐ์ค : ๋์ด์ ์ง์์ ๊ด๋ จ์ฑ์ด ์๋ค.
[๋จ๊ณ 1] ํ์ผ ๊ฐ์ ธ์ค๊ธฐ
data = read.csv("cleanData.csv")
head(data)
[๋จ๊ณ 2] ๋ณ์ ์ ํ
x = data$position # ํ - ์ง์ ๋ณ์ ์ด์ฉ
y = data$age3 # ์ด - ๋์ด ๋ฆฌ์ฝ๋ฉ ๋ณ์ ์ด์ฉ
[๋จ๊ณ 3] ์ฐ์ ๋๋ฅผ ์ด์ฉํ ๋ณ์๊ฐ์ ๊ด๋ จ์ฑ ๋ณด๊ธฐ - plot(x,y) ํจ์ ์ด์ฉ
plot(x, y) # ๋ ๋ณ์ธ ๊ฐ ์๊ด์ฑ ํ์ธ
[๋จ๊ณ 4] ๋
๋ฆฝ์ฑ ๊ฒ์
chisq.test(x=x, y=y)
X-squared = 287.9, df = 8, p-value < 2.2e-16
[๋จ๊ณ 5] ๊ฒ์ ๊ฒฐ๊ณผ ํด์
๊ท๋ฌด๊ฐ์ค์ ์ง์งํ๋ ํ๋ฅ ์ด ๋งค์ฐ ๋ฎ๋ค. ๋ฐ๋ผ์ ๋งค์ฐ ์ ์๋ฏธํ ์์ค์์
๋ ๋ณ์ธ ๊ฐ์ ์ฐจ์ด๋ฅผ ๋ณด์ธ๋ค.
03. ์ง์ ์ ํ์ ๋ฐ๋ฅธ ์๋ต์ ๋์ ์ฐจ์ด๊ฐ ์๋๊ฐ๋ฅผ ๋จ๊ณ๋ณ๋ก ๊ฒ์ ํ์์ค.[๋์ง์ฑ ๊ฒ์ ]
[๋จ๊ณ 1] ํ์ผ ๊ฐ์ ธ์ค๊ธฐ
response = read.csv("response.csv")
head(response) # ๋ณ์ ๋ณด๊ธฐ
job response
[๋จ๊ณ 2] ์ฝ๋ฉ ๋ณ๊ฒฝ
job ์นผ๋ผ ์ฝ๋ฉ ๋ณ๊ฒฝ : 1:ํ์, 2:์ง์ฅ์ธ, 3:์ฃผ๋ถ
response ์นผ๋ผ ์ฝ๋ฉ ๋ณ๊ฒฝ : 1:๋ฌด์๋ต, 2:๋ฎ์, 3:๋์
job ๋ณ์ ๋ฆฌ์ฝ๋ฉ : job2
response$job2[response$job==1] = '1:ํ์'
response$job2[response$job==2] = '2:์ง์ฅ์ธ'
response$job2[response$job==3] = '3:์ฃผ๋ถ'
response ๋ณ์ ๋ฆฌ์ฝ๋ฉ : response2
response$response2[response$response==1] = '1:๋ฌด์๋ต'
response$response2[response$response==2] = '2:๋ฎ์'
response$response2[response$response==3] = '3:๋์'
[๋จ๊ณ 3] ๊ต์ฐจ๋ถํ ํ ์์ฑ
table(response$job2, response$response2)
1:๋ฌด์๋ต 2:๋ฎ์ 3:๋์
1:ํ์ 25 37 8
2:์ง์ฅ์ธ 10 62 53
3:์ฃผ๋ถ 5 41 59
[๋จ๊ณ 4] ๋์ง์ฑ ๊ฒ์
CrossTable(response$job2, response$response2, chisq = T)
Chi^2 = 58.2081 d.f. = 4 p = 6.900771e-12
[๋จ๊ณ 5] ๊ฒ์ ๊ฒฐ๊ณผ ํด์
๋งค์ฐ ์ ์๋ฏธํ ์์ค์์ ์ง์
์ ์ ํ์ ๋ฐ๋ฅธ ์๋ต์จ์ ์ฐจ์ด๊ฐ ์๋ค๊ณ ๋ณผ ์ ์๋ค.
'๊ฐ์ธ๊ณต๋ถ > R' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
29. R ์๊ด๋ถ์ ์ฐ์ต๋ฌธ์ (0) | 2021.10.13 |
---|---|
28. R T๊ฒ์ ์ฐ์ต๋ฌธ์ (0) | 2021.10.12 |
22. R ํ ์คํธ๋ง์ด๋ ์ฐ์ต๋ฌธ์ (0) | 2021.10.06 |
21. R ํต๊ณ๊ธฐ๋ณธ๊ฐ๋ 2 (ํต๊ณ๋ถ์๋ชจ๋ธ) (0) | 2021.10.05 |
19. R EDA, ๋ฐ์ดํฐ์ ์ฒ๋ฆฌ ์ฐ์ต๋ฌธ์ (0) | 2021.10.03 |